Browse Source

working on enemy movement

master
NCLanceman 7 months ago
parent
commit
798cc1e2ca
  1. 50
      entities/enemy.lua
  2. 3
      main.lua

50
entities/enemy.lua

@ -23,21 +23,26 @@ function Enemy:new(x, y, radius, playableArea)
self.fireRate = 1
self.destroy = false
self.hitbox = { (self.x + 20), (self.x + 20), (self.y + 20), (self.y + 44) }
self.height = 22
self.width = 24
self.x_offset = 18
self.y_offset = 20
self.hitbox = {
(self.x + self.x_offset),
(self.x + self.x_offset + self.width),
(self.y + self.y_offset),
(self.y + self.y_offset + self.height),
}
self.move_bounds = self:setMoveBounds()
--print("Move Bounds: ", self.move_bounds[1], " , ", self.move_bounds[2])
print("Move Bounds: ", self.move_bounds[1], " , ", self.move_bounds[2])
enemyBullets = {}
end
function Enemy:draw()
--draw stuff
love.graphics.rectangle(
"line",
self.hitbox[1],
self.hitbox[3],
(self.hitbox[2] - self.hitbox[1]),
(self.hitbox[4] - self.hitbox[3])
)
love.graphics.rectangle("line", self.hitbox[1], self.hitbox[3], self.width, self.height)
love.graphics.draw(self.image, self.x, self.y, 0, 1, 1)
for _, v in ipairs(enemyBullets) do
@ -51,12 +56,15 @@ function Enemy:update(dt, player)
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) }
--self.hitbox = { (self.x + 18), (self.x + 44), (self.y + 20), (self.y + 44) }
self:setHitbox()
--movement
if self.x < self.move_bounds[1] then
if self.hitbox[1] < self.move_bounds[1] then
self.x = self.move_bounds[1]
self.speed = -self.speed
elseif self.x > self.move_bounds[2] then
elseif self.hitbox[2] > self.move_bounds[2] then
self.x = self.move_bounds[2] - self.width - self.x_offset
self.speed = -self.speed
end
@ -85,7 +93,8 @@ end
--shoot at the player a variable number of seconds
function Enemy:fire()
local fire_origin = { self.x + 26, self.y + 44 }
--local fire_origin = { self.x + 26, self.y + 44 }
local fire_origin = { (self.x + (self.width / 2)), self.y + self.height }
if love.timer.getTime() - fireTime > self.fireRate then
table.insert(enemyBullets, Blaster(fire_origin[1], fire_origin[2], "enemy"))
fireTime = love.timer.getTime()
@ -97,13 +106,24 @@ function Enemy:setMoveBounds()
if (self.hitbox[1] - self.move_radius) < self.playableLeft then
result[1] = self.playableLeft
result[2] = self.playableLeft + self.hitbox[1] + move_radius
--result[2] = self.playableLeft + self.hitbox[1] + self.move_radius
result[2] = self.playableLeft + (self.move_radius * 2)
elseif (self.hitbox[2] + self.move_radius) > self.playableRight then
--result[1] = self.playableRight - self.hitbox[2] - self.move_radius
result[1] = self.playableRight - (self.move_radius * 2)
result[2] = self.playableRight
result[1] = self.playableRight - self.hitbox[2] - move_radius
else
result[1] = self.x - self.move_radius
result[2] = self.x + self.move_radius
end
return result
end
function Enemy:setHitbox()
self.hitbox = {
(self.x + self.x_offset),
(self.x + self.width + self.x_offset),
(self.y + self.y_offset),
(self.y + self.height + self.y_offset),
}
end

3
main.lua

@ -16,8 +16,9 @@ function love.load()
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))
table.insert(ListOfEnemies, Enemy((bounds[2] - 25), 75, 70, bounds))
--set background for space!
love.graphics.setBackgroundColor(27 / 255, 51 / 255, 85 / 255)

Loading…
Cancel
Save