diff --git a/obj/game/Player.lua b/obj/game/Player.lua index 3c179b6..ce6096b 100644 --- a/obj/game/Player.lua +++ b/obj/game/Player.lua @@ -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 diff --git a/obj/game/ShootEffect.lua b/obj/game/ShootEffect.lua new file mode 100644 index 0000000..6115dcf --- /dev/null +++ b/obj/game/ShootEffect.lua @@ -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