2019-04-20 22:51:56 +02:00
|
|
|
-- world2D.lua :: a basic 2D world based on bump2D.
|
|
|
|
|
|
|
|
--[[
|
|
|
|
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.
|
|
|
|
]]
|
|
|
|
|
2019-04-22 08:35:07 +02:00
|
|
|
local cwd = (...):gsub('%.world2D$', '') .. "."
|
2019-04-20 22:51:56 +02:00
|
|
|
|
|
|
|
local BaseWorld = require(cwd .. "baseworld")
|
|
|
|
local World2D = BaseWorld:extend()
|
|
|
|
|
|
|
|
local Sti = require(cwd .. "libs.sti")
|
|
|
|
local Bump = require(cwd .. "libs.bump")
|
2019-04-29 11:21:41 +02:00
|
|
|
local CameraSystem = require(cwd .. "camera")
|
2019-04-20 22:51:56 +02:00
|
|
|
|
|
|
|
function World2D:new(scene, actorlist, mapfile)
|
2019-05-01 16:25:44 +02:00
|
|
|
World2D.super.new(self, scene, actorlist, mapfile)
|
2019-04-20 22:51:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- ACTORS FUNCTIONS
|
2019-06-22 19:27:31 +02:00
|
|
|
-- Add support for bodies in Actor functions
|
2019-04-20 22:51:56 +02:00
|
|
|
|
2019-05-01 16:25:44 +02:00
|
|
|
function World2D:initActors()
|
2019-06-16 10:08:59 +02:00
|
|
|
self.currentCreationID = 0
|
2019-06-22 17:19:54 +02:00
|
|
|
self.actors = {}
|
|
|
|
self.bodies = Bump.newWorld(50)
|
2019-05-01 16:25:44 +02:00
|
|
|
end
|
|
|
|
|
2019-06-28 10:06:38 +02:00
|
|
|
function World2D:newActor(name, x, y)
|
|
|
|
self.obj.index[name](self, x, y)
|
|
|
|
end
|
|
|
|
|
|
|
|
function World2D:newCollision(name, x, y, w, h)
|
|
|
|
self.obj.collisions[name](self, x, y, w, h)
|
|
|
|
end
|
|
|
|
|
2019-04-20 22:51:56 +02:00
|
|
|
function World2D:registerActor(actor)
|
2019-06-22 17:19:54 +02:00
|
|
|
World2D.super.registerActor(self, actor)
|
2019-04-20 22:51:56 +02:00
|
|
|
end
|
|
|
|
|
2019-06-27 21:22:33 +02:00
|
|
|
function World2D:moveActor(actor, x, y, filter)
|
|
|
|
return self.bodies:move(actor.mainHitbox, x, y, filter)
|
2019-06-22 19:27:31 +02:00
|
|
|
end
|
|
|
|
|
2019-06-27 21:20:54 +02:00
|
|
|
function World2D:getActorsInRect(x, y, w, h)
|
|
|
|
local bodies = self.bodies:queryRect(x, y, w, h)
|
|
|
|
local returnquery = {}
|
|
|
|
|
|
|
|
for i,body in ipairs(bodies) do
|
|
|
|
if (body.isMainHitBox) then
|
|
|
|
table.insert(returnquery, body.owner)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return returnquery
|
|
|
|
end
|
|
|
|
|
2019-06-28 10:06:38 +02:00
|
|
|
-- PLAYER FUNCTIONS
|
|
|
|
-- Load player stuff
|
|
|
|
|
|
|
|
function World2D:addPlayer(actor, sourceid, haveCam)
|
|
|
|
local player = {}
|
|
|
|
player.actor = actor
|
|
|
|
player.sourceid = sourceid or 1
|
|
|
|
|
|
|
|
table.insert(self.players, player)
|
|
|
|
|
2019-06-29 11:25:40 +02:00
|
|
|
self.cameras:addTarget(player.actor)
|
2019-06-28 10:06:38 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- MAP LOADING FUNCTIONS
|
|
|
|
-- Handle loading of actors from map
|
|
|
|
|
2019-06-28 11:54:34 +02:00
|
|
|
function World2D:batchActor(objectlayer, object)
|
2019-06-28 10:06:38 +02:00
|
|
|
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 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)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-28 11:54:34 +02:00
|
|
|
function World2D:newActorFromMap(objectlayer, object)
|
2019-06-28 10:06:38 +02:00
|
|
|
self:newActor(objectlayer.name, object.x, object.y)
|
|
|
|
end
|
|
|
|
|
2019-06-28 11:54:34 +02:00
|
|
|
function World2D:newCollisionFromMap(objectlayer, object)
|
2019-06-28 10:06:38 +02:00
|
|
|
self:newCollision(objectlayer.name, object.x, object.y, object.width, object.height)
|
|
|
|
end
|
|
|
|
|
2019-06-28 11:54:34 +02:00
|
|
|
function World2D:addPlayerFromMap(object, i)
|
2019-06-29 11:25:40 +02:00
|
|
|
self:addPlayer(self.obj.Player(self, object.x, object.y), i)
|
2019-06-28 10:06:38 +02:00
|
|
|
end
|
|
|
|
|
2019-06-27 21:22:33 +02:00
|
|
|
-- BODIES MANAGEMENT FUNCTIONS
|
|
|
|
-- Basic function to handle bodies. Wrappers around Bump2D functions
|
|
|
|
|
|
|
|
function World2D:registerBody(body)
|
|
|
|
return self.bodies:add(body, body.x, body.y, body.w, body.h)
|
|
|
|
end
|
2019-06-22 19:27:31 +02:00
|
|
|
|
|
|
|
function World2D:updateBody(body)
|
|
|
|
return self.bodies:update(body, body.x, body.y, body.w, body.h)
|
|
|
|
end
|
|
|
|
|
|
|
|
function World2D:removeBody(body)
|
|
|
|
return self.bodies:remove(body)
|
2019-04-20 22:51:56 +02:00
|
|
|
end
|
|
|
|
|
2019-06-27 21:22:33 +02:00
|
|
|
function World2D:checkCollision(body, x, y, filter)
|
|
|
|
return self.bodies:check(body, x, y, filter)
|
2019-05-05 13:01:00 +02:00
|
|
|
end
|
|
|
|
|
2019-06-27 21:20:54 +02:00
|
|
|
function World2D:getBodiesInRect(x, y, w, h)
|
2019-06-22 17:19:54 +02:00
|
|
|
return self.bodies:queryRect(x, y, w, h)
|
2019-04-20 22:51:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return World2D
|