From d0c372b1e5d195c0eeeffe05f16930c53adb6818 Mon Sep 17 00:00:00 2001 From: Eltrac Date: Thu, 13 Mar 2025 14:23:47 +0800 Subject: refactor: clearer code --- objects/Obstacle.lua | 1 + objects/Shot.lua | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) (limited to 'objects') diff --git a/objects/Obstacle.lua b/objects/Obstacle.lua index 69438f0..ef83a5b 100644 --- a/objects/Obstacle.lua +++ b/objects/Obstacle.lua @@ -6,6 +6,7 @@ Obstacle.__index = Obstacle function Obstacle:new(name, location, body) local obj = { + objectType = "obstacle", name = name, presetLocation = (type(location) == "string" and location) or nil, x = (type(location) == "table" and location.x) or nil, diff --git a/objects/Shot.lua b/objects/Shot.lua index 67daa5f..90cb54b 100644 --- a/objects/Shot.lua +++ b/objects/Shot.lua @@ -39,21 +39,28 @@ function Shot:die() end function Shot:onContact(other, contact) + local sort = other.objectType + local dieOnThisContact = true + -- when contact with enemy, deal damage - if other.objectType == "enemy" then + if sort == "enemy" then + local data = bulletData[self.type] other:takeDamage(self.damage) other:onHit(self) - if bulletData[self.type] and bulletData[self.type].afterHit then - bulletData[self.type].afterHit(other.x, other.y, other.type) + if data and data.afterHit then + data.afterHit(other.x, other.y, other.type) end end - if other.objectType == "player" then + if sort == "player" then if not self.friendly then G.player:takeDamage(self.damage) - self:die() + else + dieOnThisContact = false end - else + end + + if dieOnThisContact then self:die() end end -- cgit v1.2.3