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.

105 lines
2.9 KiB

--! file enemy.lua
require("blaster")
Enemy = Object:extend()
local fireTime = love.timer.getTime()
local enemyBullets
--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.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 = {}
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)
for _, v in ipairs(enemyBullets) do
--love.graphics.circle("fill", v.x, v.y, v.radius)
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) }
--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
--firing!
self:fire()
for i, v in ipairs(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(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()
if love.timer.getTime() - fireTime > self.fireRate then
table.insert(enemyBullets, Blaster(self.x, self.y, "enemy"))
fireTime = love.timer.getTime()
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