summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEltrac <hi@guhub.cn>2025-03-15 22:04:38 +0800
committerEltrac <hi@guhub.cn>2025-03-15 22:04:38 +0800
commit47927bad0624445f9c10722e8daee962ebf489d9 (patch)
tree8c23381687292aee615010e5976a54505522f306
parentea925c7616524609d19ae528ea553d7509741b7c (diff)
fix: unexpected body retention and related issues
-rw-r--r--logic/map.lua2
-rw-r--r--objects/Door.lua2
-rw-r--r--objects/Shot.lua1
-rw-r--r--objects/room.lua16
4 files changed, 17 insertions, 4 deletions
diff --git a/logic/map.lua b/logic/map.lua
index 1a546be..01091eb 100644
--- a/logic/map.lua
+++ b/logic/map.lua
@@ -11,7 +11,7 @@ local map = {}
function map.switchRoom(destination)
-- unload current room
- G.currentRoom:removeAllBodies()
+ G.currentRoom:unload()
-- load new room
destination:init()
diff --git a/objects/Door.lua b/objects/Door.lua
index 04a934b..343b2c3 100644
--- a/objects/Door.lua
+++ b/objects/Door.lua
@@ -22,6 +22,7 @@ end
function Door:new(location, within, to)
local obj = {
+ objectType = "door",
location = location,
size = config.graphics.doorSize,
thickness = config.graphics.doorThickness,
@@ -33,7 +34,6 @@ function Door:new(location, within, to)
setmetatable(obj, Door)
calculateDoorLocation(obj)
- G.BodyLifeCycleManager:create(obj, { obj.w, obj.h }, "static")
return obj
end
diff --git a/objects/Shot.lua b/objects/Shot.lua
index 6c7f7fb..80858c5 100644
--- a/objects/Shot.lua
+++ b/objects/Shot.lua
@@ -15,6 +15,7 @@ function Shot:new(bulletType, target, firer, friendly)
local fireY = firer.y + sin * offset
local obj = {
+ objectType = "shot",
type = bulletType,
x = fireX, y = fireY,
speed = { x = cos*config.bullet.speed, y = sin*config.bullet.speed },
diff --git a/objects/room.lua b/objects/room.lua
index 401dff1..6a84404 100644
--- a/objects/room.lua
+++ b/objects/room.lua
@@ -236,6 +236,10 @@ function Room:init()
end
end
+ for _, door in pairs(self.doors) do
+ G.BodyLifeCycleManager:create(door, { door.w, door.h }, "static")
+ end
+
self.walls = self:addWalls()
-- play music
@@ -250,15 +254,23 @@ function Room:init()
end
end
-function Room:removeAllBodies()
+function Room:unload()
for _, group in pairs(self.objects) do
for _, obj in ipairs(group) do
G.BodyLifeCycleManager:destroy(obj.physicsBody)
end
+ group = {}
end
+
for _, wall in ipairs(self.walls) do
- G.BodyLifeCycleManager:destroy(wall.body)
+ G.BodyLifeCycleManager:destroy(wall.physicsBody)
end
+ self.walls = {}
+
+ for _, door in pairs(self.doors) do
+ G.BodyLifeCycleManager:destroy(door.physicsBody)
+ end
+
self.objects.shots = {}
end