RPG collision detection!
Posted: Wed Oct 13, 2010 12:50 am
Code: Select all
function love.load()
-- our tiles
tile = {}
for i=0,3 do -- change 3 to the number of tile images minus 1.
tile[0] = love.graphics.newImage( "test1.bmp" ) --the tiles are loaded to be used.
tile[1] = love.graphics.newImage( "test2.bmp" )
tile[2] = love.graphics.newImage( "test3.bmp" )
tile[3] = love.graphics.newImage( "pic.png" )
end
-- the map; the 0,1,2,3 etc. are the things you see above! (tile[0] etc.)
map={
{ 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0},
{ 3, 1, 0, 0, 2, 2, 2, 0, 3, 0, 3, 0, 1, 1, 1, 0, 0, 3, 0, 0, 0},
{ 3, 1, 0, 0, 2, 0, 2, 0, 3, 0, 3, 0, 1, 0, 0, 0, 0, 0, 3, 0, 0},
{ 3, 1, 1, 0, 2, 2, 2, 0, 0, 3, 0, 0, 1, 1, 0, 0, 0, 0, 0, 3, 0},
{ 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 3},
{ 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 2},
{ 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 2, 2, 2, 0, 3, 3, 3, 0, 1, 1, 1, 0, 2, 0, 0, 0, 0,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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 1, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
}
--need to find a way to make "buffer tiles" outside the map, like Pokemon
-- map variables
map_w = #map[1] -- Obtains the width of the first row of the map
map_h = #map -- Obtains the height of the map
map_x = 0 --starting position of when the game is started.
map_y = 0
map_display_buffer = 2 -- We have to buffer one tile before and behind our viewpoint.
-- Otherwise, the tiles will just pop into view, and we don't want that.
map_display_w = 20
map_display_h = 15
tile_w = 64
tile_h = 64
love.keyboard.setKeyRepeat( 20, 400 )
end
function draw_map()
offset_x = map_x % tile_w
offset_y = map_y % tile_h
firstTile_x = math.floor(map_x / tile_w)
firstTile_y = math.floor(map_y / tile_h)
for y=1, (map_display_h + map_display_buffer) do
for x=1, (map_display_w + map_display_buffer) do
-- Note that this condition block allows us to go beyond the edge of the map.
if y+firstTile_y >= 1 and y+firstTile_y <= map_h
and x+firstTile_x >= 1 and x+firstTile_x <= map_w
then
love.graphics.draw(
tile[map[y+firstTile_y][x+firstTile_x]],
(x*tile_w) - offset_x - tile_w/2,
(y*tile_h) - offset_y - tile_h/2)
end
end
end
end
function love.keypressed(key,unicode)
-- get input
if key == "up" then
map_y = map_y -64 --Look into a way to make this transition work smoothly but still snap to the tile.
end
if key == "down" then
map_y = map_y+64
end
if key == "left" then
map_x = map_x -64
end
if key == "right" then
map_x = map_x+64
end
--we need a button to give the four diagnol arrows, signalling which button pushes can occur at those arrows. Then, down-left, down-right, etc. need to make their movement.
-- check boundaries
if map_x < 0 then
map_x = 0
end
if map_y < 0 then
map_y = 0
end
if map_x > map_w * tile_w - map_display_w * tile_w then
map_x = map_w * tile_w - map_display_w * tile_w
end
if map_y > map_h * tile_h - map_display_h * tile_h then
map_y = map_h * tile_h - map_display_h * tile_h
end
if
end
function love.draw()
draw_map()
end
This was my thought process: I could tell it that tile[1], tile[2], and tile[3] are impassable objects. Thus, it does not move through the object (it's position stays the same). However, I do not know how to approach writing this. I do not know how to have it find what tile it is on, it seems like it draws the tiles, but that there is no real way to get back to the position of the tiles (if you get what I mean). I have been trying for a while now, and I am absolutely stumped!
I'm not looking for someone to write it for me, though... I want to try to get this on my own!
(Also, yes, i know there are a lot of comments. These were for someone else.. who happened to have too busy of a schedule to work with me anymore >.<;)
Thanks for helping a newbie out.