Open-Terrarium/collision.lua
2017-06-20 03:49:11 -04:00

36 lines
825 B
Lua

--basic grid based collision detection
function collision(oldposx,oldposy)
if (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.35 then
local oldposx,oldposy = player.playerx,player.playery
player.playery = player.playery + 1
collision(oldposx,oldposy)
gravtimer = 0
end
else
gravtimer = 0
end
end