From 39fda7fdfb28bc784d42b1fc95d375ab1beefdfe Mon Sep 17 00:00:00 2001 From: Eltrac Date: Fri, 14 Mar 2025 17:31:17 +0800 Subject: feat: purple heart can now bounce --- objects/Shot.lua | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'objects/Shot.lua') diff --git a/objects/Shot.lua b/objects/Shot.lua index 48e5869..cbadabe 100644 --- a/objects/Shot.lua +++ b/objects/Shot.lua @@ -1,4 +1,5 @@ local bulletData = require "data.bulletData" +local Loot = require "objects.loot" local Shot = {} Shot.__index = Shot @@ -39,11 +40,11 @@ end function Shot:onContact(other, contact) local sort = other.objectType + local data = bulletData[self.type] local dieOnThisContact = true -- when contact with enemy, deal damage if sort == "enemy" then - local data = bulletData[self.type] other:takeDamage(self.damage) other:onHit(self) if data and data.afterHit then @@ -63,6 +64,28 @@ function Shot:onContact(other, contact) dieOnThisContact = false end + -- bouncy heart + if sort ~= "player" and data and data.bouncy then + -- if heart can still bounce + if not self.bounced then self.bounced = 0 end + if self.bounced <= data.bouncy then + dieOnThisContact = false + local dx = other.x - self.x + local dy = other.y - self.y + local angle = utils.atan2(dy, dx) + self.physicsBody:setLinearVelocity(math.cos(angle) * config.bullet.speed, + math.sin(angle) * config.bullet.speed) + self.bounced = self.bounced + 1 + -- if not, then die or break + else + dieOnThisContact = true + if math.random(3) == 1 then + local lootItem = Loot:new("brokenheart", other.x, other.y) + table.insert(G.currentRoom.objects.loots, lootItem) + end + end + end + if dieOnThisContact then self:die() end -- cgit v1.2.3