diff --git a/entities/enemy.lua b/entities/enemy.lua index cb16bd6..99abd24 100644 --- a/entities/enemy.lua +++ b/entities/enemy.lua @@ -13,8 +13,6 @@ function Enemy:new(x, y, radius, playableArea) self.x = x self.y = y - self.width = self.image:getWidth() - self.height = self.image:getHeight() self.playableLeft = playableArea[1] self.playableRight = playableArea[2] @@ -26,6 +24,8 @@ function Enemy:new(x, y, radius, playableArea) 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]) enemyBullets = {} end @@ -41,16 +41,19 @@ function Enemy:draw() end function Enemy:update(dt, player) - local game_bounds = { self.origin[1] - self.move_radius, self.origin[1] + self.move_radius } + -- if spawn location is near the edge, adjust the bounds such that the radius + -- does not overlap the bounds of the playable area local window_height = love.graphics.getHeight() self.x = self.x + self.speed * dt - self.hitbox = { (self.x + 18), (self.x + 44), (self.y + 20), (self.y + 44) } + --movement - if self.x > game_bounds[2] then + 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 < game_bounds[1] then + 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 @@ -65,7 +68,7 @@ function Enemy:update(dt, player) end if v.destroy == true then - print("Enemy Bullet Removed! Bullets in table remaining: ", #enemyBullets) + --print("Enemy Bullet Removed! Bullets in table remaining: ", #enemyBullets) table.remove(enemyBullets, i) end @@ -84,3 +87,19 @@ function Enemy:fire() fireTime = love.timer.getTime() end end + +function Enemy:setMoveBounds(hitbox_bounds, move_radius) + local result = {} + + if (self.origin[1] - hitbox_bounds[1] - 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.playableRight + result[1] = self.playableRight - hitbox_bounds[2] - (move_radius * 2) + else + result[1] = self.x - move_radius + result[2] = self.x + move_radius + end + return result +end diff --git a/entities/player.lua b/entities/player.lua index ee10621..a60cbb5 100644 --- a/entities/player.lua +++ b/entities/player.lua @@ -28,6 +28,7 @@ function Player:new(x, y, playableArea) --(left, right, top, bottom) self.bounds = { (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 = {} end @@ -61,7 +62,7 @@ function Player:update(dt, enemies) if v.destroy == true then table.remove(playerBullets, i) - print("Bullet Destroyed! Bullets in Table: ", #playerBullets) + --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 diff --git a/main.lua b/main.lua index 25929e3..d13239f 100644 --- a/main.lua +++ b/main.lua @@ -10,12 +10,13 @@ function love.load() local window_height = love.graphics.getHeight() local bounds = ui:returnBounds() + 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) listOfEnemies = {} - table.insert(listOfEnemies, Enemy(bounds[1] + 20, 100, 70, bounds)) + table.insert(listOfEnemies, Enemy(bounds[1] + 50, 100, 70, bounds)) --set background for space! love.graphics.setBackgroundColor(27 / 255, 51 / 255, 85 / 255)