Browse Source

Why did I do this?

master
NCLanceman 7 months ago
parent
commit
87f53c0579
  1. 33
      entities/enemy.lua
  2. 3
      entities/player.lua
  3. 3
      main.lua

33
entities/enemy.lua

@ -13,8 +13,6 @@ function Enemy:new(x, y, radius, playableArea)
self.x = x self.x = x
self.y = y self.y = y
self.width = self.image:getWidth()
self.height = self.image:getHeight()
self.playableLeft = playableArea[1] self.playableLeft = playableArea[1]
self.playableRight = playableArea[2] self.playableRight = playableArea[2]
@ -26,6 +24,8 @@ function Enemy:new(x, y, radius, playableArea)
self.destroy = false self.destroy = false
self.hitbox = { (self.x + 18), (self.x + 44), (self.y + 20), (self.y + 44) } 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 = {} enemyBullets = {}
end end
@ -41,16 +41,19 @@ function Enemy:draw()
end end
function Enemy:update(dt, player) 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() local window_height = love.graphics.getHeight()
self.x = self.x + self.speed * dt self.x = self.x + self.speed * dt
self.hitbox = { (self.x + 18), (self.x + 44), (self.y + 20), (self.y + 44) } self.hitbox = { (self.x + 18), (self.x + 44), (self.y + 20), (self.y + 44) }
--movement --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 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 self.speed = -self.speed
end end
@ -65,7 +68,7 @@ function Enemy:update(dt, player)
end end
if v.destroy == true then 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) table.remove(enemyBullets, i)
end end
@ -84,3 +87,19 @@ function Enemy:fire()
fireTime = love.timer.getTime() fireTime = love.timer.getTime()
end end
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

3
entities/player.lua

@ -28,6 +28,7 @@ function Player:new(x, y, playableArea)
--(left, right, top, bottom) --(left, right, top, bottom)
self.bounds = { (self.x + 8), (self.x + 38), (self.y + 10), (self.y + 36) } 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 = {} playerBullets = {}
end end
@ -61,7 +62,7 @@ function Player:update(dt, enemies)
if v.destroy == true then if v.destroy == true then
table.remove(playerBullets, i) table.remove(playerBullets, i)
print("Bullet Destroyed! Bullets in Table: ", #playerBullets) --print("Bullet Destroyed! Bullets in Table: ", #playerBullets)
end end
--recieve the list of enemies and check to see if the bullets hit --recieve the list of enemies and check to see if the bullets hit
for _, j in ipairs(enemies) do for _, j in ipairs(enemies) do

3
main.lua

@ -10,12 +10,13 @@ function love.load()
local window_height = love.graphics.getHeight() local window_height = love.graphics.getHeight()
local bounds = ui:returnBounds() local bounds = ui:returnBounds()
print("Playable Bounds: ", bounds[1], " , ", bounds[2])
--place the player in the middle of the screen, close to the bottom --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] + 20, 100, 70, bounds)) table.insert(listOfEnemies, Enemy(bounds[1] + 50, 100, 70, bounds))
--set background for space! --set background for space!
love.graphics.setBackgroundColor(27 / 255, 51 / 255, 85 / 255) love.graphics.setBackgroundColor(27 / 255, 51 / 255, 85 / 255)

Loading…
Cancel
Save