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.
102 lines
2.7 KiB
102 lines
2.7 KiB
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
|
|
|
|
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)
|
|
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)
|
|
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
|
|
|
|
-- Update method for the Player class
|
|
function Player:update(dt)
|
|
local bulletSpeed = 20000
|
|
|
|
if EnableKeyPress1 == true and self.p == 1 then
|
|
if love.keyboard.isDown("space") then
|
|
local newBullet = self:shoot(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(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
|
|
|