I'm a newby myself, but I happen to be making the exact same kind of game (only with the random dungeons). Here's what I do:
First I check which tile the player is standing on. This is simple to figure out, since you know what the x-location of the player is, and what the offset is of the map and the camera. So, since you know how big a tile is, and what the size is of your map, you can calculate the exact tile that the player is on. Its simply a matter of unleashing some good old math on the x and y location of the player.
Most of the newb tutorials write the tiles in a table, with an x and y value. What I did was create a second table, where I write all the collision. I created a reference file that the game loads, which basically instructs the game what the collision type is for any tile on my sprite sheet. Then as I create the initial sprite batch, I also look up the collision, and write the collision to a separate table of the exact same size as the map. So each tile in the map-table, also has an entry in my collision-table.
Code: Select all
-- Collision reference sheet for the tilesheet
--"X"=wall--"."=floor--"W"=water--"L"=lava--"F"=firepit--"E"=emptyfirepit
--"P"=pit--"T"=spiketrap--"S"=stairs--"U"=hammerblockup--"C"=chest
--"D"=hammerblockdown--"I"=blockup--"O"=blockdown--"B"=blockswitch--"Q"=pressurepad
local collisionsheet={
{ "X","X","X","X","X","X","X","X","X","X","X","X","X","X","X","X","X"},
{ "X","X","X","X","X","X","X","X","X","X","X","X","X","X","X","X","X"},
{ "X","X","X","X","X","X","X","X","X","X","X","X","X","X","X","X","."},
{ "X","X","X","X","X","X","X","X","X","X","X","X","X","X","X","X","."},
{ "X","X","X",".",".",".",".",".",".",".","X","X","X","X","X","X","."},
{ "X","X","X",".",".",".",".",".",".",".","X","X","X","X","X","X","Q"},
{ "X","X","X","X","X","X","X","X","X","X","X","X","X",".",".",".","."},
{ "X","X","X","X","X","X","X","X","X","X","X","X",".",".",".","X","P"},
{ "X","X","X","X","X","X","X","X","X","X","X","X","S","S","S","S","X"},
{ "X","X","X","X","X","X","X","X","X","X","X","X","S","S","S","S","X"},
{ "X","X","X","X","X","X","X","X","X","X","X","X","X","X","X","X","X"},
{ "X","X","X","X","X","X","X","X","X","X","X","X","X","X","X","X","."},
{ "X","X","X","X","X","X","X","X","X","X","X","X","X","X","X","X","."},
{ "X","X","X","X","X","X","X","X","X","X","X","X","X","X","X","X","."},
{ "X","X","X","X","X","X","X","X","X","X","X","X","X","X","X","X","."},
{ "X","X","X","X","X","X","X","X","X","X","X","X","X","X","X","X","."},
{ "X","X","X","X","X","X","X","X","X","X","X","X","S","S","S","S","."},
{ "X","X","X","X","X","X","X","X","X","X","X","X","S","S","S","S","."},
{ ".",".",".",".",".",".","X","X","X","X",".",".",".",".",".",".","X"},
{ ".",".",".",".",".",".","X","X","X","X",".",".",".",".",".",".","."},
{ ".","X","X","X","X","X","X","E","F","F","F","X","X","X","T","T","T"},
{ ".","X","X","X","X","X","X","O","I","I","O","I","I",".",".","X","X"},
{ "L","W","W","W","X",".",".",".","X",".",".","X",".","B","B","X","."},
{ "L","X","X","X","X",".","X","X","X",".","X","X","X",".",".",".","."},
{ "L","X","X","X",".",".",".","X","X","X","X","X","X",".",".",".","."},
{ "X","X","X","X","X",".",".","X","X","X","X","X",".",".",".",".","."},
{ "X","X","X","X","X",".",".","X","X","X",".",".",".","D","U","U","U"},
{ ".",".",".",".","X",".",".",".",".",".",".",".","S","S","S","S","C"},
{ ".",".",".",".",".",".",".",".",".",".",".",".","S","S","S","S","X"},
{ ".",".",".",".","S","S","S","S","S","S","S","S","X","X","X","X","C"},
{ "C","C","X","X","S","S","S","S","S","S","S","S","X","X","X","X","X"}
}
return collisionsheet
An "X" is a wall, and a "." is a floor. I load this file, and then all this information is saved to a variable. So I only need to know what sprite the tile is using, and then check this table to know if there's an "X" (a collision).
And whenever I change the tiles in the spritebatch, I run this script to also update the collisions-table accordingly. The collisions-table must always be updated along with the map-table.
So since I now know what tile the player is standing on, I can check the collision table to see if the tile is considered a wall or a floor. I can also check this for any adjacent tiles. For example, if the player is moving right, I check tile: playertileX+1,playertileY
To make sure that the player doesn't cheat by walking between two tiles, I check 3 tiles in a row in front of the player. Lastly, I make sure that the sprite of the player is actually overlapping with the location of the tile. Because maybe the player is still a few pixels away from the wall. I also auto correct the location of the player, so that he is no longer overlapping with the wall. Because I don't want the player to push himself through the wall.
One important tip I can give you, is to debug the collision by drawing a rectangle on the location you are checking, like I did in the picture. This allows you to check if the location is updated correctly as the map is scrolling. I use this code to draw the debug info:
"collisionTile" is a table that contains the number of the tile, and a boolean that either indicates a collision or not. I have another script that checks the actual collision. I then run this script over all those tiles when drawing the game. The script automatically draws the rectangle as red, if the player is colliding, or green if there is no collision.
If all of this sounds a bit overwhelming, then start by figuring out what tile the player is on, and drawing a rectangle on that tile. That is step one, and you can slowly figure out the rest from there.