feat(world): make object creation more customizable by worlds

This commit is contained in:
Kazhnuz 2019-06-28 10:06:38 +02:00
parent 369e0ceec0
commit e6c2af5429
3 changed files with 79 additions and 6 deletions

View file

@ -31,7 +31,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **actor:** Rename all function related to YGravity to just \*Gravity
- **world** Separate "queryRect()" into two functions
- **world:** Separate "queryRect()" into two functions
- **world:** Make object creation more customizable by worlds
### Fixed

View file

@ -268,7 +268,7 @@ function BaseWorld:loadMapCollisions()
if self:isCollisionIndexed(objectlayer.name) then
print("DEBUG: loading actors in " .. objectlayer.name .. " collision layer")
for k, object in pairs(objectlayer.objects) do
self:newCollision(objectlayer.name, object.x, object.y, object.width, object.height)
self:newCollisionFromMap(objectlayer, object)
end
self.map:removeLayer(objectlayer.name)
end
@ -281,9 +281,9 @@ function BaseWorld:loadMapActors()
print("DEBUG: loading actors in " .. objectlayer.name .. " actor layer")
for k, object in pairs(objectlayer.objects) do
if (object.properties.batchActor) then
self:batchActor(objectlayer.name, object)
self:batchActor(objectlayer, object)
else
self:newActor(objectlayer.name, object.x, object.y)
self:newActorFromMap(objectlayer, object)
end
end
self.map:removeLayer(objectlayer.name)
@ -291,7 +291,8 @@ function BaseWorld:loadMapActors()
end
end
function BaseWorld:batchActor(name, object)
function BaseWorld: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
@ -309,6 +310,18 @@ function BaseWorld:batchActor(name, object)
end
end
function BaseWorld:newActorFromMap(objectlayer, object)
self:newActor(objectlayer.name, object.x, object.y)
end
function BaseWorld:newCollisionFromMap(objectlayer, object)
self:newCollision(objectlayer.name, object.x, object.y, object.width, object.height)
end
function BaseWorld:addPlayerFromMap(object, i)
self:addPlayer(self.obj.Player(self, object.x, object.y), i, true)
end
function BaseWorld:loadMapPlayers()
for k, objectlayer in pairs(self.map.layers) do
if (objectlayer.name == "player") then
@ -317,7 +330,7 @@ function BaseWorld:loadMapPlayers()
for k, object in pairs(objectlayer.objects) do
if (i <= self.playerNumber) then
-- TODO: don't hardcode camera handling
self:addPlayer(self.obj.Player(self, object.x, object.y), i, true)
self:addPlayerFromMap(object, i)
end
i = i + 1
end

View file

@ -43,6 +43,14 @@ function World2D:initActors()
self.bodies = Bump.newWorld(50)
end
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
function World2D:registerActor(actor)
World2D.super.registerActor(self, actor)
end
@ -64,6 +72,56 @@ function World2D:getActorsInRect(x, y, w, h)
return returnquery
end
-- 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)
if (haveCam) then
local xx, yy = player.actor:getViewCenter()
self.cameras:addView(xx, yy, player.actor)
end
end
-- MAP LOADING FUNCTIONS
-- Handle loading of actors from map
function BaseWorld: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 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
function BaseWorld:newActorFromMap(objectlayer, object)
self:newActor(objectlayer.name, object.x, object.y)
end
function BaseWorld:newCollisionFromMap(objectlayer, object)
self:newCollision(objectlayer.name, object.x, object.y, object.width, object.height)
end
function BaseWorld:addPlayerFromMap(object, i)
self:addPlayer(self.obj.Player(self, object.x, object.y), i, true)
end
-- BODIES MANAGEMENT FUNCTIONS
-- Basic function to handle bodies. Wrappers around Bump2D functions