From 6b48551d59818b385200221f0a42afea0a3545d5 Mon Sep 17 00:00:00 2001 From: Eltrac Date: Wed, 12 Mar 2025 17:55:00 +0800 Subject: fix: game crash when creating new body during world update use a body lifecycle manager to create after world update is done --- objects/Loot.lua | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'objects') diff --git a/objects/Loot.lua b/objects/Loot.lua index c8cd51f..9c272a1 100644 --- a/objects/Loot.lua +++ b/objects/Loot.lua @@ -1,4 +1,5 @@ local Body = require "objects.Body" +local physics = require "logic.physics" local Loot = {} Loot.__index = Loot @@ -12,16 +13,22 @@ function Loot:new(name, x, y) } setmetatable(obj, Loot) + G.BodyLifeCycleManager:create(obj, config.lootSize, "static") return obj end -function Loot:update(dt) +function Loot:onContact(other) local loots = G.currentRoom.objects.loots - if self.body:collide(G.player.body, self.x, self.y, G.player.x, G.player.y) then + if other.objectType == "player" then local pickedLoot = table.remove(loots, utils.indexof(loots, self)) table.insert(G.player.hearts, pickedLoot.type) + self.physicsBody:destroy() end end +function Loot:update(dt) + -- nothing yet +end + return Loot -- cgit v1.2.3