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.
93 lines
2.2 KiB
93 lines
2.2 KiB
--! file: player.lua
|
|
Player = Object:extend()
|
|
require("blaster")
|
|
local playerBullets
|
|
|
|
--int x, y
|
|
--playableArea {int[2]}
|
|
function Player:new(x, y, playableArea)
|
|
self.image = {
|
|
{ "fullHealth", love.graphics.newImage("/assets/player/player01.png") },
|
|
{ "damaged", love.graphics.newImage("/assets/player/player02.png") },
|
|
{ "halfHealth", love.graphics.newImage("/assets/player/player03.png") },
|
|
{ "nearDeath", love.graphics.newImage("/assets/player/player04.png") },
|
|
}
|
|
|
|
self.x = x
|
|
self.y = y
|
|
self.width = self.image[1][2]:getWidth()
|
|
self.height = self.image[1][2]:getHeight()
|
|
self.playableLeft = playableArea[1]
|
|
self.playableRight = playableArea[2]
|
|
|
|
self.type = "player"
|
|
self.health = 100
|
|
self.speed = 200
|
|
|
|
--bounds of the hitbox
|
|
--(left, right, top, bottom)
|
|
self.bounds = { (self.x + 8), (self.x + 38), (self.y + 10), (self.y + 36) }
|
|
|
|
playerBullets = {}
|
|
end
|
|
|
|
function Player:update(dt, enemies)
|
|
local maximumLeft = self.playableLeft - 8
|
|
local maximumRight = self.playableRight - 38
|
|
|
|
--movement
|
|
if love.keyboard.isDown("left") then
|
|
self.x = self.x - self.speed * dt
|
|
end
|
|
if love.keyboard.isDown("right") then
|
|
self.x = self.x + self.speed * dt
|
|
end
|
|
|
|
if self.x < maximumLeft then
|
|
self.x = maximumLeft
|
|
elseif self.x > maximumRight then
|
|
self.x = maximumRight
|
|
end
|
|
|
|
--update bounds
|
|
self.bounds = { (self.x + 8), (self.x + 38), (self.y + 10), (self.y + 36) }
|
|
|
|
--bullets!
|
|
for i, v in ipairs(playerBullets) do
|
|
v:update(dt)
|
|
if v.y < 0 then
|
|
v.destroy = true
|
|
end
|
|
|
|
if v.destroy == true then
|
|
table.remove(playerBullets, i)
|
|
print("Bullet Destroyed! Bullets in Table: ", #playerBullets)
|
|
end
|
|
--recieve the list of enemies and check to see if the bullets hit
|
|
for _, j in ipairs(enemies) do
|
|
v:checkCollision(j)
|
|
end
|
|
end
|
|
end
|
|
|
|
function Player:draw()
|
|
local fullHealth = self.image[1][2]
|
|
|
|
love.graphics.draw(fullHealth, self.x, self.y)
|
|
|
|
for _, v in ipairs(playerBullets) do
|
|
v:draw()
|
|
end
|
|
end
|
|
|
|
function Player:keyPressed(key)
|
|
local fire_origin_x = self.x + 35
|
|
local fire_origin_y = self.y + 35
|
|
--Add controls here
|
|
|
|
--Shooting controls
|
|
if key == "space" then
|
|
--pew pew
|
|
table.insert(playerBullets, Blaster(fire_origin_x, fire_origin_y, "player"))
|
|
end
|
|
end
|
|
|