From 74f515a38586e093d8fa459e5f1e74a600490527 Mon Sep 17 00:00:00 2001 From: yuki Date: Wed, 12 Nov 2025 00:12:58 -0300 Subject: [PATCH] add explosion particles on player death --- obj/game/ExplodeParticle.lua | 51 ++++++++++++++++++++++++++++++++++++ obj/game/Player.lua | 7 +++++ 2 files changed, 58 insertions(+) create mode 100644 obj/game/ExplodeParticle.lua diff --git a/obj/game/ExplodeParticle.lua b/obj/game/ExplodeParticle.lua new file mode 100644 index 0000000..b8b2ebf --- /dev/null +++ b/obj/game/ExplodeParticle.lua @@ -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 diff --git a/obj/game/Player.lua b/obj/game/Player.lua index d6dbe8a..42bf40c 100644 --- a/obj/game/Player.lua +++ b/obj/game/Player.lua @@ -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