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/player.lua

144 lines
3.6 KiB

Player = Object:extend()
local cos = math.cos
local sin = math.sin --optimisation
local max = math.max --optimisation
-- Constructor for the Player class
function Player:new(p, x, y, health, image, speed)
self.p = p
self.image = love.graphics.newImage(image)
self.x = x
self.y = y
self.health = health
self.speed = speed
self.bulletSpeed = _G.DEFAULT_B_SPEED
self.width = self.image:getWidth()
self.height = self.image:getHeight()
--Power up flag
self.PUName = ""
self.PUTime = 0
self.PUOn = false
-- Collision Stuff
--self.collider = World:newRectangleCollider(x, y, 50, 62)
self.collider = World:newCircleCollider(x, y, 24 * X_SCALE)
self.collider:setFixedRotation(true)
if self.p == 1 then
self.collider:setCollisionClass("Player1")
elseif self.p == 2 then
self.collider:setCollisionClass("Player2")
end
-- Rotation Stuff
self.rotation = math.rad(90)
self.rotSpeed = 3
--self.scaleX = 1 * _G.X_SCALE
--self.scaleY = 1 * _G.Y_SCALE
self.scaleX = 1
self.scaleY = 1
self.originX = self.width / 2
self.originY = self.height / 2
--Velocity
self.vx = 0
self.vy = 0
-- Bullets shot
self.shot = 0
end
-- Method to handle shooting
function Player:shoot(bulletSpeed)
if self.shot <= 10 then
self.shot = self.shot + 1
local offsetX = cos(self.rotation) * self.width * 1.5
local offsetY = sin(self.rotation) * self.height * 1.5
local bulletX = self.x + offsetX
local bulletY = self.y + offsetY
local newBullet = Bullet(bulletX, bulletY, self.p, bulletSpeed, self.rotation)
return newBullet
else
return nil
end
end
function Player:handleKeys(up, down, left, right, dt)
self.vx, self.vy = 0, 0 --reset every frame
if love.keyboard.isDown(up) then
self.vx = cos(self.rotation) * (self.speed * dt)
self.vy = sin(self.rotation) * (self.speed * dt)
end
if love.keyboard.isDown(down) then
self.vx = cos(self.rotation) * (self.speed / 2 * dt) * -1
self.vy = sin(self.rotation) * (self.speed / 2 * dt) * -1
end
if love.keyboard.isDown(left) then
self.rotation = self.rotation - (self.rotSpeed * dt)
end
if love.keyboard.isDown(right) then
self.rotation = self.rotation + (self.rotSpeed * dt)
end
self.collider:setLinearVelocity(self.vx, self.vy)
end
function Player:updateCol()
if self.p == 1 then
self.x = self.collider:getX()
self.y = self.collider:getY()
elseif self.p == 2 then
self.x = self.collider:getX()
self.y = self.collider:getY()
end
end
function Player:getPowerUp(powerup)
self.PUName = powerup.name
self.PUTime = powerup.time
end
function Player:powerup()
self.PUOn = true
if self.PUName == "speed" then
--print("" .. self.PUName .. " for " .. self.PUTime)
self.speed = self.speed * 2.5
self.rotSpeed = self.rotSpeed * 1.5
end
end
-- Update method for the Player class
function Player:update(dt)
if self.PUOn then
self.PUTime = self.PUTime - dt
if max(0, self.PUTime) <= 0 then
-- Reset the player
self.PUName = "none"
self.PUTime = 0
self.PUOn = false
self.speed = _G.DEFAULT_P_SPEED
end
end
if EnableKeyPress1 == true and self.p == 1 then
if love.keyboard.isDown("space") then
local newBullet = self:shoot(self.bulletSpeed)
table.insert(Bullets1, newBullet)
KeyPressTime1 = KeyDelay1
EnableKeyPress1 = false
end
end
if EnableKeyPress2 == true and self.p == 2 then
if love.keyboard.isDown("return") then
local newBullet = self:shoot(self.bulletSpeed)
table.insert(Bullets2, newBullet)
KeyPressTime2 = KeyDelay2
EnableKeyPress2 = false
end
end
end
function Player:draw()
love.graphics.draw(self.image, self.x, self.y, self.rotation, self.scaleX, self.scaleY, self.originX, self.originY)
end