Open-Terrarium/collision.lua
2017-06-20 04:16:34 -04:00

40 lines
1.0 KiB
Lua

--basic grid based collision detection
function collision(oldposx,oldposy)
--stairs
if (player.playerx <= mapwidth and player.playerx > 1) and
(player.playery < mapheight and player.playery > 1) and
tiles[player.playerx][player.playery]["block"] == 2 then
player.playery = player.playery - 1
elseif (player.playerx > mapwidth or player.playerx <= 0) or (player.playery > mapheight or player.playery <= 0) or tiles[player.playerx][player.playery]["block"] ~= 0 then
player.playerx,player.playery = oldposx,oldposy
--can't move
oof:setPitch(love.math.random(65,100)/100)
oof:stop()
oof:play()
return(true)
end
end
--make the player fall when in air
gravtimer = 0
function gravity(dt)
if player.playery < mapheight and tiles[player.playerx][player.playery+1]["block"] == 0 then
gravtimer = gravtimer + dt
if gravtimer >= 0.2 then
local oldposx,oldposy = player.playerx,player.playery
player.playery = player.playery + 1
collision(oldposx,oldposy)
gravtimer = 0
end
else
gravtimer = 0
end
end