diff --git a/entity.lua b/entity.lua index bfe5a7e..9ed5580 100644 --- a/entity.lua +++ b/entity.lua @@ -10,26 +10,225 @@ entity_count = 0 --create entities -function entity.create_entity(type,sizex,sizey,texture,chunkx,chunky,posx,posy) +function entity.create_entity(type,sizex,sizey,texture,chunkx,chunky,posx,posy,inertiax,inertiay,item) entity_count = entity_count + 1 entity_table[entity_count] = {} entity_table[entity_count]["sizex"] = sizex entity_table[entity_count]["sizey"] = sizey - entity_table[entity_count]["texture"] = texture entity_table[entity_count]["chunkx"] = chunkx entity_table[entity_count]["chunky"] = chunky entity_table[entity_count]["posx"] = posx entity_table[entity_count]["posy"] = posy + entity_table[entity_count]["inertiax"] = inertiax + entity_table[entity_count]["inertiay"] = inertiay + entity_table[entity_count]["on_block"] = false + entity_table[entity_count]["entity_in_unloaded_chunk"] = false + entity_table[entity_count]["item"] = item + + + if item then + entity_table[entity_count]["texture"] = texture_table[item] + else + entity_table[entity_count]["texture"] = texture + end + + end --draw entities function entity.render_entity() if entity_count > 0 then for i = 1,entity_count do - local drawx = player_drawnx+(entity_table[i]["posx"]*scale)+(map_max*scale*(entity_table[i]["chunkx"]-chunkx))-(player.playerx*scale) - local drawy = player_drawny+(entity_table[i]["posy"]*scale)+(map_max*scale*(chunky-entity_table[i]["chunky"]))-(player.playery*scale) - love.graphics.draw(entity_table[i]["texture"], drawx,drawy,0, scale/16*entity_table[i]["sizex"], scale/16*entity_table[i]["sizey"],scale*(entity_table[i]["sizex"]*4)/16,scale*(entity_table[i]["sizey"]*4)/16) - + local drawx = (player_drawnx+(entity_table[i]["posx"]*scale)+(map_max*scale*(entity_table[i]["chunkx"]-chunkx))-(player.playerx*scale))-((entity_table[i]["sizex"]/2)*scale) + local drawy = (player_drawny+(entity_table[i]["posy"]*scale)+(map_max*scale*(chunky-entity_table[i]["chunky"]))-(player.playery*scale))-((entity_table[i]["sizey"]/2)*scale) + love.graphics.draw(entity_table[i]["texture"], drawx,drawy,0, scale*entity_table[i]["sizex"]/16, scale*entity_table[i]["sizey"]/16)--,scale*(entity_table[i]["sizex"]),scale*(entity_table[i]["sizey"])) + --love.graphics.circle( "fill", drawx+((entity_table[i]["sizex"]/2)*scale), drawy+((entity_table[i]["sizey"]/2)*scale), 3 ) end end end + + +--entity gravity +function entity.gravity() + if entity_count > 0 then + for i = 1,entity_count do + if entity_table[i]["entity_in_unloaded_chunk"] == false then + --print(entity_table[i]["on_block"]) + if entity_table[i]["on_block"] == true then + entity_table[i]["inertiay"] = 0 + --lastheight = math.floor(((map_max-player.playery)+(chunky*map_max))) + --print(lastheight) + --player.on_block = false + else + if entity_table[i]["inertiay"] < 0.3 then + entity_table[i]["inertiay"] = entity_table[i]["inertiay"] + 0.01 + --print(entity_table[i]["inertiay"]) + end + end + else + print("entity "..i.." in unloaded chunk") + entity_table[i]["inertiax"] = 0 + entity_table[i]["inertiay"] = 0 + end + end + end +end + +--applying the entity physics +function entity.physics_apply(dt) + --local oldposx,oldposy = player.playerx,player.playery + + + if entity_count > 0 then + for i = 1,entity_count do + + + entity.collision(i,entity_table[i].posx,entity_table[i].posy) + + --collisionx(oldposx) + --print(entity_table[i].inertiax) + if math.abs(entity_table[i].inertiax) <= 0.005 then + entity_table[i].inertiax = 0 + elseif entity_table[i].inertiax < 0 then + entity_table[i].inertiax = entity_table[i].inertiax + 0.005 + elseif entity_table[i].inertiax > 0 then + entity_table[i].inertiax = entity_table[i].inertiax - 0.005 + else + entity_table[i].inertiax = entity_table[i].inertiax + end + end + end + +end + + + +-- +function entity.collision(i,oldposx,oldposy) + + entity_table[i]["entity_in_unloaded_chunk"] = false + + + + local xer = {-entity_table[i]["sizex"]/4,entity_table[i]["sizex"]/4} + local yer = {-entity_table[i]["sizey"]/4,entity_table[i]["sizey"]/4} + local fall = true + + --check the corners (y) + entity_table[i]["posy"] = entity_table[i]["posy"] + entity_table[i]["inertiay"] + for q = 1,2 do + for r = 1,2 do + local squarex = math.floor(entity_table[i]["posx"]+xer[q]) + local squarey = math.floor(entity_table[i]["posy"]+yer[r]) + + --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 + --print(chunkerx, chunkery, "|", squarex,squarey) + --print( loaded_chunks[chunkerx][chunkery][squarex][squarey]["block"]) + --if (squarex1 > map_max or squarex1 <= 0) or (squarey1 > map_max or squarey1 <= 0) or blocks[loaded_chunks[0][0][squarex1][squarey1]["block"]]["collide"] ~= false then + local entity_chunkx = entity_table[i]["chunkx"] + local entity_chunky = entity_table[i]["chunky"] + + + if loaded_chunks[entity_chunkx+chunkerx] and loaded_chunks[entity_chunkx+chunkerx][chunkery+entity_chunky] and loaded_chunks[entity_chunkx+chunkerx][chunkery+entity_chunky][squarex] and loaded_chunks[entity_chunkx+chunkerx][chunkery+entity_chunky][squarex][squarey] then + if blocks[loaded_chunks[entity_chunkx+chunkerx][chunkery+entity_chunky][squarex][squarey]["block"]]["collide"] ~= false then + + ---print("PUTTING BACK TO OLD") + entity_table[i]["posy"] = oldposy + + if r == 2 then + entity_table[i]["on_block"] = true + --print("IT'S ON THE BLOCK") + fall = false + end + if r == 1 then + entity_table[i]["on_block"] = false + end + end + else + entity_table[i]["entity_in_unloaded_chunk"] = true + end + end + end + if fall == true then + entity_table[i]["on_block"] = false + end + + + --check the corners(x) + entity_table[i]["posx"] = entity_table[i]["posx"] + entity_table[i]["inertiax"] + for q = 1,2 do + for r = 1,3 do + local squarex = math.floor(entity_table[i]["posx"]+xer[q]) + + local squarey + + if r < 3 then + squarey = math.floor(entity_table[i]["posy"]+yer[r]) + + else + squarey = math.floor(entity_table[i]["posy"]) + end + + + --print(squarex) + --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 + local entity_chunkx = entity_table[i]["chunkx"] + local entity_chunky = entity_table[i]["chunky"] + + + --if (squarex1 > map_max or squarex1 <= 0) or (squarey1 > map_max or squarey1 <= 0) or blocks[loaded_chunks[0][0][squarex1][squarey1]["block"]]["collide"] ~= false then + if loaded_chunks[entity_chunkx+chunkerx] and loaded_chunks[entity_chunkx+chunkerx][chunkery+entity_chunky] and loaded_chunks[entity_chunkx+chunkerx][chunkery+entity_chunky][squarex] and loaded_chunks[entity_chunkx+chunkerx][chunkery+entity_chunky][squarex][squarey] then + if blocks[loaded_chunks[entity_chunkx+chunkerx][chunkery+entity_chunky][squarex][squarey]["block"]]["collide"] ~= false then + entity_table[i]["inertiax"] = 0 + entity_table[i]["posx"] = oldposx + --print("stopping x inertia and pos") + end + else + entity_table[i]["entity_in_unloaded_chunk"] = true + end + end + end +end + diff --git a/main.lua b/main.lua index d8033e1..a12b71d 100644 --- a/main.lua +++ b/main.lua @@ -117,6 +117,11 @@ function love.update(dt) print("clear") end - maplib.new_block(player.playerx,player.playery) + entity.gravity() + + entity.physics_apply(dt) + + + maplib.new_block(player.playerx,player.playery) end diff --git a/player.lua b/player.lua index b51a079..8e8c6cb 100644 --- a/player.lua +++ b/player.lua @@ -125,7 +125,7 @@ function love.keypressed( key, scancode, isrepeat ) end if key == "n" then - entity.create_entity("item",0.2,0.2,texture_table[2],chunkx,chunky,player.playerx,player.playery) + entity.create_entity("item",0.4,0.4,texture_table[2],chunkx,chunky,player.playerx,player.playery,0,0,nil) end @@ -199,6 +199,9 @@ function mine(key,dt) inventory_add(loaded_chunks[selected_chunkx][selected_chunky][mx][my]["block"]) + print(math.random(10,50)/1000) + entity.create_entity("item",0.4,0.4,nil,selected_chunkx,selected_chunky,mx,my,math.random(-30,30)/1000,math.random(10,50)/1000,loaded_chunks[selected_chunkx][selected_chunky][mx][my]["block"]) + loaded_chunks[selected_chunkx][selected_chunky][mx][my]["block"] = 1 --love.filesystem.write( "/map/"..chunkx+selected_chunkx.."_"..chunky+selected_chunky..".txt", TSerial.pack(loaded_chunks[selected_chunkx][selected_chunky]))