|
|
|
Player = Object:extend()
|
|
|
|
cos = math.cos
|
|
|
|
sin = math.sin --optimisation
|
|
|
|
|
|
|
|
-- Constructor for the Player class
|
|
|
|
function Player:new(p, x, y, health, image, speed, max)
|
|
|
|
self.p = p
|
|
|
|
self.image = love.graphics.newImage(image)
|
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
self.health = health
|
|
|
|
self.speed = speed
|
|
|
|
self.max = max
|
|
|
|
self.width = self.image:getWidth()
|
|
|
|
self.height = self.image:getHeight()
|
|
|
|
|
|
|
|
-- Collision Stuff
|
|
|
|
self.collider = World:newRectangleCollider(x, y, 64, 64)
|
|
|
|
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 = 2
|
|
|
|
self.scaleX = 1
|
|
|
|
self.scaleY = 1
|
|
|
|
self.originX = self.width / 2
|
|
|
|
self.originY = self.height / 2
|
|
|
|
|
|
|
|
--Velocity
|
|
|
|
self.vx = 0
|
|
|
|
self.vy = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Method to handle shooting
|
|
|
|
function Player:shoot(bulletSpeed)
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Update method for the Player class
|
|
|
|
function Player:update(dt)
|
|
|
|
self.vx = 0
|
|
|
|
self.vy = 0
|
|
|
|
local bulletSpeed = 20000
|
|
|
|
|
|
|
|
if self.p == 1 then
|
|
|
|
-- Handle player 1 controls
|
|
|
|
if love.keyboard.isDown("w") then
|
|
|
|
self.vx = cos(self.rotation) * (self.speed * dt)
|
|
|
|
self.vy = sin(self.rotation) * (self.speed * dt)
|
|
|
|
elseif love.keyboard.isDown("s") then
|
|
|
|
self.vx = cos(self.rotation) * (self.speed * dt) * -1
|
|
|
|
self.vy = sin(self.rotation) * (self.speed * dt) * -1
|
|
|
|
elseif love.keyboard.isDown("a") then
|
|
|
|
self.rotation = self.rotation - (self.rotSpeed * dt)
|
|
|
|
elseif love.keyboard.isDown("d") then
|
|
|
|
self.rotation = self.rotation + (self.rotSpeed * dt)
|
|
|
|
end
|
|
|
|
|
|
|
|
self.collider:setLinearVelocity(self.vx, self.vy)
|
|
|
|
|
|
|
|
-- Check for collision with walls
|
|
|
|
if self.collider:enter("Wall") then
|
|
|
|
print("Player 1 collided with wall")
|
|
|
|
end
|
|
|
|
|
|
|
|
if EnableKeyPress1 == true then
|
|
|
|
if love.keyboard.isDown("space") then
|
|
|
|
local newBullet = self:shoot(bulletSpeed)
|
|
|
|
table.insert(Bullets1, newBullet)
|
|
|
|
KeyPressTime1 = KeyDelay1
|
|
|
|
EnableKeyPress1 = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self.x = self.collider:getX()
|
|
|
|
self.y = self.collider:getY()
|
|
|
|
|
|
|
|
-- Handlle map changes
|
|
|
|
if love.keyboard.isDown("1") then
|
|
|
|
LoadMap("1")
|
|
|
|
print("Map 1 loaded")
|
|
|
|
end
|
|
|
|
elseif self.p == 2 then
|
|
|
|
-- Handle player 2 controls
|
|
|
|
if love.keyboard.isDown("up") then
|
|
|
|
self.vx = cos(self.rotation) * (self.speed * dt)
|
|
|
|
self.vy = sin(self.rotation) * (self.speed * dt)
|
|
|
|
elseif love.keyboard.isDown("down") then
|
|
|
|
self.vx = cos(self.rotation) * (self.speed * dt) * -1
|
|
|
|
self.vy = sin(self.rotation) * (self.speed * dt) * -1
|
|
|
|
elseif love.keyboard.isDown("left") then
|
|
|
|
self.rotation = self.rotation - (self.rotSpeed * dt)
|
|
|
|
elseif love.keyboard.isDown("right") then
|
|
|
|
self.rotation = self.rotation + (self.rotSpeed * dt)
|
|
|
|
end
|
|
|
|
self.collider:setLinearVelocity(self.vx, self.vy)
|
|
|
|
|
|
|
|
-- Check for collision with walls
|
|
|
|
if self.collider:enter("Wall") then
|
|
|
|
print("Player 2 collided with wall")
|
|
|
|
end
|
|
|
|
|
|
|
|
if EnableKeyPress2 == true then
|
|
|
|
if love.keyboard.isDown("return") then
|
|
|
|
local newBullet = self:shoot(bulletSpeed)
|
|
|
|
table.insert(Bullets2, newBullet)
|
|
|
|
KeyPressTime2 = KeyDelay2
|
|
|
|
EnableKeyPress2 = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self.x = self.collider:getX()
|
|
|
|
self.y = self.collider:getY()
|
|
|
|
end
|
|
|
|
|
|
|
|
function Player:draw()
|
|
|
|
love.graphics.draw(self.image, self.x, self.y, self.rotation, self.scaleX, self.scaleY, self.originX, self.originY)
|
|
|
|
end
|