Browse Source

Corrected enemy hitbox / sprite draw

master
NCLanceman 7 months ago
parent
commit
9530402ae9
  1. 22
      blaster.lua
  2. 36
      entities/enemy.lua
  3. 6
      entities/player.lua
  4. 23
      main.lua

22
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

36
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

6
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

23
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

Loading…
Cancel
Save