Compare commits
3 commits
5cbd3385c8
...
eb7ce44fa1
| Author | SHA1 | Date | |
|---|---|---|---|
| eb7ce44fa1 | |||
| 8ad931f33b | |||
| 9f389e5232 |
4 changed files with 30 additions and 5 deletions
25
obj/Area.lua
25
obj/Area.lua
|
|
@ -16,9 +16,14 @@ 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
|
||||
for i = #self.game_objects, 1, -1 do
|
||||
local game_object = self.game_objects[i]
|
||||
|
||||
game_object:update(dt)
|
||||
|
||||
if game_object:isDead() then
|
||||
table.remove(self.game_objects, i)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -27,6 +32,18 @@ function Area:draw()
|
|||
for _, game_object in ipairs(self.game_objects) do game_object:draw() end
|
||||
end
|
||||
|
||||
---Destroys area
|
||||
function Area:destroy()
|
||||
for i = #self.game_objects, 1, -1 do
|
||||
local game_object = self.game_objects[i]
|
||||
game_object:kill()
|
||||
table.remove(self.game_objects, i)
|
||||
end
|
||||
self.game_objects = {}
|
||||
|
||||
if self.world then self.world = nil end
|
||||
end
|
||||
|
||||
---Adds game object to area
|
||||
---@param game_object_type string name of game object's class
|
||||
---@param x number|nil horizontal position
|
||||
|
|
@ -38,9 +55,9 @@ function Area:addGameObject(game_object_type, x, y, opts)
|
|||
return game_object
|
||||
end
|
||||
|
||||
---Initializes HC physics as a world
|
||||
---Initializes HC collision manager as a world
|
||||
---@param cell_size number|nil
|
||||
function Area:addPhysicsWorld(cell_size)
|
||||
function Area:addCollisionManager(cell_size)
|
||||
self.world = HC(cell_size or 100)
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ function GameObject:new(area, x, y, opts)
|
|||
self.id = UUID()
|
||||
self.dead = false
|
||||
self.timer = Timer()
|
||||
self.collider = nil
|
||||
end
|
||||
|
||||
---Updates game object
|
||||
|
|
@ -36,6 +37,10 @@ function GameObject:destroy()
|
|||
self.timer:clear() -- cancel all tweens/after/every
|
||||
self.timer = nil
|
||||
end
|
||||
if self.collider then
|
||||
self.area.world:remove(self.collider)
|
||||
self.collider = nil
|
||||
end
|
||||
self.dead = true
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ end
|
|||
|
||||
function Player:update(dt)
|
||||
Player.super.update(self, dt)
|
||||
if self:isDead() then return end
|
||||
|
||||
-- controls
|
||||
if input:down('left') then self.r = self.r - self.rv*dt end
|
||||
|
|
@ -41,6 +42,8 @@ function Player:update(dt)
|
|||
|
||||
-- move collision area
|
||||
self.collider:moveTo(self.x, self.y)
|
||||
|
||||
if input:pressed('f2') then self:kill() end
|
||||
end
|
||||
|
||||
function Player:draw()
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ local Stage = Room:extend()
|
|||
|
||||
function Stage:new()
|
||||
Stage.super.new(self)
|
||||
self.area:addPhysicsWorld()
|
||||
self.area:addCollisionManager()
|
||||
self.area:addGameObject('Player', gw/2, gh/2)
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue