Jump to content

  • Log In with Google      Sign In   
  • Create Account


hdnine

Member Since 19 Dec 2004
Offline Last Active Jun 06 2013 05:52 AM

Topics I've Started

[JS] Confused how to retrieve variable value from within a function

09 November 2012 - 03:57 AM

I'm having problems seeing how to solve the particular issue of returning the tile size to the caller. I have my own namespace called "goon" where i've added different functions, for example Tile below which i use to build my level. If i try:

console.log(goon.Tile.size); // This returns undefined.
console.log(goon.Tile.getTileSize()); // Throws the error: goon.Tile.getTileSize is not a function

[source lang="jscript"]goon.Tile = function(type, col, row) { this.type = type; this.size = 30; this.top = row * this.size; this.left = col * this.size; this.bottom = row * this.size + this.size; this.right = col * this.size + this.size; this.getTileSize = function() { return this.size; };}[/source]

So i tried adding a prototype and removed the above but this till doesn't work so obviously i'm doing something wrong here. I just need to get the size value from the function, is this even possible?

[source lang="jscript"]goon.Tile = function(type, col, row) { this.type = type; this.size = 30; this.top = row * this.size; this.left = col * this.size; this.bottom = row * this.size + this.size; this.right = col * this.size + this.size;}goon.Tile.prototype.getTileSize = function() { return this.size;};[/source]

Is the problem that i need to invoke or call the function first? Maybe you can't have a function within a function. Anyway, any help would be apprecieated.

2D tile level drawing performance, which of two approaches is best?

29 October 2012 - 09:57 AM

Hey everyone,

As i was trying to implement my tile based level in the game i started thinking about performance. Right now the level is set up like this:

[source lang="jscript"]map: [ [2,2,2,0,0,0,0,0,0,0,0,0,0,0,2,2,2,1,1,1,0,0,0,0,0,0,0,0,0,0,0,2,2,2], [2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2], [2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2], [0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,0,0,0,0], [0,0,2,0,0,2,0,0,0,0,0,2,2,0,0,0,0,0,0,2,0,0,2,0,0,0,0,0,2,2,0,0,0,0], [0,0,0,2,2,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0,2,0,0,0,0], [2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2], [2,0,0,0,0,0,0,2,2,2,0,0,0,0,0,0,2,2,0,0,0,0,0,0,2,2,2,0,0,0,0,0,0,2], [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]],[/source]
The width of the screen is equal to 17 of the above 23 tiles in width. Right now, all of the above are pushed into a current level array which the drawing routine later renders to screen. This basically means that all tiles that are not 0 have their own X-Y positions and as the player moves right, all of them are updated with new X positions. I was figuring if this was a bad approach since there are so many tiles to keep track of. Half of them are rendered off-screen but still...

Would it be better to just render the first 17 columns and then have a routine that pushes the next column set into the current level array and remove the "left most" ones as the player moves right and vice versa? Would this improve performance?

The thing is that i would first have to find which column of tiles that are the next in line, pull them out, set their X-Y positions and then push them into the current level array and lastly render them out. Seems equally taxing from a performance stand point so what do you guys think?

Javascript collision detection in tile based game (demo page available)

22 October 2012 - 05:14 AM

The demo can be tested here (it also has a syntax highlighter)
http://www.wonderwor...y-and-friction/

Hey everyone,

I've yet again started a small game project where immediately i got stuck on the same subject as before, collision detection. I know that i'm not that good at math so i went with the approach i assumed as being correct. That is testing the player object against all tile objects on screen to see if there is a collision:

The syntax highlighter went mad on me so i pasted the code as text instead (it can however be viewed at the link above)

// If moving up
for(var index in Level.currentLayout) {
var obj = Level.currentLayout[index];

if(87 in Game.keys || Player.vel.y < 0 || Player.airborn) {
if(((Player.right >= obj.left && Player.right <= obj.right) || (Player.left <= obj.right && Player.left >= obj.left)) && (Player.top - distance >= obj.top && Player.top - distance <= obj.bottom)) {
Player.pos.y = obj.bottom + 1;
Player.collision.top = true;
break;
} else {
Player.collision.top = false;
}
}
}
// If moving left
for(var index in Level.currentLayout) {
var obj = Level.currentLayout[index];

if(65 in Game.keys || Player.vel.x < 0) {
if((Player.left - distance <= obj.right && Player.left - distance >= obj.left) && ((Player.bottom >= obj.top && Player.bottom <= obj.bottom) || (Player.top >= obj.top && Player.top <= obj.bottom))) {
Player.vel.x *= -1;
Player.collision.left = true;
break;
} else {
Player.collision.left = false;
}
}
}
// If moving down
for(var index in Level.currentLayout) {
var obj = Level.currentLayout[index];

if(((Player.right >= obj.left && Player.right <= obj.right) || (Player.left <= obj.right && Player.left >= obj.left)) && (Player.bottom + distance >= obj.top && Player.bottom + distance <= obj.bottom)) {
Player.pos.y = obj.top - Player.height - 1;
Player.collision.bottom = true;
Player.airborn = false;
break;
} else {
Player.collision.bottom = false;
Player.airborn = true;
}
}
// If moving right
for(var index in Level.currentLayout) {
var obj = Level.currentLayout[index];

if(68 in Game.keys || Player.vel.x > 0) {
if((Player.right + distance >= obj.left && Player.right + distance <= obj.right) && ((Player.bottom >= obj.top && Player.bottom <= obj.bottom) || (Player.top >= obj.top && Player.top <= obj.bottom))) {
Player.vel.x *= -1;
Player.collision.right = true;
break;
} else {
Player.collision.right = false;
}
}
}

It kinda works but i keep having this feeling that the code is just too complicated for a tile based 2D platform game, or, simply that it's badly written? Without physics it works really well but with it i get stuck on edges between tiles and on tiles. Alos, the gravity seems too slow but if i increase it, other things like the jumping starts to act up so i in turn have to increase that to compensate and all of a sudden everything gets messed up.

I have some code that generates the level tiles and displays them on canvas which works nicely. The problem is somewhere with the collision detection and since my math really suck, i have been reading and reading about different approaches and i just don't understand most of them.

I would really appreciate if someone could give me some input on a more correct way to handle this, preferably with example code or demo pages. Thanks a bunch!

Also: please add Javascript/Canvas to the Topic Prefix ^^

[web] Need help with my PHP-based Text Adventure parser

12 October 2011 - 03:56 AM

Hey everyone!

I have been working about a month on a PHP-based text adventure engine which at this point can parse two words like TAKE SUITCASE or EXAMINE SOFA. All verbs and objects are located in arrays which are fetched from a database at the start of the game. The problem i have is that i can't seem to find a good way to parse text if the input is more than two words.

In PHP i use EXPLODE to seperate the words and then check each word against the different arrays that i have -- verbs, objects, prepositions, noise words and so on. These are marked with "v" if verb, "p" if preposition and so on to determine what kind of word the are right... So far i can write TAKE <object> and it will be picked up, or, EXAMINE <object> and a text explaining the object will be shown.

So here is the problem which i really would like to find a more elegant solution for -- i use IF / ELSE and SWITCH / CASE conditional statements to handle the parsing and these are quickly getting larger as i try to move in to a third word. My plan is for the text parser to handle at least 5-7 words not counting the noise words like "the". Here is some example code:

// First word
if(isset($input[0]))
{
	$word1 = checkWord(0,$input);
}

// ---------------

// WORD 1 - SYSTEM
// ---------------
if(isset($word1['type']) && $word1['type'] == 's')
{
	// System commands can only be alone, any following words will be ignored
	eval($word1['word']['action']);
}
// ------------------
// WORD 1 - DIRECTION
// ------------------
elseif(isset($word1['type']) && $word1['type'] == 'd')
{
	// This will set the player to the destination she has chosen
	$array = $word1['word']['dest'];
	$_SESSION['player']->setPos($array);
}

So basically the only words that are allowed later on when parsing three or more words are the ones starting with a verb. So the code is similar to this:

// -------------
// WORD 1 - VERB
// -------------
elseif(isset($word1['type']) &&  $word1['type']== 'v')
{
	// The first word is...
	switch($word1['word']['name'])
	{
		// BACK
		case 'back':
			// If the verb is "back" then go to the last position the player was at
			eval($word1['word']['action']);
		break; // back
		// GO
		case 'exit': case 'enter': case 'crawl': case 'go': case 'move': case 'run': case 'walk':
			// --------------------
			// WORD 2 - PREPOSITION
			// --------------------
			if(isset($word2['type']) && $word2['type'] == 'p')
			{
				// The second word is...
				switch($word2['word']['name'])
				{
					case 'above': case 'over':
					break;
					case 'across':
					break;
					case 'after':
					break;
					case 'around':
					break;
					case 'behind':
					break;
					case 'below': case 'beneath': case 'under':
					break;
					case 'between':
					break;
					case 'beyond':
					break;
					case 'in': case 'inside': case 'into':
					break;
					case 'near':
					break;
					case 'on': case 'onto':
					break;
					case 'out': case 'outside':
					break;
					case 'through':
					break;
					case 'to': case 'towards':
					break;
					case 'until':
					break;
				}
			}
			// ------------------
			// WORD 2 - DIRECTION
			// ------------------
			elseif(isset($word2['type']) && $word2['type'] == 'd')
			{
				// This is like saying "go north" or "go kitchen" instead if just "north" or "kitchen"
				$array = $word2['word']['dest'];
				$_SESSION['player']->setPos($array);
			}
			else
			{
				goto showError;
			}


I realized that at this stage there are a lot of repetitions to start off with and A LOT more code just to check every possible combination like VERB+OBJECT, VERB+PREPOSITION+OBJECT, VERB+OBJECT+PREP+OBJECT and so on. Please help me... there has to be a better way to handle this than thousands of lines of code to check all possible combos? I was thinking of having some sort of relational database table which would contain all verbs and which prepositions they can be followed by, not sure if this is a better solution.

Another problem is that some verbs only work with specific prepositions or objects, for example WALK ACROSS BRIDGE works fine, TAKE ACROSS BRIDGE doesn't make sense. LOOK (verb) UNDER (preposition) TABLE (noun/object) will work fine, WEAR UNDER TABLE doesn't. Hopefully you see the problem since the combinations seems to be endless. Two words are easy, three gets complicated and more than that seems impossible.

Thanks,
Krister Nielsen
Sweden

Need some quick help for a school project

19 December 2004 - 04:06 AM

Hey everyone, Me and three others are planing on doing a very basic text adventure game in Visual C++ and the console window. It's for a school project and i was hoping that someone could help me with a specific menu system. We are all very new to C++ and have recently learned about for example classes and the STL. I was thinking of making a menu like this one: http://www.wonderworks.se/online/menu.gif ...where the user could choose by using the arrow keys and highlighting a choice, then pressing ENTER to continue. Is this at all possible in the console window or does it have to be made in Win32? Now Win32 is something that we haven't gone thru yet in class so we really want to stick to the more simpler console window. Also: Is it possible to make the menu so that the cursor "_" doesn't show on screen after the highlighted text "START the game_" as i have seen in some cases? One more thing: Can i change the background color to, for example grey with white text? Any help would be appreciated. Thanks! Krister Nielsen Karlshamn, Sweden

PARTNERS