bytepath/obj/Area.lua

47 lines
1.2 KiB
Lua

---@module 'obj/Area'
---@class Area:Object
---@field room Room
---@field game_objects GameObject[]
---@field world table|nil
local Area = Object:extend()
---Instantiates area
function Area:new(room)
self.room = room
self.game_objects = {}
self.world = nil
end
---Updates area
---@param dt number
function Area:update(dt)
for i, game_object in ipairs(self.game_objects) do
if game_object:isDead() then table.remove(self.game_objects, i) end
game_object:update(dt)
end
end
---Draws area
function Area:draw()
for _, game_object in ipairs(self.game_objects) do game_object:draw() end
end
---Adds game object to area
---@param game_object_type string name of game object's class
---@param x number|nil horizontal position
---@param y number|nil vertical position
---@param opts table|nil additional arguments
function Area:addGameObject(game_object_type, x, y, opts)
local game_object = _G[game_object_type](self, x or 0, y or 0, opts or {})
table.insert(self.game_objects, game_object)
return game_object
end
---Initializes HC physics as a world
---@param cell_size number|nil
function Area:addPhysicsWorld(cell_size)
self.world = HC(cell_size or 100)
end
return Area