2019-04-01 08:08:54 +02:00
|
|
|
local BaseWorld = Object:extend()
|
|
|
|
|
2019-04-07 13:22:23 +02:00
|
|
|
-- INIT FUNCTIONS
|
|
|
|
-- All functions to init the world and the map
|
|
|
|
|
2019-04-07 13:35:49 +02:00
|
|
|
function BaseWorld:new(scene, actorlist)
|
2019-04-01 08:08:54 +02:00
|
|
|
self.scene = scene
|
2019-04-07 13:35:49 +02:00
|
|
|
self.setActorList(actorlist)
|
2019-04-01 13:09:21 +02:00
|
|
|
self.actors = {}
|
2019-04-01 08:08:54 +02:00
|
|
|
end
|
|
|
|
|
2019-04-07 13:35:49 +02:00
|
|
|
function BaseWorld:setActorList(actorlist)
|
|
|
|
if actorlist == nil then
|
|
|
|
error("FATAL: You must set an actorlist to your world")
|
|
|
|
else
|
|
|
|
self.obj = require(actorlist)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-07 13:22:23 +02:00
|
|
|
-- ACTOR MANAGEMENT FUNCTIONS
|
|
|
|
-- Basic function to handle actors
|
|
|
|
|
2019-04-01 13:09:21 +02:00
|
|
|
function BaseWorld:registerActor(actor)
|
|
|
|
table.insert(self.actors, actor)
|
2019-04-01 08:08:54 +02:00
|
|
|
end
|
|
|
|
|
2019-04-07 13:31:25 +02:00
|
|
|
function World:removeActor(actor)
|
|
|
|
for i,v in ipairs(self.actors) do
|
|
|
|
if v == actor then
|
|
|
|
table.remove(self.actors, i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function World:moveActor(actor, x, y, filter)
|
|
|
|
self.actors[actor].x = x
|
|
|
|
self.actors[actor].y = y
|
|
|
|
-- as the baseworld have no collision function, we return empty collision
|
|
|
|
-- datas, but from the same type than bump2D will return
|
|
|
|
return x, y, {}, 0
|
|
|
|
end
|
|
|
|
|
|
|
|
function World:queryRect(x, y, w, h)
|
|
|
|
local query = {}
|
|
|
|
local x2, y2 = x + w, y + h
|
|
|
|
for i,v in ipairs(self.actors) do
|
|
|
|
if (v.x >= x) and (v.x + v.w >= x1) and
|
|
|
|
(v.y >= y) and (v.y + v.h >= y1) then
|
|
|
|
|
|
|
|
table.insert(query, v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return v
|
|
|
|
end
|
|
|
|
|
2019-04-01 13:09:21 +02:00
|
|
|
function BaseWorld:countActors()
|
|
|
|
return #self.actors
|
2019-04-01 12:47:08 +02:00
|
|
|
end
|
|
|
|
|
2019-04-07 13:31:25 +02:00
|
|
|
function World:getActors()
|
|
|
|
return self.actors
|
|
|
|
end
|
|
|
|
|
2019-04-07 13:22:23 +02:00
|
|
|
-- UPDATE FUNCTIONS
|
|
|
|
-- All update functions
|
|
|
|
|
2019-04-01 08:08:54 +02:00
|
|
|
function BaseWorld:update(dt)
|
2019-04-01 13:09:21 +02:00
|
|
|
for i,v in ipairs(self.actors) do
|
2019-04-01 08:08:54 +02:00
|
|
|
v:update(dt)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-07 13:22:23 +02:00
|
|
|
-- DRAW FUNCTIONS
|
|
|
|
-- All function to draw the map, world and actors
|
|
|
|
|
2019-04-01 08:08:54 +02:00
|
|
|
function BaseWorld:draw(dt)
|
2019-04-01 13:09:21 +02:00
|
|
|
for i,v in ipairs(self.actors) do
|
2019-04-01 08:08:54 +02:00
|
|
|
v:draw(dt)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return BaseWorld
|