summaryrefslogtreecommitdiff
path: root/objects
diff options
context:
space:
mode:
authorEltrac <hi@guhub.cn>2025-03-13 14:23:47 +0800
committerEltrac <hi@guhub.cn>2025-03-13 14:23:47 +0800
commitd0c372b1e5d195c0eeeffe05f16930c53adb6818 (patch)
tree735fe49450a85671bd3ba3adc0812e857a8d8430 /objects
parent932fbe81f9b9fdda88d97a7efbca4f7d9a409bfb (diff)
refactor: clearer code
Diffstat (limited to 'objects')
-rw-r--r--objects/Obstacle.lua1
-rw-r--r--objects/Shot.lua19
2 files changed, 14 insertions, 6 deletions
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