In order to implement my code, Your code needed to be adapted...
- remEnemy = {} needed to be moved to the beginning of the file, so you can use it
also you can not use #enemies any more, so you need to count the enemies manually...
Code: Select all
foe = {}
bullets = {}
remEnemy = {}
function trigger()
spawn = math.random(1,5)
end
function brigger()
spawnboss = math.random(1,6)
end
function myswerve()
swerve = math.random(5)
end
dofire = 0
function firing()
dofire = 1
end
bosshealth = 100
function foe.load()
cron.every (1, trigger)
cron.every (3, brigger)
cron.every (4, myswerve)
cron.every (3, firing)
swerve = 3
spawn = 0
spawnboss = 0
respawn = 0
reinforce = 0
shootn = 0
wolv = love.graphics.newImage('shmuppics/wolverine.png')
bdgr = love.graphics.newImage('shmuppics/honeybadger.png')
hover = love.graphics.newImage('shmuppics/hoverguard.png')
torpido = love.graphics.newImage('shmuppics/bullet_1.png')
bossgun_1 = love.graphics.newImage('shmuppics/bossgun_1.png')
bossgun_2 = love.graphics.newImage('shmuppics/bossgun_3.png')
bossgun_3 = love.graphics.newImage('shmuppics/bossgun_3.png')
lazor = love.graphics.newImage('shmuppics/lazor.png')
cluster = love.graphics.newImage('shmuppics/cluster.png')
volley = love.graphics.newImage('shmuppics/volley.png')
volley2 = love.graphics.newImage('shmuppics/volley.png')
blast = love.graphics.newImage('shmuppics/explosion.png')
scrap1 = love.graphics.newImage('shmuppics/scrap1.png')
scrap2 = love.graphics.newImage('shmuppics/scrap2.png')
scrap3 = love.graphics.newImage('shmuppics/scrap3.png')
scrap4 = love.graphics.newImage('shmuppics/scrap4.png')
darkship_up = love.graphics.newImage('shmuppics/darkship_up.png')
darkship_down = love.graphics.newImage('shmuppics/darkship_down.png')
darkship_left = love.graphics.newImage('shmuppics/darkship_left.png')
darkship_right = love.graphics.newImage('shmuppics/darkship_right.png')
darkship_left_super = love.graphics.newImage('shmuppics/darkship_left_super.png')
darkship_right_super = love.graphics.newImage('shmuppics/darkship_right_super.png')
bosshp = love.graphics.newImage('shmuppics/bosshp.png')
redship = love.graphics.newImage('shmuppics/redship.png')
blueship = love.graphics.newImage('shmuppics/blueship.png')
warlord = love.graphics.newImage('shmuppics/warlord.png')
swarlord = love.graphics.newImage('shmuppics/swarlord.png')
null = love.graphics.newImage('shmuppics/null.png')
glowlord = newAnimation(warlord, 136, 131, 0.2, 0)
glowlord:setMode("bounce")
enemies = {}
bosses = {}
for i=0,2 do
enemy = {}
enemy.img = wolv
enemy.width = 50
enemy.height = 20
enemy.x = i * (enemy.width + 100) + 100
enemy.y = i * (enemy.height) - 400
table.insert(enemies, enemy)
end
end
function foe.update(dt)
glowlord:update(dt)
if bosshealth < 1 then
bosshealth = 0
end
for _, bullet in ipairs(bullets) do
if CheckCollision(player.x, player.y, player.width, player.height, bullet.x, bullet.y, bullet.width, bullet.height) then
playerhealth = playerhealth - 5
end
end
shootn = shootn + 1
if shootn > 50 then
shootn = 0
end
for i,v in ipairs(bullets) do
if v.img == torpido then
v.y = v.y + 700 * dt
elseif v.img == bossgun_1 then
v.y = v.y + 1000 * dt
elseif v.img == bossgun_2 then
v.x = v.x - 800 * dt
elseif v.img == bossgun_3 then
v.x = v.x + 800 * dt
elseif v.img == lazor then
v.y = v.y + 1000 * dt
elseif v.img == cluster then
v.y = v.y - 600 * dt
elseif v.img == volley then
v.x = v.x - 700 * dt
elseif v.img == volley2 then
v.x = v.x + 700 * dt
end
end
remBullet = {}
for i,v in pairs(enemies) do
if v.img == hover then
if v.aim == shootn then
bullet = {}
bullet.img = torpido
bullet.x = v.x + 70
bullet.y = v.y + 50
bullet.width = 10
bullet.height = 10
table.insert(bullets, bullet) -- reset
end
end
if v.img == blueship then
if v.aim == shootn then
bullet = {}
bullet.img = lazor
bullet.x = v.x + 90
bullet.y = v.y + 50
bullet.width = 20
bullet.height = 60
table.insert(bullets, bullet) -- reset
end
end
if v.img == redship then
if v.aim == shootn then
bullet = {}
bullet.img = cluster
bullet.x = v.x + 90
bullet.y = v.y + 50
bullet.width = 1
bullet.height = 1
table.insert(bullets, bullet) -- reset
bullet = {}
bullet.img = cluster
bullet.x = v.x + 25
bullet.y = v.y + 50
bullet.width = 1
bullet.height = 1
table.insert(bullets, bullet)
bullet = {}
bullet.img = cluster
bullet.x = v.x + 50
bullet.y = v.y + 70
bullet.width = 1
bullet.height = 1
table.insert(bullets, bullet)
bullet = {}
bullet.img = cluster
bullet.x = v.x + 75
bullet.y = v.y + 70
bullet.width = 1
bullet.height = 1
table.insert(bullets, bullet)
bullet = {}
bullet.img = cluster
bullet.x = v.x + 100
bullet.y = v.y + 50
bullet.width = 1
bullet.height = 1
table.insert(bullets, bullet)
end
end
if v.img == swarlord then
if v.aim == shootn then
bullet = {}
bullet.img = volley
bullet.x = v.x + 90
bullet.y = v.y + 30
bullet.width = 20
bullet.height = 60
table.insert(bullets, bullet) -- reset
bullet = {}
bullet.img = volley
bullet.x = v.x + 90
bullet.y = v.y + 70
bullet.width = 20
bullet.height = 60
table.insert(bullets, bullet) -- reset
bullet = {}
bullet.img = volley2
bullet.x = v.x + 90
bullet.y = v.y + 30
bullet.width = 20
bullet.height = 60
table.insert(bullets, bullet) -- reset
bullet = {}
bullet.img = volley2
bullet.x = v.x + 90
bullet.y = v.y + 70
bullet.width = 20
bullet.height = 60
table.insert(bullets, bullet) -- reset
end
end
end
for i,v in ipairs(bosses) do
if v.img == darkship_down then
if v.aim == shootn then
bullet = {}
bullet.img = bossgun_1
bullet.x = v.x + 70
bullet.y = v.y + 50
bullet.width = 10
bullet.height = 10
table.insert(bullets, bullet) -- reset
end
end
if v.img == darkship_left then
if v.aim == shootn then
bullet = {}
bullet.img = bossgun_3
bullet.x = v.x + 70
bullet.y = v.y + 70
bullet.width = 50
bullet.height = 50
table.insert(bullets, bullet) -- reset
end
end
if v.img == darkship_right then
if v.aim == shootn then
bullet = {}
bullet.img = bossgun_2
bullet.x = v.x + 70
bullet.y = v.y + 70
bullet.width = 50
bullet.height = 50
table.insert(bullets, bullet) -- reset
end
end
end
if stage == 1 then
if spawn == 1 then
for i=0,4 do
enemy = {}
enemy.img = wolv
enemy.width = 70
enemy.height = 20
enemy.x = i * (enemy.width + 100) + 100
enemy.y = i * (enemy.height) - 400
enemy.aim = 100
enemy.timer = 0
table.insert(enemies, enemy)
spawn = 0
end
end
if spawn == 2 then
for i=0,5 do
enemy = {}
enemy.img = wolv
enemy.width = 70
enemy.height = 20
enemy.x = i * (enemy.width + 200) + 200
enemy.y = i * (enemy.height) - 400
enemy.aim = 100
enemy.timer = 0
table.insert(enemies, enemy)
spawn = 0
end
end
if spawn == 3 then
for i=0,3 do
enemy = {}
enemy.img = wolv
enemy.width = 70
enemy.height = 20
enemy.x = i * (enemy.width + 150) + 100
enemy.y = i * (enemy.height) - 400
enemy.aim = 100
enemy.timer = 0
table.insert(enemies, enemy)
spawn = 0
end
end
if spawn == 4 then
for i=0,1 do
enemy = {}
enemy.img = hover
enemy.width = 70
enemy.height = 20
enemy.x = i * (enemy.width + 100) - 300
enemy.y = i * (enemy.height - 70) + 100
enemy.aim = math.random( 1, 49 )
enemy.timer = 0
table.insert(enemies, enemy)
spawn = 0
end
end
if spawn == 5 then
for i=0,1 do
enemy = {}
enemy.img = bdgr
enemy.width = 70
enemy.height = 20
enemy.x = i * (enemy.width) + 800
enemy.y = i * (enemy.height) - 200
enemy.aim = 100
enemy.timer = 0
table.insert(enemies, enemy)
spawn = 0
end
end
end
if stage == 2 then
end
if stage == 3 then
end
if stage == 4 then
if spawnboss == 1 then
for i=0,0 do
boss = {}
boss.img = darkship_right
boss.width = 80
boss.height = 60
boss.x = i * (enemy.width) + 1200
boss.y = i * (enemy.height) + 400 + 50
boss.aim = math.random( 1, 69 )
table.insert(bosses, boss)
spawnboss = 0
end
end
if spawnboss == 2 then
for i=0,0 do
boss = {}
boss.img = darkship_left
boss.width = 80
boss.height = 60
boss.x = i * (enemy.width) - 200
boss.y = i * (enemy.height) + 400 + 50
boss.aim = math.random( 1, 69 )
table.insert(bosses, boss)
spawnboss = 0
end
end
if spawnboss == 3 then
for i=0,0 do
boss = {}
boss.img = darkship_right_super
boss.width = 100
boss.height = 220
boss.x = i * (enemy.width) + 1200
boss.y = i * (enemy.height) + swerve * 150
table.insert(bosses, boss)
spawnboss = 0
end
end
if spawnboss == 4 then
for i=0,0 do
boss = {}
boss.img = darkship_left_super
boss.width = 100
boss.height = 220
boss.x = i * (enemy.width) - 200
boss.y = i * (enemy.height) + swerve * 150
table.insert(bosses, boss)
spawnboss = 0
end
end
if spawnboss == 5 then
for i=0,0 do
boss = {}
boss.img = darkship_up
boss.width = 80
boss.height = 60
boss.x = i * (enemy.width) + 500
boss.y = i * (enemy.height) + 1000
table.insert(bosses, boss)
spawnboss = 0
end
end
if spawnboss == 6 then
for i=0,0 do
boss = {}
boss.img = darkship_down
boss.width = 80
boss.height = 60
boss.x = i * (enemy.width) + 500
boss.y = i * (enemy.height) - 200
boss.aim = math.random( 1, 49 )
table.insert(bosses, boss)
spawnboss = 0
end
end
if spawn == 1 then
for i=0,2 do
enemy = {}
enemy.img = scrap1
enemy.width = 80
enemy.height = 30
enemy.x = i * (enemy.width) + 300 * swerve
enemy.y = i * (enemy.height + 200) - 500
enemy.aim = 100
enemy.timer = 0
table.insert(enemies, enemy)
spawn = 0
swerve = math.random(5)
end
end
if spawn == 2 then
for i=0,1 do
enemy = {}
enemy.img = scrap2
enemy.width = 80
enemy.height = 30
enemy.x = i * (enemy.width) + swerve + 250 * swerve
enemy.y = i * (enemy.height + 300) - 500
enemy.aim = 100
enemy.timer = 0
if #remEnemy == 0 then
table.insert(enemies, enemy)
else
local random = math.random(1,#remEnemy)
enemies[remEnemy[random]] = enemy
table.remove(remEnemy, random)
end
spawn = 0
swerve = math.random(4)
end
end
if spawn == 3 then
for i=0,0 do
enemy = {}
enemy.img = scrap3
enemy.width = 80
enemy.height = 30
enemy.x = i * (enemy.width) + 100 * swerve
enemy.y = i * (enemy.height + 300) - 300
enemy.aim = 100
enemy.timer = 0
if #remEnemy == 0 then
table.insert(enemies, enemy)
else
local random = math.random(1,#remEnemy)
enemies[remEnemy[random]] = enemy
table.remove(remEnemy, random)
end
spawn = 0
swerve = math.random (3,5)
end
end
if spawn == 4 then
for i=0,0 do
enemy = {}
enemy.img = scrap4
enemy.width = 80
enemy.height = 30
enemy.x = i * (enemy.width + 100 * swerve) + 600
enemy.y = i * (enemy.height + 200) - 300
enemy.aim = 100
enemy.timer = 0
if #remEnemy == 0 then
table.insert(enemies, enemy)
else
local random = math.random(1,#remEnemy)
enemies[remEnemy[random]] = enemy
table.remove(remEnemy, random)
end
spawn = 0
swerve = math.random (2,4)
end
end
end
if stage == 5 then
if spawn == 1 and # enemies < 3 then
for i=0,0 do
enemy = {}
enemy.img = blueship
enemy.width = 80
enemy.height = 50
enemy.x = i * (enemy.width + 100) - 100
enemy.y = i
enemy.aim = math.random(30,49)
enemy.timer = 0
if #remEnemy == 0 then
table.insert(enemies, enemy)
else
local random = math.random(1,#remEnemy)
enemies[remEnemy[random]] = enemy
table.remove(remEnemy, random)
end
spawn = 0
end
end
if spawn == 2 then
for i=0,0 do
enemy = {}
enemy.img = redship
enemy.width = 80
enemy.height = 50
enemy.x = i * (enemy.width + 100) + 600
enemy.y = i - 100
enemy.aim = math.random(30,49)
enemy.timer = 0
if #remEnemy == 0 then
table.insert(enemies, enemy)
else
local random = math.random(1,#remEnemy)
enemies[remEnemy[random]] = enemy
table.remove(remEnemy, random)
end
spawn = 0
end
end
if spawn == 3 then
for i=0,3 do
enemy = {}
enemy.img = wolv
enemy.width = 70
enemy.height = 20
enemy.x = i * (enemy.width + 150) + 100
enemy.y = i * (enemy.height) - 400
enemy.aim = 100
enemy.timer = 0
if #remEnemy == 0 then
table.insert(enemies, enemy)
else
local random = math.random(1,#remEnemy)
enemies[remEnemy[random]] = enemy
table.remove(remEnemy, random)
end
spawn = 0
end
end
if spawn == 4 then
for i=0,0 do
enemy = {}
enemy.img = swarlord
enemy.width = 80
enemy.height = 50
enemy.x = i * (enemy.width + 100) + 600
enemy.y = i - 100
enemy.aim = math.random(30,50)
enemy.timer = 0
if #remEnemy == 0 then
table.insert(enemies, enemy)
else
local random = math.random(1,#remEnemy)
enemies[remEnemy[random]] = enemy
table.remove(remEnemy, random)
end
spawn = 0
end
end
end
remBoss = {}
if gamestate == "playing" or "winstage" then
for i, v in ipairs(bullets) do
if v.y > 920 then
table.insert(remBullet, i)
end
end
for i, v in ipairs(remBullet) do
table.remove(bullet, i)
end
for i,v in pairs(enemies) do
if v.img == wolv then
v.y = v.y + dt * 300
v.x = v.x + math.cos(game.clock) * swerve
elseif v.img == hover then
v.x = v.x + dt * 300
elseif v.img == bdgr then
v.x = v.x - dt * 500
v.y = v.y + dt * 500
elseif v.img == torpido then
v.y = v.y + dt * 800
elseif v.img == scrap1 then
v.x = v.x - dt * 15
v.y = v.y + dt * 100
elseif v.img == scrap2 then
v.x = v.x + dt * 5
v.y = v.y + dt * 250
elseif v.img == scrap3 then
v.x = v.x + dt * 30
v.y = v.y + dt * 30
elseif v.img == scrap4 then
v.x = v.x - dt * 50
v.x = v.x + dt * 10
elseif v.img == blueship then
if v.x > player.x then
v.x = v.x - dt * 30 * swerve
elseif v.x < player.x then
v.x = v.x + dt * 30 * swerve
end
elseif v.img == redship then
if v.x > player.x then
v.y = v.y + dt * 500
v.x = v.x + dt * 100
elseif v.x < player.x then
v.y = v.y + dt * 400
v.x = v.x - dt * 100
end
elseif v.img == swarlord then
v.y = v.y + dt * 500
end
end
for i,v in pairs(enemies) do
--[[if v.y > 920 then
enemies[i] = nil
table.insert(remEnemy, i)
end
if v.x > 1400 then
enemies[i] = nil
table.insert(remEnemy, i)
end
if v.x < -300 then
enemies[i] = nil
table.insert(remEnemy, i)
end ]]
if v.y > love.graphics.getHeight()
- 200 then
-- just a test, You may use the code above
enemies[i] = nil
table.insert(remEnemy, i)
end
end
for i,v in ipairs(bosses) do
if v.img == darkship_right then
v.y = v.y + math.cos(game.clock) * swerve
v.x = v.x - dt * 500
elseif v.img == darkship_left then
v.y = v.y + math.cos(game.clock) * swerve
v.x = v.x + dt * 500
elseif v.img == darkship_left_super then
v.x = v.x + dt * 600
elseif v.img == darkship_right_super then
v.x = v.x - dt * 600
elseif v.img == darkship_up then
v.y = v.y - dt * 500
v.x = v.x + math.cos(game.clock) * swerve
elseif v.img == darkship_down then
v.y = v.y + dt * 500
end
if v.y < 400 then
table.insert(remBoss, i)
end
if v.y > 920 then
table.insert(remBoss, i)
end
if v.x > 1400 then
table.insert(remBoss, i)
end
if v.x < -300 then
table.insert(remBoss, i)
end
for i, v in ipairs(remBoss) do
table.remove(boss, v)
end
end
end
end
function foe.draw()
love.graphics.setColor(255,100,100,200)
love.graphics.rectangle("fill", 980, 110, 20, bosshealth * 6)
love.graphics.setColor(255,255,255,255)
love.graphics.draw(bosshp, 973, 102)
if gamestate == 'playing' then
for i,v in ipairs(bullets) do
love.graphics.draw (v.img, v.x, v.y)
end
end
for i,v in pairs(enemies) do
love.graphics.draw (v.img, v.x, v.y)
end
if gamestate == 'playing' then
for i,v in ipairs(bosses) do
love.graphics.draw (v.img, v.x, v.y)
end
end
end