add explosion particles on player death

This commit is contained in:
yuki 2025-11-12 00:12:58 -03:00
parent 62bc32c97a
commit 74f515a385
2 changed files with 58 additions and 0 deletions

View file

@ -0,0 +1,51 @@
---@class ExplodeParticle:GameObject
---@field color table
---@field r number
---@field s number
---@field v number
---@field lw number
local ExplodeParticle = GameObject:extend()
function ExplodeParticle:new(area, x, y, opts)
ExplodeParticle.super.new(self, area, x, y, opts)
-- particle effect's color
self.color = opts.color or COLORS.default
-- particle angle
self.r = random(0, 2*math.pi)
-- scale
self.s = opts.s or random(2,3)
-- velocity
self.v = opts.v or random(75, 150)
-- line width
self.lw = opts.lw or 2
self.timer:tween(
opts.d or random(.3,.5),
self, {s = 0, v = 0, lw = 0},
'linear',
function() self:kill() end
)
end
---Updates ExplodeParticle
---@param dt number delta time
function ExplodeParticle:update(dt)
ExplodeParticle.super.update(self, dt)
self.x = self.x + self.v*math.cos(self.r)*dt
self.y = self.y + self.v*math.sin(self.r)*dt
--self.r = self.r + self.v*dt
end
---Draws ExplodeParticle
function ExplodeParticle:draw()
ExplodeParticle.super.draw(self)
pushRotateScale(self.x, self.y, self.r)
love.graphics.setColor(self.color)
love.graphics.setLineWidth(self.lw)
love.graphics.line(self.x - self.s, self.y, self.x + self.s, self.y)
love.graphics.setLineWidth(1)
love.graphics.setColor(COLORS.default)
love.graphics.pop()
end
return ExplodeParticle

View file

@ -82,4 +82,11 @@ function Player:shoot()
proj.timer:tween(0.5, proj, {v = 400}, 'linear')
end
function Player:kill()
for i=1, random(8,12) do
self.area:addGameObject('ExplodeParticle', self.x, self.y)
end
self:destroy()
end
return Player