Open-Terrarium/collision.lua

47 lines
1.2 KiB
Lua
Raw Normal View History

--basic grid based collision detection
function collision(oldposx,oldposy)
2017-06-20 04:16:34 -04:00
--stairs
2017-07-05 04:50:27 -04:00
--if (player.playerx <= map_max and player.playerx > 1) and
-- (player.playery < map_max and player.playery > 1) and
-- loaded_chunks[0][0][player.playerx][player.playery]["block"] == 3 then
-- player.playery = player.playery - 1
if (player.playerx > map_max or player.playerx <= 0) or (player.playery > map_max or player.playery <= 0) or loaded_chunks[0][0][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
2017-07-04 02:22:32 -04:00
if player.playery == map_max then
2017-06-22 03:38:12 -04:00
player.playery = player.playery + 1
maplib.new_block()
return
end
if loaded_chunks[0][0][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