diff options
| author | Eltrac <hi@guhub.cn> | 2025-03-12 17:58:31 +0800 |
|---|---|---|
| committer | Eltrac <hi@guhub.cn> | 2025-03-12 17:58:31 +0800 |
| commit | 33cf1a9f7fe9a87923db11f8d1e2a7cf756a690a (patch) | |
| tree | 8f7f23eb52ca2b06797254378240bec947802c91 /utils | |
| parent | 85601ce710290a11b936e0b175a870babebcd85c (diff) | |
refactor: use lifecycle manager to destroy all bodies
Diffstat (limited to 'utils')
| -rw-r--r-- | utils/bodyLifecycleManager.lua | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/utils/bodyLifecycleManager.lua b/utils/bodyLifecycleManager.lua index 3b387b3..3bfee24 100644 --- a/utils/bodyLifecycleManager.lua +++ b/utils/bodyLifecycleManager.lua @@ -3,7 +3,8 @@ local physics = require "logic.physics" function bodyLifeCycleManager.new() local self = setmetatable({ - toCreate = {} + toCreate = {}, + toDestroy = {} }, {__index = bodyLifeCycleManager}) return self end @@ -15,6 +16,13 @@ function bodyLifeCycleManager:create(object, shape, bodyType) ) end +function bodyLifeCycleManager:destroy(object) + table.insert( + self.toDestroy, + object + ) +end + function bodyLifeCycleManager:update(dt) if self.toCreate and #self.toCreate > 0 then for _, data in ipairs(self.toCreate) do @@ -22,6 +30,13 @@ function bodyLifeCycleManager:update(dt) end self.toCreate = {} end + + if self.toDestroy and #self.toDestroy > 0 then + for _, body in ipairs(self.toDestroy) do + body:destroy() + end + self.toDestroy = {} + end end return bodyLifeCycleManager |
