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.
124 lines
3.1 KiB
124 lines
3.1 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.x = x
|
|
self.y = y
|
|
self.playableLeft = playableArea[1]
|
|
self.playableRight = playableArea[2]
|
|
|
|
self.move_radius = radius
|
|
self.destroy = false
|
|
|
|
self.height, self.width, self.x_offset, self.y_offset,
|
|
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
|
|
--for the base class, draw the hitbox
|
|
--if the enemy is destroyed, remove the image and display the score for two seconds
|
|
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:setHitbox()
|
|
|
|
--movement
|
|
if self.hitbox[1] < self.move_bounds[1] then
|
|
self.speed = -self.speed
|
|
elseif self.hitbox[2] > self.move_bounds[2] then
|
|
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 + (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.move_radius * 2)
|
|
elseif (self.hitbox[2] + self.move_radius) > self.playableRight then
|
|
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
|
|
|