ZLP1/scripts/entities/bullet.lua

85 lines
2.2 KiB
Lua

gunBullet = Entity:New(x,y)
function gunBullet:New(x,y,angle,data)
local o = Entity:New(x,y,angle)
o.target = {}
o.hitbox = 2
o.draw_scale = 2
o.angle = angle
o.hits = data.hits
o.damage = data.damage
o.animation = AnimationContainer:New(data.animation)
o.moveSpeed = data.moveSpeed
o.moveSpeedValues = data.moveSpeedValues or { min = 0, max = 100}
o.moveSpeedIncrease = data.moveSpeedIncrease or 0
o.moveHorizontalIncrease = data.moveHorizontalIncrease or 0
o.moveVerticalIncrease = data.moveVerticalIncrease or 0
o.moveHorizontal = 0
o.moveVertical = 0
-- lists
o:AddList(List.Simulate)
local friendly = data.friendly or false
local hostile = data.hostile or false
if friendly == true then
o:AddList(List.FriendlyBullet)
end
if hostile == true then
o:AddList(List.EnemyBullet)
end
setmetatable(o, self)
self.__index = self
return o
end
function gunBullet:Step()
self.moveSpeed = self.moveSpeed + self.moveSpeedIncrease
self.moveSpeed = math.max(self.moveSpeedValues.min,self.moveSpeed)
self.moveSpeed = math.min(self.moveSpeedValues.max,self.moveSpeed)
self.moveHorizontal = self.moveHorizontal + self.moveHorizontalIncrease
self.moveVertical = self.moveVertical + self.moveVerticalIncrease
local move_x = (self.moveSpeed) * math.cos(self.angle) + self.moveHorizontal
local move_y = (self.moveSpeed) * math.sin(self.angle) + self.moveVertical
self.pos.x = self.pos.x + move_x
self.pos.y = self.pos.y + move_y
self:AngleTowards(
self.pos.x + move_x,
self.pos.y + move_y
)
if self.pos.x < 0 or self.pos.x > window_width
or self.pos.y < 0 or self.pos.y > window_height then
self:RemoveList(List.Simulate)
self:RemoveList(List.CollidesEnemies)
end
end
function gunBullet:Draw()
self.animation:Animate()
love.graphics.line(self.pos.x, self.pos.y, self.pos.x + 10*self.vel.x, self.pos.y + 10*self.vel.y)
self.animation:Draw(
self.pos.x,
self.pos.y,
self.angle+math.rad(90),
self.draw_scale,
self.draw_scale
)
--[[love.graphics.draw(
self.sprite,
self.pos.x,
self.pos.y,
self.angle+math.rad(90),
self.draw_scale,
self.draw_scale,
self.sprite:getPixelWidth()/2,
self.sprite:getPixelHeight()/2
)]]
end