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

110 lines
2.8 KiB

5 months ago
Player = Object:extend()
4 months ago
cos = math.cos
sin = math.sin --optimisation
5 months ago
4 months ago
-- Constructor for the Player class
function Player:new(p, x, y, health, image, speed)
5 months ago
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()
4 months ago
-- Collision Stuff
--self.collider = World:newRectangleCollider(x, y, 50, 62)
self.collider = World:newCircleCollider(x, y, 24)
4 months ago
self.collider:setFixedRotation(true)
5 months ago
if self.p == 1 then
self.collider:setCollisionClass("Player1")
elseif self.p == 2 then
self.collider:setCollisionClass("Player2")
end
4 months ago
-- Rotation Stuff
5 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
4 months ago
--Velocity
self.vx = 0
self.vy = 0
2 months ago
-- Bullets shot
self.shot = 0
5 months ago
end
4 months ago
-- Method to handle shooting
5 months ago
function Player:shoot(bulletSpeed)
2 months ago
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
5 months ago
end
2 months ago
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 / 2 * dt) * -1
self.vy = sin(self.rotation) * (self.speed / 2 * dt) * -1
2 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
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
4 months ago
-- Update method for the Player class
5 months ago
function Player:update(dt)
2 months ago
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
5 months ago
2 months ago
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
5 months ago
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