A Little Space Game made in Love2D
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.

75 lines
1.8 KiB

--! file: player.lua
Player = Object:extend()
require("blaster")
local listOfBullets
function Player:new(x, y)
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.health = 100
self.speed = 200
listOfBullets = {}
end
function Player:update(dt, enemies)
--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
--bullets!
for i, v in ipairs(listOfBullets) do
v:update(dt)
if v.y < 0 then
v.destroy = true
end
if v.destroy == true then
table.remove(listOfBullets, i)
print("Bullet Destroyed! Bullets in Table: ", #listOfBullets)
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 vert = { self.x, self.y, (self.x - 70), (self.y + 70), (self.x + 70), (self.y + 70) }
--love.graphics.polygon("fill", vert)
local fullHealth = self.image[1][2]
love.graphics.draw(fullHealth, self.x, self.y)
for _, v in ipairs(listOfBullets) do
--love.graphics.setColor(v.color)
love.graphics.circle("fill", v.x, v.y, v.radius)
end
--love.graphics.setColor(1, 1, 1)
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(listOfBullets, Blaster(fire_origin_x, fire_origin_y, "player"))
end
end