diff --git a/player.lua b/player.lua index 34db3e7..3639ab2 100644 --- a/player.lua +++ b/player.lua @@ -30,6 +30,17 @@ function Player:new(p, x, y, health, image, speed) self.originY = self.height / 2 end +function Player:shoot(bulletSpeed) + local cos = math.cos + local sin = math.sin --optimisation + local offsetX = cos(self.rotation) * self.width / 2 + local offsetY = sin(self.rotation) * self.height / 2 + local bulletX = self.x + offsetX + local bulletY = self.y + offsetY + local newBullet = Bullet(bulletX, bulletY, self.p, bulletSpeed, self.rotation) + return newBullet +end + function Player:update(dt) local cos = math.cos local sin = math.sin --optimisation @@ -52,11 +63,7 @@ function Player:update(dt) if EnableKeyPress1 == true then if love.keyboard.isDown("space") then - local offsetX = math.cos(self.rotation) * self.width / 2 - local offsetY = math.sin(self.rotation) * self.height / 2 - local bulletX = self.x + offsetX - local bulletY = self.y + offsetY - local newBullet = Bullet(bulletX, bulletY, self.p, bulletSpeed, self.rotation) + local newBullet = self:shoot(bulletSpeed) table.insert(Bullets1, newBullet) KeyPressTime1 = KeyDelay1 EnableKeyPress1 = false @@ -86,11 +93,7 @@ function Player:update(dt) if EnableKeyPress2 == true then if love.keyboard.isDown("return") then - local offsetX = math.cos(self.rotation) * self.width / 2 - local offsetY = math.sin(self.rotation) * self.height / 2 - local bulletX = self.x + offsetX - local bulletY = self.y + offsetY - local newBullet = Bullet(bulletX, bulletY, self.p, bulletSpeed, self.rotation) + local newBullet = self:shoot(bulletSpeed) table.insert(Bullets2, newBullet) KeyPressTime2 = KeyDelay2 EnableKeyPress2 = false