You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
love2d-tank/bullet.lua

78 lines
1.7 KiB

Bullet = Object:extend()
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:newRectangleCollider(x, y, 10, 15)
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
--TODO: find out a way to check the direction of a bullet
end
function Bullet:update(dt)
local cos = math.cos
local sin = math.sin --optimisation
local atan2 = math.atan2 --optimisation
if self.p == 1 then
local dx = math.cos(self.rotation) * self.speed * dt
local dy = math.sin(self.rotation) * self.speed * dt
self.x = self.x + dx
self.y = self.y + dy
self.collider:setPosition(self.x, self.y)
end
if self.p == 2 then
local dx = math.cos(self.rotation) * self.speed * dt
local dy = math.sin(self.rotation) * self.speed * dt
self.x = self.x + dx
self.y = self.y + dy
self.collider:setPosition(self.x, self.y)
end
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)
love.graphics.draw(
self.image,
self.x,
self.y,
self.rotation,
self.scaleX,
self.scaleY,
self.originX,
self.originY
)
end
end