enemy.lua code
Code: Select all
--Makes enemy a class
Enemy = Object.extend(Object)
--calling enemy() plays this function
function Enemy.new(self)
self.x = 0
self.y = math.random(0, screeny)
self.width = math.random(5,10)
self.height = math.random(5,10)
self.speed = math.random(50,100)
self.health = math.random(1,50)
self.alive = true
self.check = false
self.path1 = math.random(100,200)
self.path2 = math.random(0, screenx)
end
function Enemy.update(self, dt)
if self.x < self.path1 then
self.x = self.x + self.speed * dt
end
if self.y < self.path2 and self.x >= self.path1 then
self.y = self.y + self.speed * dt
end
if self.y > self.path2 and self.x >= self.path1 then
self.y = self.y - self.speed * dt
end
if self.x >= self.path1 and self.x < screenx + 10 then
self.x = self.x + self.speed * dt
end
if self.x > screenx then
self.alive = false
end
end
function Enemy.draw(self)
love.graphics.rectangle("line", self.x, self.y, self.width, self.height)
end
main.lua relevant code
Code: Select all
function love.update(dt)
Timer.update(dt)
player:update(dt)
--bullet update
for i,v in ipairs(listOfBullets) do
v:update(dt)
if not v.alive then
--Remove it from the list
table.remove(listOfEnemies, i)
end
end
--enemy update
for i,v in ipairs(listOfEnemies) do
v:update(dt)
end
for i,v in ipairs(listOfBullets) do
for i2, v2 in ipairs(listOfEnemies) do
BuEn = checkCollision(v.x,v.y,2,2,v2.x,v2.y,v2.width,v2.height)
if BuEn == true then
BuEn = false
table.remove(listOfBullets, v)
table.remove(listOfEnemies, v2)
end
end
end
end
function love.keypressed(key)
if key == "e" then
--Put a new instance of Bullet inside listOfBullets.
table.insert(listOfBullets, Bullet(player.x, player.y))
end
end