From 9530402ae9a7d234dc42e9a397a67cc80fef2880 Mon Sep 17 00:00:00 2001 From: NCLanceman Date: Sun, 13 Apr 2025 00:49:44 -0500 Subject: [PATCH] Corrected enemy hitbox / sprite draw --- blaster.lua | 22 +++++++++++----------- entities/enemy.lua | 36 ++++++++++++++++++++---------------- entities/player.lua | 6 +++--- main.lua | 23 ++++++++++++----------- 4 files changed, 46 insertions(+), 41 deletions(-) diff --git a/blaster.lua b/blaster.lua index c4b8d5e..4499471 100644 --- a/blaster.lua +++ b/blaster.lua @@ -11,7 +11,7 @@ function Blaster:new(x, y, owner) self.destroy = false self.owner = owner - self.bounds = { self.x, (self.x + self.width), self.y, (self.y + self.height) } + self.hitbox = { self.x, (self.x + self.width), self.y, (self.y + self.height) } end function Blaster:draw() @@ -26,19 +26,19 @@ function Blaster:update(dt) self.y = self.y + self.speed * dt end - self.bounds = { self.x, (self.x + self.width), self.y, (self.y + self.height) } + self.hitbox = { self.x, (self.x + self.width), self.y, (self.y + self.height) } end function Blaster:checkCollision(obj) - local self_left = self.bounds[1] - local self_right = self.bounds[2] - local self_top = self.bounds[3] - local self_bottom = self.bounds[4] - - local obj_left = obj.bounds[1] - local obj_right = obj.bounds[2] - local obj_top = obj.bounds[3] - local obj_bottom = obj.bounds[4] + local self_left = self.hitbox[1] + local self_right = self.hitbox[2] + local self_top = self.hitbox[3] + local self_bottom = self.hitbox[4] + + local obj_left = obj.hitbox[1] + local obj_right = obj.hitbox[2] + local obj_top = obj.hitbox[3] + local obj_bottom = obj.hitbox[4] if (self_right > obj_left) and (self_left < obj_right) and (self_bottom > obj_top) and (self_top < obj_bottom) then self.destroy = true diff --git a/entities/enemy.lua b/entities/enemy.lua index 99abd24..4ed91aa 100644 --- a/entities/enemy.lua +++ b/entities/enemy.lua @@ -23,19 +23,24 @@ function Enemy:new(x, y, radius, playableArea) self.fireRate = 1 self.destroy = false - self.hitbox = { (self.x + 18), (self.x + 44), (self.y + 20), (self.y + 44) } - self.move_bounds = self:setMoveBounds({ 18, 44 }, self.move_radius) - print("Move Bounds: ", self.move_bounds[1], " , ", self.move_bounds[2]) + self.hitbox = { (self.x + 20), (self.x + 20), (self.y + 20), (self.y + 44) } + self.move_bounds = self:setMoveBounds() + --print("Move Bounds: ", self.move_bounds[1], " , ", self.move_bounds[2]) enemyBullets = {} end function Enemy:draw() --draw stuff - --love.graphics.rectangle("line", self.x, self.y, self.width, self.height) - love.graphics.draw(self.image, self.x, self.y, 0, -1) + love.graphics.rectangle( + "line", + self.hitbox[1], + self.hitbox[3], + (self.hitbox[2] - self.hitbox[1]), + (self.hitbox[4] - self.hitbox[3]) + ) + love.graphics.draw(self.image, self.x, self.y, 0, 1, 1) for _, v in ipairs(enemyBullets) do - --love.graphics.circle("fill", v.x, v.y, v.radius) v:draw() end end @@ -50,10 +55,8 @@ function Enemy:update(dt, player) --movement if self.x < self.move_bounds[1] then - print("Reversing direction! Hitbox Left Location: ", self.hitbox[1], " Play Bound Left: ", self.playableLeft) self.speed = -self.speed elseif self.x > self.move_bounds[2] then - print("Reversing direction! Hitbox Right Location: ", self.hitbox[2], " Play Bound Right: ", self.playableRight) self.speed = -self.speed end @@ -82,24 +85,25 @@ end --shoot at the player a variable number of seconds function Enemy:fire() + local fire_origin = { self.x + 26, self.y + 44 } if love.timer.getTime() - fireTime > self.fireRate then - table.insert(enemyBullets, Blaster(self.x, self.y, "enemy")) + table.insert(enemyBullets, Blaster(fire_origin[1], fire_origin[2], "enemy")) fireTime = love.timer.getTime() end end -function Enemy:setMoveBounds(hitbox_bounds, move_radius) +function Enemy:setMoveBounds() local result = {} - if (self.origin[1] - hitbox_bounds[1] - move_radius) < self.playableLeft then + if (self.hitbox[1] - self.move_radius) < self.playableLeft then result[1] = self.playableLeft - result[2] = self.playableLeft + hitbox_bounds[1] + (move_radius * 2) - elseif (self.origin[2] + hitbox_bounds[2] + move_radius) > self.playableRight then + result[2] = self.playableLeft + self.hitbox[1] + move_radius + elseif (self.hitbox[2] + self.move_radius) > self.playableRight then result[2] = self.playableRight - result[1] = self.playableRight - hitbox_bounds[2] - (move_radius * 2) + result[1] = self.playableRight - self.hitbox[2] - move_radius else - result[1] = self.x - move_radius - result[2] = self.x + move_radius + result[1] = self.x - self.move_radius + result[2] = self.x + self.move_radius end return result end diff --git a/entities/player.lua b/entities/player.lua index a60cbb5..d6e6280 100644 --- a/entities/player.lua +++ b/entities/player.lua @@ -26,7 +26,7 @@ function Player:new(x, y, playableArea) --bounds of the hitbox --(left, right, top, bottom) - self.bounds = { (self.x + 8), (self.x + 38), (self.y + 10), (self.y + 36) } + self.hitbox = { (self.x + 8), (self.x + 38), (self.y + 10), (self.y + 36) } print("Player: Max Left: ", self.playableLeft - 8, ", Max Right: ", self.playableRight - 38) playerBullets = {} @@ -50,8 +50,8 @@ function Player:update(dt, enemies) self.x = maximumRight end - --update bounds - self.bounds = { (self.x + 8), (self.x + 38), (self.y + 10), (self.y + 36) } + --update hitbox + self.hitbox = { (self.x + 8), (self.x + 38), (self.y + 10), (self.y + 36) } --bullets! for i, v in ipairs(playerBullets) do diff --git a/main.lua b/main.lua index d13239f..041cfd4 100644 --- a/main.lua +++ b/main.lua @@ -12,37 +12,38 @@ function love.load() print("Playable Bounds: ", bounds[1], " , ", bounds[2]) --place the player in the middle of the screen, close to the bottom - player = Player(window_width / 2, window_height * 0.9, bounds) + Player = Player(window_width / 2, window_height * 0.9, bounds) - listOfEnemies = {} + ListOfEnemies = {} - table.insert(listOfEnemies, Enemy(bounds[1] + 50, 100, 70, bounds)) + table.insert(ListOfEnemies, Enemy(bounds[1] + 50, 100, 70, bounds)) + table.insert(ListOfEnemies, Enemy((bounds[1] + bounds[2]) / 2, 125, 70, bounds)) --set background for space! love.graphics.setBackgroundColor(27 / 255, 51 / 255, 85 / 255) end function love.update(dt) - player:update(dt, listOfEnemies) + Player:update(dt, ListOfEnemies) - for i, v in ipairs(listOfEnemies) do + for i, v in ipairs(ListOfEnemies) do if v.destroy == true then - print("Enemy destroyed! Enemies in Table:", #listOfEnemies) - table.remove(listOfEnemies, i) + print("Enemy destroyed! Enemies in Table:", #ListOfEnemies) + table.remove(ListOfEnemies, i) end - v:update(dt, player) + v:update(dt, Player) end end function love.draw() ui:draw() - player:draw() - for _, v in ipairs(listOfEnemies) do + Player:draw() + for _, v in ipairs(ListOfEnemies) do v:draw() end end function love.keypressed(key) - player:keyPressed(key) + Player:keyPressed(key) end