|
|
|
Bullet = Object:extend()
|
|
|
|
cos = math.cos
|
|
|
|
sin = math.sin --optimisation
|
|
|
|
pi = math.pi
|
|
|
|
|
|
|
|
function Bullet:new(x, y, p, speed, rotation)
|
|
|
|
self.image = love.graphics.newImage("assets/bullet.png")
|
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
self.p = p
|
|
|
|
self.speed = speed
|
|
|
|
self.rotation = rotation
|
|
|
|
|
|
|
|
self.width = self.image:getWidth()
|
|
|
|
self.height = self.image:getHeight()
|
|
|
|
|
|
|
|
self.scaleX = 1
|
|
|
|
self.scaleY = 1
|
|
|
|
self.originX = self.width / 2
|
|
|
|
self.originY = self.height / 2
|
|
|
|
|
|
|
|
self.collider = World:newCircleCollider(x, y, 10)
|
|
|
|
self.collider:setFixedRotation(true)
|
|
|
|
self.collider:setPosition(self.x, self.y)
|
|
|
|
|
|
|
|
if self.p == 1 then
|
|
|
|
self.collider:setCollisionClass("Bullet1")
|
|
|
|
elseif self.p == 2 then
|
|
|
|
self.collider:setCollisionClass("Bullet2")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Bullet:update(dt)
|
|
|
|
--If a bullet hits a wall, make it bounce off the wall and change its rotation
|
|
|
|
if self.p == 1 then
|
|
|
|
if self.collider:enter("Wall") then
|
|
|
|
--calculate normal of the wall
|
|
|
|
--calculate the angle of reflection
|
|
|
|
local angle = (math.atan2(dx, dy))
|
|
|
|
--get normal of the wall without using getNormal()
|
|
|
|
|
|
|
|
----TODO: fix this
|
|
|
|
|
|
|
|
--set the new angle
|
|
|
|
self.rotation = angle
|
|
|
|
self.collider:setAngle(self.rotation)
|
|
|
|
end
|
|
|
|
dx = cos(self.rotation) * self.speed * dt
|
|
|
|
dy = sin(self.rotation) * self.speed * dt
|
|
|
|
self.collider:setLinearVelocity(dx, dy)
|
|
|
|
end
|
|
|
|
|
|
|
|
if self.p == 2 then
|
|
|
|
local dx = cos(self.rotation) * self.speed * dt
|
|
|
|
local dy = sin(self.rotation) * self.speed * dt
|
|
|
|
self.collider:setLinearVelocity(dx, dy)
|
|
|
|
end
|
|
|
|
|
|
|
|
self.x = self.collider:getX()
|
|
|
|
self.y = self.collider:getY()
|
|
|
|
end
|
|
|
|
|
|
|
|
function Bullet:draw()
|
|
|
|
for _, _ in ipairs(Bullets1) do
|
|
|
|
love.graphics.draw(
|
|
|
|
self.image,
|
|
|
|
self.x,
|
|
|
|
self.y,
|
|
|
|
self.rotation,
|
|
|
|
self.scaleX,
|
|
|
|
self.scaleY,
|
|
|
|
self.originX,
|
|
|
|
self.originY
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, _ in ipairs(Bullets2) do
|
|
|
|
love.graphics.draw(
|
|
|
|
self.image,
|
|
|
|
self.x,
|
|
|
|
self.y,
|
|
|
|
self.rotation,
|
|
|
|
self.scaleX,
|
|
|
|
self.scaleY,
|
|
|
|
self.originX,
|
|
|
|
self.originY
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|