Open-Terrarium/collision.lua

156 lines
4.0 KiB
Lua
Raw Normal View History

--basic grid based collision detection
2017-07-10 04:41:19 -04:00
2017-07-09 22:23:40 -04:00
--squared collision detection
2017-07-10 00:34:00 -04:00
player.on_block = false
function collision(oldposx,oldposy)
--do stairs
2017-07-12 02:14:30 -04:00
local xer = {-0.2,0.2}
2017-07-09 22:43:58 -04:00
local yer = {0,1}
2017-07-10 00:34:00 -04:00
local fall = true
--check the corners (y)
player.playery = player.playery + player.inertiay
2017-07-09 22:23:40 -04:00
for q = 1,2 do
for r = 1,2 do
2017-07-12 02:14:30 -04:00
local squarex = math.floor(player.playerx+xer[q])
local squarey = math.floor(player.playery+yer[r])
--use this to detect outside chunk 00
local chunkerx = 0
local chunkery = 0
2017-07-12 02:23:42 -04:00
if squarex < 1 then
chunkerx = -1
squarex = map_max
print("adjusting x -1")
elseif squarex > map_max then
chunkerx = 1
squarex = 1
print("adjusting x +1")
2017-07-12 02:14:30 -04:00
end
2017-07-12 02:23:42 -04:00
if squarey < 1 then
chunkery = 1
squarey = map_max
print("adjusting y +1")
elseif squarey > map_max then
chunkery = -1
squarey = 1
print("adjusting y -1")
end
print(chunkerx, chunkery, "|", squarex,squarey)
print( loaded_chunks[chunkerx][chunkery][squarex][squarey]["block"])
2017-07-09 22:43:58 -04:00
--if (squarex1 > map_max or squarex1 <= 0) or (squarey1 > map_max or squarey1 <= 0) or ore[loaded_chunks[0][0][squarex1][squarey1]["block"]]["collide"] ~= false then
2017-07-12 02:23:42 -04:00
if ore[loaded_chunks[chunkerx][chunkery][squarex][squarey]["block"]]["collide"] ~= false then
2017-07-10 00:34:00 -04:00
player.playery = oldposy
if r == 2 then
player.on_block = true
fall = false
end
if r == 1 then
player.on_block = false
end
end
end
end
if fall == true then
player.on_block = false
end
--check the corners(x)
player.playerx = player.playerx + player.inertiax
2017-07-10 00:34:00 -04:00
for q = 1,2 do
for r = 1,2 do
2017-07-12 02:14:30 -04:00
local squarex = math.floor(player.playerx+xer[q])
local squarey = math.floor(player.playery+yer[r])
2017-07-12 02:23:42 -04:00
--use this to detect outside chunk 00
local chunkerx = 0
local chunkery = 0
if squarex < 1 then
chunkerx = -1
squarex = map_max
print("adjusting x -1")
elseif squarex > map_max then
chunkerx = 1
squarex = 1
print("adjusting x +1")
end
if squarey < 1 then
chunkery = 1
squarey = map_max
print("adjusting y +1")
elseif squarey > map_max then
chunkery = -1
squarey = 1
print("adjusting y -1")
end
2017-07-10 00:34:00 -04:00
--if (squarex1 > map_max or squarex1 <= 0) or (squarey1 > map_max or squarey1 <= 0) or ore[loaded_chunks[0][0][squarex1][squarey1]["block"]]["collide"] ~= false then
2017-07-12 02:23:42 -04:00
if ore[loaded_chunks[chunkerx][chunkery][squarex][squarey]["block"]]["collide"] ~= false then
2017-07-09 22:43:58 -04:00
player.inertiax = 0
2017-07-10 00:34:00 -04:00
player.playerx = oldposx
2017-07-10 04:49:16 -04:00
--print("stopping x inertia and pos")
2017-07-09 22:23:40 -04:00
end
end
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 == map_max then
-- player.playery = player.playery + 1
-- maplib.new_block()
-- return
--end
gravtimer = gravtimer + dt
2017-07-08 04:20:34 -04:00
--reverse gravity in water
if player.playery ~= 1 and ore[loaded_chunks[0][0][player.playerx][player.playery]["block"]]["float"] == true then
if gravtimer >= 0.2 then
local oldposx,oldposy = player.playerx,player.playery
player.playery = player.playery - 1
2017-07-09 22:23:40 -04:00
--collision(oldposx,oldposy)
2017-07-08 04:20:34 -04:00
gravtimer = 0
end
2017-07-08 04:24:21 -04:00
elseif player.playery == 1 and ore[loaded_chunks[0][1][player.playerx][map_max]["block"]]["float"] == true then
2017-07-08 04:20:34 -04:00
if gravtimer >= 0.2 then
player.playery = player.playery - 1
maplib.new_block()
end
--else apply normal gravity
elseif player.playery ~= map_max and ore[loaded_chunks[0][0][player.playerx][player.playery+1]["block"]]["collide"] == false then
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
2017-07-09 22:23:40 -04:00
--collision(oldposx,oldposy)
2017-06-20 03:39:40 -04:00
gravtimer = 0
end
2017-07-08 03:40:04 -04:00
elseif player.playery == map_max and ore[loaded_chunks[0][-1][player.playerx][1]["block"]]["collide"] == false then
2017-07-07 03:42:19 -04:00
--print("applying new chunk gravity")
if gravtimer >= 0.2 then
player.playery = player.playery + 1
maplib.new_block()
end
2017-06-20 03:39:40 -04:00
else
2017-07-08 03:40:04 -04:00
--print("failure")
2017-06-20 03:39:40 -04:00
gravtimer = 0
end
end