proper player and general collision disposal for gc

This commit is contained in:
yuki 2025-11-11 04:51:36 -03:00
parent 5cbd3385c8
commit 9f389e5232
3 changed files with 15 additions and 2 deletions

View file

@ -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

View file

@ -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

View file

@ -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()