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

107 lines
3.2 KiB

Player = Object:extend()
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()
--Collision Stuff
self.collider = World:newBSGRectangleCollider(x, y, 64, 64, 4)
self.collider:setPosition(self.x, self.y)
if self.p == 1 then
self.collider:setCollisionClass("Player1")
elseif self.p == 2 then
self.collider:setCollisionClass("Player2")
end
self.collider:setType("static")
self.collider:setObject(self)
--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
end
function Player:shoot(bulletSpeed)
local cos = math.cos
local sin = math.sin --optimisation
local offsetX = cos(self.rotation) * self.width / 2
local offsetY = sin(self.rotation) * self.height / 2
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:update(dt)
local cos = math.cos
local sin = math.sin --optimisation
local bulletSpeed = 300
if self.p == 1 then
if love.keyboard.isDown("w") then
self.x = self.x + cos(self.rotation) * (self.speed * dt)
self.y = self.y + sin(self.rotation) * (self.speed * dt)
self.collider:setPosition(self.x, self.y)
elseif love.keyboard.isDown("s") then
self.x = self.x - cos(self.rotation) * (self.speed * dt)
self.y = self.y - sin(self.rotation) * (self.speed * dt)
self.collider:setPosition(self.x, self.y)
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
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
--Query Collision
if self.collider:enter("Player2") then
local collision_data = self.collider:getEnterCollisionData("Player2")
print(collision_data)
end
end
if self.p == 2 then
if love.keyboard.isDown("up") then
self.x = self.x + cos(self.rotation) * (self.speed * dt)
self.y = self.y + sin(self.rotation) * (self.speed * dt)
self.collider:setPosition(self.x, self.y)
elseif love.keyboard.isDown("down") then
self.x = self.x - cos(self.rotation) * (self.speed * dt)
self.y = self.y - sin(self.rotation) * (self.speed * dt)
self.collider:setPosition(self.x, self.y)
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
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
end
function Player:draw()
love.graphics.draw(self.image, self.x, self.y, self.rotation, self.scaleX, self.scaleY, self.originX, self.originY)
end