A Little Space Game made in Love2D
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
3.5 KiB

--! file enemy.lua
require("blaster")
Enemy = Object:extend()
--int x, y, radius
--playableArea -> {int[2]}
function Enemy:new(x, y, radius, playableArea)
self.origin = { x, y }
self.image = love.graphics.newImage("/assets/enemy/e-fighter.png")
self.x = x
self.y = y
self.playableLeft = playableArea[1]
self.playableRight = playableArea[2]
self.type = "enemy"
self.health = 10
self.move_radius = radius
self.speed = 80
self.fireRate = 1
self.destroy = false
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])
self.fireTime = love.timer.getTime()
self.enemyBullets = {}
end
function Enemy:draw()
--draw stuff
love.graphics.rectangle("line", self.hitbox[1], self.hitbox[3], self.width, self.height)
--TODO: Resolve this magic number bullshit on line 52. (Why do I subtract 4?)
love.graphics.draw(
self.image,
self.x,
self.y,
3.14159,
1,
1,
(self.x_offset + self.width * 2 - 4),
(self.y_offset + self.height * 2)
)
for _, v in ipairs(self.enemyBullets) do
v:draw()
end
end
function Enemy:update(dt, player)
-- 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) }
self:setHitbox()
--movement
if self.hitbox[1] < self.move_bounds[1] then
--self.x = self.move_bounds[1]
self.speed = -self.speed
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
--firing!
self:fire()
for i, v in ipairs(self.enemyBullets) do
v:update(dt)
if v.y > window_height then
v.destroy = true
end
if v.destroy == true then
--print("Enemy Bullet Removed! Bullets in table remaining: ", #enemyBullets)
table.remove(self.enemyBullets, i)
end
v:checkCollision(player)
end
--health check
if self.health < 0 then
self.destroy = true
end
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 + (self.width / 2)) + self.x_offset), (self.y + self.height + self.y_offset) }
if love.timer.getTime() - self.fireTime > self.fireRate then
table.insert(self.enemyBullets, Blaster(fire_origin[1], fire_origin[2], "enemy"))
self.fireTime = love.timer.getTime()
end
end
function Enemy:setMoveBounds()
local result = {}
if (self.hitbox[1] - self.move_radius) < self.playableLeft then
result[1] = self.playableLeft
--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
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