144 lines
4.3 KiB
Lua
144 lines
4.3 KiB
Lua
-- 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")
|
|
|
|
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)
|
|
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)
|
|
-- Just a placeholder before adding a better algorythm
|
|
World3D.super.getActorsInRect(x, y, w, h)
|
|
end
|
|
|
|
function World3D:getVisibleActors(id)
|
|
|
|
return self.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
|
|
|
|
return World3D
|