summaryrefslogtreecommitdiff
path: root/objects
diff options
context:
space:
mode:
authorEltrac <hi@guhub.cn>2025-03-16 15:25:27 +0800
committerEltrac <hi@guhub.cn>2025-03-16 15:25:27 +0800
commit220fdf4e42902d9155ad838bb8596a6bbfc7de51 (patch)
treeff91fc7d17b3e00b6812604c0c6046e7853075bf /objects
parent7c1ded6fdf9cf897132bd2dd8133064c7b07976c (diff)
fix: next bullet index issues when lose or gain heart
Diffstat (limited to 'objects')
-rw-r--r--objects/Loot.lua2
-rw-r--r--objects/Player.lua20
2 files changed, 20 insertions, 2 deletions
diff --git a/objects/Loot.lua b/objects/Loot.lua
index cfbfea8..1e39211 100644
--- a/objects/Loot.lua
+++ b/objects/Loot.lua
@@ -22,7 +22,7 @@ function Loot:onContact(other)
local loots = G.currentRoom.objects.loots
if other.objectType == "player" then
local pickedLoot = table.remove(loots, utils.indexof(loots, self))
- table.insert(G.player.hearts, pickedLoot.type)
+ G.player:gainHeart(pickedLoot.type)
G.BodyLifeCycleManager:destroy(self.physicsBody)
end
end
diff --git a/objects/Player.lua b/objects/Player.lua
index 89b1b54..5103b17 100644
--- a/objects/Player.lua
+++ b/objects/Player.lua
@@ -94,6 +94,10 @@ function Player:shoot(target)
table.remove(self.hearts, self.nextBullet)
self.nextBullet = self.nextBullet - 1
+ if self.nextBullet <= 0 then
+ self.nextBullet = #self.hearts
+ end
+
self.shootCooldown = G.time + config.player.shoot.cooldown
end
@@ -143,6 +147,20 @@ function Player:punch(target)
audio.play("punch")
end
+function Player:loseHeart()
+ if self.nextBullet == #self.hearts then
+ self.nextBullet = self.nextBullet - 1
+ end
+ table.remove(self.hearts)
+end
+
+function Player:gainHeart(name)
+ if self.nextBullet == #self.hearts then
+ self.nextBullet = self.nextBullet + 1
+ end
+ table.insert(self.hearts, name)
+end
+
function Player:onContact(other, contact)
-- detect contact with enemies
if other.objectType == "enemy" then
@@ -150,7 +168,7 @@ function Player:onContact(other, contact)
-- take the damage
if not (other.stunned or self:isShielded()) then
audio.play("getsHit")
- table.remove(self.hearts)
+ self:loseHeart()
-- knock back Player
local ex, ey = other.x, other.y
local dx, dy = self.x - ex, self.y - ey