add basic projectile
This commit is contained in:
parent
b79c2cf866
commit
429f8ef813
3 changed files with 51 additions and 2 deletions
|
|
@ -27,8 +27,10 @@ function Player:new(area, x, y, opts)
|
||||||
self.max_v = 100
|
self.max_v = 100
|
||||||
-- player acceleration
|
-- player acceleration
|
||||||
self.a = 100
|
self.a = 100
|
||||||
|
-- attack rate
|
||||||
|
self.ar = 0.24
|
||||||
|
|
||||||
self.timer:every(4, function() self:shoot() end)
|
self.timer:every(self.ar, function() self:shoot() end)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Player:update(dt)
|
function Player:update(dt)
|
||||||
|
|
@ -70,6 +72,7 @@ function Player:shoot()
|
||||||
local d = 1.2*self.w
|
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)
|
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})
|
self.area:addGameObject('ShootEffect', offset_x, offset_y, {player = self, d = d})
|
||||||
|
self.area:addGameObject('Projectile', offset_x, offset_y, {r = self.r})
|
||||||
end
|
end
|
||||||
|
|
||||||
return Player
|
return Player
|
||||||
|
|
|
||||||
46
obj/game/Projectile.lua
Normal file
46
obj/game/Projectile.lua
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
---@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)
|
||||||
|
end
|
||||||
|
|
||||||
|
---Draws Projectile
|
||||||
|
function Projectile:draw()
|
||||||
|
Projectile.super.draw(self)
|
||||||
|
if DEBUG then
|
||||||
|
love.graphics.setColor(1, 0, 0)
|
||||||
|
self.collider:draw()
|
||||||
|
end
|
||||||
|
love.graphics.setColor(1, 1, 1)
|
||||||
|
love.graphics.circle('fill', self.x, self.y, self.s)
|
||||||
|
end
|
||||||
|
|
||||||
|
return Projectile
|
||||||
|
|
@ -23,7 +23,7 @@ end
|
||||||
function ShootEffect:draw()
|
function ShootEffect:draw()
|
||||||
ShootEffect.super.draw(self)
|
ShootEffect.super.draw(self)
|
||||||
pushRotateScale(self.x, self.y, self.player.r+math.pi/4)
|
pushRotateScale(self.x, self.y, self.player.r+math.pi/4)
|
||||||
love.graphics.setColor(0.6, 0.5, 0.1)
|
love.graphics.setColor(1,1,1)
|
||||||
love.graphics.rectangle('fill', self.x - self.w/2, self.y - self.w/2, self.w, self.w)
|
love.graphics.rectangle('fill', self.x - self.w/2, self.y - self.w/2, self.w, self.w)
|
||||||
love.graphics.pop()
|
love.graphics.pop()
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue