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

121 lines
3.4 KiB

7 months ago
Player = Object:extend()
6 months ago
cos = math.cos
sin = math.sin --optimisation
7 months ago
6 months ago
-- Constructor for the Player class
7 months ago
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.width = self.image:getWidth()
self.height = self.image:getHeight()
6 months ago
-- Collision Stuff
self.collider = World:newRectangleCollider(x, y, 64, 64)
self.collider:setFixedRotation(true)
7 months ago
if self.p == 1 then
self.collider:setCollisionClass("Player1")
elseif self.p == 2 then
self.collider:setCollisionClass("Player2")
end
6 months ago
-- Rotation Stuff
7 months ago
self.rotation = math.rad(90)
self.rotSpeed = 2
self.scaleX = 1
self.scaleY = 1
self.originX = self.width / 2
self.originY = self.height / 2
6 months ago
--Velocity
self.vx = 0
self.vy = 0
7 months ago
end
6 months ago
-- Method to handle shooting
7 months ago
function Player:shoot(bulletSpeed)
6 months ago
local offsetX = cos(self.rotation) * self.width * 1.5
local offsetY = sin(self.rotation) * self.height * 1.5
7 months ago
local bulletX = self.x + offsetX
local bulletY = self.y + offsetY
local newBullet = Bullet(bulletX, bulletY, self.p, bulletSpeed, self.rotation)
return newBullet
end
6 months ago
-- Update method for the Player class
7 months ago
function Player:update(dt)
6 months ago
self.vx = 0
self.vy = 0
local bulletSpeed = 20000
7 months ago
if self.p == 1 then
6 months ago
-- Handle player 1 controls
7 months ago
if love.keyboard.isDown("w") then
6 months ago
self.vx = cos(self.rotation) * (self.speed * dt)
self.vy = sin(self.rotation) * (self.speed * dt)
7 months ago
elseif love.keyboard.isDown("s") then
6 months ago
self.vx = cos(self.rotation) * (self.speed * dt) * -1
self.vy = sin(self.rotation) * (self.speed * dt) * -1
7 months ago
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
6 months ago
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
7 months ago
if EnableKeyPress1 == true then
if love.keyboard.isDown("space") then
7 months ago
local newBullet = self:shoot(bulletSpeed)
7 months ago
table.insert(Bullets1, newBullet)
KeyPressTime1 = KeyDelay1
EnableKeyPress1 = false
end
end
6 months ago
self.x = self.collider:getX()
self.y = self.collider:getY()
elseif self.p == 2 then
-- Handle player 2 controls
7 months ago
if love.keyboard.isDown("up") then
6 months ago
self.vx = cos(self.rotation) * (self.speed * dt)
self.vy = sin(self.rotation) * (self.speed * dt)
7 months ago
elseif love.keyboard.isDown("down") then
6 months ago
self.vx = cos(self.rotation) * (self.speed * dt) * -1
self.vy = sin(self.rotation) * (self.speed * dt) * -1
7 months ago
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
6 months ago
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
7 months ago
if EnableKeyPress2 == true then
if love.keyboard.isDown("return") then
7 months ago
local newBullet = self:shoot(bulletSpeed)
7 months ago
table.insert(Bullets2, newBullet)
KeyPressTime2 = KeyDelay2
EnableKeyPress2 = false
end
end
end
6 months ago
self.x = self.collider:getX()
self.y = self.collider:getY()
7 months ago
end
function Player:draw()
love.graphics.draw(self.image, self.x, self.y, self.rotation, self.scaleX, self.scaleY, self.originX, self.originY)
end