diff --git a/blaster.lua b/blaster.lua index a7863e9..8681a24 100644 --- a/blaster.lua +++ b/blaster.lua @@ -4,6 +4,8 @@ function Blaster:new(x, y, owner) self.x = x self.y = y self.radius = 10 + self.height = self.radius * 2 + self.width = self.radius * 2 self.speed = 700 self.color = { 1, 0, 0 } self.destroy = false @@ -12,7 +14,7 @@ end function Blaster:draw() --draw stuff - --love.graphics.circle("fill", self.x, self.y, self.radius) + love.graphics.circle("fill", self.x, self.y, self.radius) end function Blaster:update(dt) @@ -25,18 +27,18 @@ end function Blaster:checkCollision(obj) local self_left = self.x - local self_right = self.x + self.radius * 2 + local self_right = self.x + self.width local self_top = self.y - local self_bottom = self.y + self.radius * 2 + local self_bottom = self.y + self.height local obj_left = obj.x local obj_right = obj.x + obj.width local obj_top = obj.y local obj_bottom = obj.y + obj.height - if self_right > obj_left and self_left < obj_right and self_bottom > obj_top and self_top < obj_bottom then + if (self_right > obj_left) and (self_left < obj_right) and (self_bottom > obj_top) and (self_top < obj_bottom) then self.destroy = true - + obj.health = obj.health - 25 --other blaster logic here end end diff --git a/entities/enemy.lua b/entities/enemy.lua index 36266ff..2c5da96 100644 --- a/entities/enemy.lua +++ b/entities/enemy.lua @@ -3,17 +3,20 @@ require("blaster") Enemy = Object:extend() local fireTime = love.timer.getTime() +local listOfBullets function Enemy:new(x, y, radius) self.origin = { x, y } + self.image = love.graphics.newImage("/assets/enemy/e-fighter.png") self.x = x self.y = y - self.width = 30 - self.height = 30 - self.health = 100 + self.width = self.image:getWidth() + self.height = self.image:getHeight() + self.health = 10 self.move_radius = radius self.speed = 100 self.fireRate = 2 + self.destroy = false listOfBullets = {} end @@ -21,16 +24,15 @@ end function Enemy:draw() --draw stuff --love.graphics.rectangle("line", self.x, self.y, self.width, self.height) - local fighter = love.graphics.newImage("/assets/enemy/e-fighter.png") - - love.graphics.draw(fighter, self.x, self.y, 0, -1) + love.graphics.draw(self.image, self.x, self.y, 0, -1) for _, v in ipairs(listOfBullets) do - love.graphics.circle("fill", v.x, v.y, v.radius) + --love.graphics.circle("fill", v.x, v.y, v.radius) + v:draw() end end -function Enemy:update(dt) +function Enemy:update(dt, player) local bounds = { self.origin[1] - self.move_radius, self.origin[1] + self.move_radius } local window_height = love.graphics.getHeight() @@ -57,6 +59,12 @@ function Enemy:update(dt) print("Enemy Bullet Removed! Bullets in table remaining: ", #listOfBullets) table.remove(listOfBullets, i) end + + v:checkCollision(player) + end + --health check + if self.health < 0 then + self.destroy = true end end diff --git a/entities/player.lua b/entities/player.lua index 58eb5af..d63f7f6 100644 --- a/entities/player.lua +++ b/entities/player.lua @@ -1,17 +1,27 @@ --! 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) +function Player:update(dt, enemies) --movement if love.keyboard.isDown("left") then self.x = self.x - self.speed * dt @@ -31,16 +41,17 @@ function Player:update(dt) 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 = love.graphics.newImage("/assets/player/player01.png") - local damaged = love.graphics.newImage("/assets/player/player02.png") - local halfHealth = love.graphics.newImage("/assets/player/player03.png") - local nearDeath = love.graphics.newImage("/assets/player/player04.png") + local fullHealth = self.image[1][2] love.graphics.draw(fullHealth, self.x, self.y) diff --git a/main.lua b/main.lua index b7c7e40..387d2d6 100644 --- a/main.lua +++ b/main.lua @@ -8,20 +8,33 @@ function love.load() local window_height = love.graphics.getHeight() player = Player(window_width / 2, window_height * 0.8) - enemy = Enemy(100, 100, 70) + + listOfEnemies = {} + + table.insert(listOfEnemies, Enemy(100, 100, 70)) --set background for space! love.graphics.setBackgroundColor(27 / 255, 51 / 255, 85 / 255) end function love.update(dt) - player:update(dt) - enemy:update(dt) + player:update(dt, listOfEnemies) + + for i, v in ipairs(listOfEnemies) do + if v.destroy == true then + print("Enemy destroyed! Enemies in Table:", #listOfEnemies) + table.remove(listOfEnemies, i) + end + + v:update(dt, player) + end end function love.draw() player:draw() - enemy:draw() + for _, v in ipairs(listOfEnemies) do + v:draw() + end end function love.keypressed(key) diff --git a/projectplan.md b/projectplan.md index fc915b1..6570fc1 100644 --- a/projectplan.md +++ b/projectplan.md @@ -1,8 +1,8 @@ Beginning and game notes for a little space game. **TODO:** -- Player character -- Enemies +- Adjust assets +-- Assets are currently too large for hitbox registration. Fix that. - Asteroid obsticles - Pickups - Score