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

87 lines
1.7 KiB
Lua

local BaseWorld = Object:extend()
-- INIT FUNCTIONS
-- All functions to init the world and the map
function BaseWorld:new(scene, actorlist)
self.scene = scene
self:setActorList(actorlist)
self.actors = {}
end
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
-- ACTOR MANAGEMENT FUNCTIONS
-- Basic function to handle actors
function BaseWorld:newActor(name, x, y)
self.obj.index[name](self, x, y)
end
function BaseWorld:registerActor(actor)
table.insert(self.actors, actor)
end
function BaseWorld:removeActor(actor)
for i,v in ipairs(self.actors) do
if v == actor then
table.remove(self.actors, i)
end
end
end
function BaseWorld: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 BaseWorld: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
function BaseWorld:countActors()
return #self.actors
end
function BaseWorld:getActors()
return self.actors
end
-- UPDATE FUNCTIONS
-- All update functions
function BaseWorld:update(dt)
for i,v in ipairs(self.actors) do
v:update(dt)
end
end
-- DRAW FUNCTIONS
-- All function to draw the map, world and actors
function BaseWorld:draw(dt)
for i,v in ipairs(self.actors) do
v:draw(dt)
end
end
return BaseWorld