Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
I have never done a grid based game. Can anyone help? — Gideros Forum

I have never done a grid based game. Can anyone help?

mykyl66mykyl66 Member
edited February 2012 in Game & application design
I can lay down a grid no issue and have a character move around a grid no issue. I can remove an object on the grid by touching it no issue.

The problem due to having no knowledge at all of grid based games is this.

Lets say I had a 5x5 grid with a character right in the center. I want to limit his moves to forward or backwards left or right to no more or less than 2 places. How would I go about this?

In my head I can imagine a table with all of the grid squares in it and capture the grid square that my character is on to compare between them but have brain ache working it out. No idea if that is even on the right path.

All part of my trying to get fully back into coding mode and trying things I have never done before.

Cheers

Mike
What would you do for your other half?

http://www.sharksoupstudios.com

Comments

  • Umh. Not sure if I got what you mean, but basically as you said you should compare the tile where your player is to the tile that "should" be the destination. If it's a no good tile, than you don't move the player, else you do.
    If the problem is knowing on which tile your player is, given for example that you have a 5x5 grid made of 32x32 pixel tiles, which top left corner is at 0, 0 position you can do:
    xPosInGrid = math.ceil(player.x/32);
    yPosInGrid = math.ceil(player.y/32);

    Then you can use the same logic to check in which grid would happen to be the new position.
    newXPosInGrid = math.ceil(newPos.x/32);
    newYPosInGrid = math.ceil(newPos.y/32);

    and then you compare those:
    if math.abs(xPosInGrid-newXPosInGrid) <= 2 and yPosInGrid == newYPosInGrid then
    move
    elseif math.abs(yPosInGrid-newYPosInGrid) <= 2 and xPosInGrid == newXPosInGrid then
    move
    end

    The yPosInGrid == newYPosInGrid and xPosInGrid == newXPosInGrid is to make sure that the player can't move in diagonal, but only left, right, down, up.

    Hope this helps, let me know if I missed something (:

    Likes: Caroline

    +1 -1 (+1 / -0 )Share on Facebook
  • CarolineCaroline Guru
    edited February 2012
    Do you mean something like this:
    maxGridRows = 5		
    maxGridColumns = 5
     
    currentPosition = {x=3, y=3}
     
    limit = 2 -- this is how far away the valid squares are
    areDiagonalsAllowed = true
     
    --print valid positions
     
    if areDiagonalsAllowed==true then
     
    	for column = currentPosition.x-limit, currentPosition.x+limit do
    		for row = currentPosition.y-limit, currentPosition.y+limit do
    			if column == currentPosition.x and row == currentPosition.y then
    				--is current position
    				print("current Position")
    			else
    				if row > 0 and row <= maxGridRows then
    					if column > 0 and column <= maxGridColumns  then
    						--is in limits
    						print("column:", column, "row:", row)
    					end
    				end	
    			end
    		end
    	end
     
    --without diagonals
    else
    	for column = currentPosition.x-limit, currentPosition.x+limit do
    		if column==currentPosition.x then
    			--is currentPosition
    			print("currentPosition")
    		else
    			if column > 0 and column <= maxGridColumns  then
    				print("column:", column, "row:", currentPosition.y)
    			end
    		end
    	end
     
    	for row = currentPosition.y-limit, currentPosition.y+limit do
    		if row==currentPosition.y then
    			--is currentPosition
    			print("currentPosition")
    		else
    			if row > 0 and row <= maxGridRows then
    				print("column:", currentPosition.x, "row:", row)
    			end
    		end
    	end
    end

    If you know Flash, there's an article here:
    http://www.yarrcade.com/2010/04/05/grid-based-games-part-1-the-basic-grid/

    And another one here:
    http://archive.gamedev.net/archive/reference/programming/features/arttilebase/
  • Lol - if I can find a way to do something without more complicated maths than plus and minusses, I will :).
  • Thanks folks. Sorry for the delay in getting back. That clarifies a few things and gives me some ideas to play with.

    Cheers

    Mike
    What would you do for your other half?

    http://www.sharksoupstudios.com
Sign In or Register to comment.