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
Comments
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
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/
Cheers
Mike
http://www.sharksoupstudios.com