Created
March 7, 2012 18:26
-
-
Save anonymous/1994943 to your computer and use it in GitHub Desktop.
Enemy horde
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ENEMY_SPAWN_EASY = 0 | |
ENEMY_SPAWN_HARD = 1 | |
EnemyHorde = class() | |
function EnemyHorde:init() | |
-- you can accept and set parameters here | |
self.frame = 0 | |
self.units = {} -- enemy units | |
self.heroBullets = {} -- hero's bullets | |
self.explosions = {} | |
self.enemySize = 100 | |
self.spawnPattern = ENEMY_SPAWN_EASY | |
self.deltaX = 4 | |
self.speedX =12 | |
self.moveDownEvery = 8 | |
offset=8 | |
ystart = HEIGHT | |
for row=1,4 do | |
for col=40+offset,599,self.enemySize + 10 do | |
table.insert(self.units, vec2(col, ystart)) | |
end | |
offset = -offset | |
ystart = ystart - self.enemySize + 10 | |
end | |
end | |
function EnemyHorde:defeated() | |
return self.units.size == 0 | |
end | |
function EnemyHorde:draw() | |
self.frame = (self.frame+1)%128 | |
changeDirectionFrames=128 | |
pushStyle() | |
if self.spawnPattern == ENEMY_SPAWN_EASY then | |
fill(123, 25, 100, 255) | |
else | |
fill(222, 5, 5, 255) | |
end | |
stroke(217, 158, 105, 255) | |
strokeWidth(8) | |
-- Spawn random enemy every 100 frames | |
--if self.frame%100 == 0 then | |
-- spawn = vec2( math.random(WIDTH), HEIGHT + self.enemySize ) | |
-- table.insert( self.units, spawn ) | |
-- end | |
if self.frame%changeDirectionFrames == 0 then | |
self.deltaX = -self.deltaX | |
if self.speedX > 3 then | |
self.speedX = self.speedX - 1 | |
end | |
end | |
for i,v in ipairs(self.units) do | |
-- Move unit down | |
if self.frame%self.moveDownEvery == 0 then | |
v.y = v.y - 1 | |
end | |
if self.frame%self.speedX == 1 then | |
v.x = v.x + self.deltaX | |
end | |
-- If hard, move in sine wave | |
-- if self.spawnPattern == ENEMY_SPAWN_HARD then | |
-- Compute movement vector | |
-- sideMove = vec2( math.sin(v.y * 0.02) * 60, 0 ) | |
-- v = v + sideMove | |
-- end | |
-- Cull unit | |
culled = false | |
if (v.y + self.enemySize) < 0 then | |
state = GAME_DEAD | |
explosion = Explosion(hero.position) | |
table.remove(self.units, i) | |
culled = true -- no continue statement | |
end | |
-- Check if hit by a bullet | |
if culled == false then | |
for j,b in ipairs(self.heroBullets) do | |
if v:dist(b) < self.enemySize/2 then | |
table.remove(self.units, i) | |
table.remove(self.heroBullets, j) | |
-- Explode! | |
table.insert(self.explosions, Explosion(v)) | |
-- Update killCount | |
killCount = killCount + 1 | |
end | |
end | |
end | |
-- Draw unit | |
sprite("Tyrian Remastered:Plane Boss",v.x,v.y) | |
end | |
-- Draw explosions | |
for i,e in ipairs(self.explosions) do | |
e:draw() | |
if e:isDone() then | |
table.remove(self.explosions,i) | |
end | |
end | |
popStyle() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment