implement basic ShootEffect

This commit is contained in:
yuki 2025-11-11 20:24:55 -03:00
parent 64c825c84e
commit 4d2874c6be
2 changed files with 38 additions and 0 deletions

View file

@ -36,6 +36,7 @@ function Player:update(dt)
-- controls
if input:down('left') then self.r = self.r - self.rv*dt end
if input:down('right') then self.r = self.r + self.rv*dt end
if input:pressed('action') then self:shoot() end
-- update velocity
self.v = math.min(self.v + self.a*dt, self.max_v)
@ -62,4 +63,10 @@ function Player:draw()
love.graphics.circle('line', self.x, self.y, self.w)
end
function Player:shoot()
local d = 1.2*self.w
local offset_x, offset_y = self.x + d*math.cos(self.r), self.y + d*math.sin(self.r)
self.area:addGameObject('ShootEffect', offset_x, offset_y, {player = self, d = d})
end
return Player

31
obj/game/ShootEffect.lua Normal file
View file

@ -0,0 +1,31 @@
---@class ShootEffect:GameObject
---@field player Player|nil
---@field d number
local ShootEffect = GameObject:extend()
function ShootEffect:new(area, x, y, opts)
ShootEffect.super.new(self, area, x, y, opts)
self.w = 8
self.timer:tween(0.4, self, {w = 0}, 'in-out-cubic', function() self:kill() end)
end
---Updates ShootEffect
---@param dt number delta time
function ShootEffect:update(dt)
ShootEffect.super.update(self, dt)
if self.player then
self.x = self.player.x + self.d*math.cos(self.player.r)
self.y = self.player.y + self.d*math.sin(self.player.r)
end
end
---Draws ShootEffect
function ShootEffect:draw()
ShootEffect.super.draw(self)
pushRotateScale(self.x, self.y, self.player.r+math.pi/4)
love.graphics.setColor(0.6, 0.5, 0.1)
love.graphics.rectangle('fill', self.x - self.w/2, self.y - self.w/2, self.w, self.w)
love.graphics.pop()
end
return ShootEffect