Open-Terrarium/collision.lua

47 lines
1.1 KiB
Lua
Raw Normal View History

--basic grid based collision detection
function collision(oldposx,oldposy)
2017-06-20 04:16:34 -04:00
--stairs
if (player.playerx <= mapwidth and player.playerx > 1) and
(player.playery < mapheight and player.playery > 1) and
2017-06-28 02:21:20 -04:00
tiles[player.playerx][player.playery]["block"] == 3 then
2017-06-20 04:16:34 -04:00
player.playery = player.playery - 1
2017-06-28 02:21:20 -04:00
elseif (player.playerx > mapwidth or player.playerx <= 0) or (player.playery > mapheight or player.playery <= 0) or tiles[player.playerx][player.playery]["block"] ~= 1 then
player.playerx,player.playery = oldposx,oldposy
2017-06-20 03:49:11 -04:00
--can't move
oof:setPitch(love.math.random(65,100)/100)
oof:stop()
oof:play()
2017-06-20 03:44:53 -04:00
return(true)
end
end
2017-06-20 03:39:40 -04:00
--make the player fall when in air
gravtimer = 0
function gravity(dt)
2017-06-22 03:38:12 -04:00
--don't apply gravity if at bottom
if player.playery == mapheight then
player.playery = player.playery + 1
maplib.new_block()
return
end
2017-06-28 02:21:55 -04:00
if tiles[player.playerx][player.playery+1]["block"] == 1 then
2017-06-20 03:39:40 -04:00
gravtimer = gravtimer + dt
2017-06-20 04:16:34 -04:00
if gravtimer >= 0.2 then
2017-06-20 03:39:40 -04:00
local oldposx,oldposy = player.playerx,player.playery
player.playery = player.playery + 1
collision(oldposx,oldposy)
gravtimer = 0
end
else
gravtimer = 0
end
end