52 lines
1.4 KiB
Lua
52 lines
1.4 KiB
Lua
---@class Projectile:GameObject
|
|
---@field r number
|
|
---@field s number|nil
|
|
---@field v number|nil
|
|
local Projectile = GameObject:extend()
|
|
|
|
function Projectile:new(area, x, y, opts)
|
|
Projectile.super.new(self, area, x, y, opts)
|
|
|
|
-- radius of collider
|
|
self.s = opts.s or 2.5
|
|
-- velocity
|
|
self.v = opts.v or 200
|
|
self.collider = self.area.world:circle(self.x, self.y, self.s)
|
|
end
|
|
|
|
---Updates Projectile
|
|
---@param dt number delta time
|
|
function Projectile:update(dt)
|
|
Projectile.super.update(self, dt)
|
|
|
|
-- update velocity
|
|
--self.v = math.min(self.v + self.a*dt, self.max_v)
|
|
|
|
-- update position
|
|
local vx = self.v * math.cos(self.r) -- velocity X
|
|
local vy = self.v * math.sin(self.r) -- velocity Y
|
|
self.x = self.x + vx * dt -- update position X
|
|
self.y = self.y + vy * dt -- update position Y
|
|
|
|
-- move collision area
|
|
self.collider:moveTo(self.x, self.y)
|
|
|
|
-- offscreen despawn
|
|
if self.x < 0 then self:kill() end
|
|
if self.y < 0 then self:kill() end
|
|
if self.x > gw then self:kill() end
|
|
if self.y > gh then self:kill() end
|
|
end
|
|
|
|
---Draws Projectile
|
|
function Projectile:draw()
|
|
Projectile.super.draw(self)
|
|
if DEBUG then
|
|
love.graphics.setColor(COLORS.collision)
|
|
self.collider:draw()
|
|
end
|
|
love.graphics.setColor(COLORS.default)
|
|
love.graphics.circle('fill', self.x, self.y, self.s)
|
|
end
|
|
|
|
return Projectile
|