epervier-old/gamecore/modules/world/world3D.lua

203 lines
5.5 KiB
Lua
Raw Normal View History

-- world3D.lua :: a basic fake3D world based on bump-2dpd.
--[[
Copyright © 2019 Kazhnuz
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]
local cwd = (...):gsub('%.world3D$', '') .. "."
local BaseWorld = require(cwd .. "baseworld")
local World3D = BaseWorld:extend()
local Sti = require(cwd .. "libs.sti")
local Bump = require(cwd .. "libs.bump")
local Bump3D = require(cwd .. "libs.bump-3dpd")
local CameraSystem = require(cwd .. "camera")
local PADDING_VALUE = 10/100
function World3D:new(scene, actorlist, mapfile)
World3D.super.new(self, scene, actorlist, mapfile)
end
-- ACTORS FUNCTIONS
-- Add support for bodies in Actor functions
function World3D:initActors()
self.currentCreationID = 0
self.actors = {}
self.bodies = Bump3D.newWorld(50)
self:initShapes()
end
function World3D:newActor(name, x, y, z)
self.obj.index[name](self, x, y, z)
end
function World3D:newCollision(name, x, y, z, w, h, d)
self.obj.collisions[name](self, x, y, z, w, h, d)
end
function World3D:moveActor(actor, x, y, z, filter)
return self.bodies:move(actor.mainHitbox, x, y, z, filter)
end
function World3D:getActorsInRect(x, y, w, h)
return self:getShapeInRect(x, y, w, h)
end
function BaseWorld:getVisibleActors(id)
local actors = {}
if (id ~= nil) then
local camx, camy, camw, camh = self.cameras:getViewCoordinate(id)
local paddingw = camw * PADDING_VALUE
local paddingh = camh * PADDING_VALUE
local x = camx - paddingw
local y = camy - paddingh
local w = camw + paddingw * 2
local h = camh + paddingh * 2
actors = self:getActorsInRect(x, y, w, h)
else
actors = self:getActors()
end
table.sort(actors, function(a,b)
if (a.y == b.y) then
if (a.depth == b.depth) then
return a.creationID < b.creationID
else
return a.depth > b.depth
end
else
return a.y < b.y
end
end)
return actors
end
-- PLAYER FUNCTIONS
-- Load player stuff
function World3D:addPlayer(actor, sourceid, haveCam)
local player = {}
player.actor = actor
player.sourceid = sourceid or 1
table.insert(self.players, player)
self.cameras:addTarget(player.actor)
end
-- MAP LOADING FUNCTIONS
-- Handle loading of actors from map
function World3D:batchActor(objectlayer, object)
local name = objectlayer.name
local gwidth = object.properties.gwidth or self.map.tilewidth
local gheight = object.properties.gheight or self.map.tileheight
local x = object.x
local y = object.y
local z = object.properties.z or 0
local w = object.width
local h = object.height
local cellHor = math.ceil(w / gwidth)
local cellVert = math.ceil(h / gheight)
for i=1, cellHor do
for j=1, cellVert do
self:newActor(name, x + (i-1)*gwidth, y + (j-1)*gheight, z)
end
end
end
function World3D:newActorFromMap(objectlayer, object)
local z = object.properties.z or 0
self:newActor(objectlayer.name, object.x, object.y, z)
end
function World3D:newCollisionFromMap(objectlayer, object)
local z = object.properties.z or 0
local d = object.properties.d or 16
self:newCollision(objectlayer.name, object.x, object.y, z, object.width, object.height, d)
end
function World3D:addPlayerFromMap(object, i)
local z = object.properties.z or 0
self:addPlayer(self.obj.Player(self, object.x, object.y, z), i)
end
-- BODIES MANAGEMENT FUNCTIONS
-- Basic function to handle bodies. Wrappers around Bump2D functions
function World3D:registerBody(body)
return self.bodies:add(body, body.x, body.y, body.z, body.w, body.h, body.d)
end
function World3D:updateBody(body)
return self.bodies:update(body, body.x, body.y, body.z, body.w, body.h, body.d)
end
function World3D:removeBody(body)
return self.bodies:remove(body)
end
function World3D:checkCollision(body, x, y, z, filter)
return self.bodies:check(body, x, y, z, filter)
end
function World3D:getBodiesInRect(x, y, w, h)
return {} --self.bodies:queryRect(x, y, w, h)
end
-- SHAPE SYSTEM
-- Handle onscreen shapes
function World3D:initShapes()
self.shapes = Bump.newWorld(50)
end
function World3D:registerShape(actor)
local x, y, w, h = actor:getShape()
return self.shapes:add(actor, x, y, w, h)
end
function World3D:updateShape(actor)
local x, y, w, h = actor:getShape()
return self.shapes:update(actor, x, y, w, h)
end
function World3D:removeShape(actor)
return self.shapes:remove(actor)
end
function World3D:checkShapeIntersection(actor, x, y)
return self.shapes:check(actor, x, y)
end
function World3D:getShapeInRect(x, y, w, h)
return self.shapes:queryRect(x, y, w, h)
end
return World3D