Skip to content

Instantly share code, notes, and snippets.

@sr3d
Created October 14, 2009 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sr3d/210190 to your computer and use it in GitHub Desktop.
Save sr3d/210190 to your computer and use it in GitHub Desktop.
var gameTrigger = [
{
when: 2
,delayMin: 4 /* time between creep spawns */
,delayMax: 5
,multiplier: 1
,creeps: [
{ count: 5, type: Soot }
,{ count: 5, type: SootLevel2 }
,{ count: 4, type: Soot }
,{ count: 1, type: SootLevel2 } // can be boss as well
,{ count: 4, type: Soot }
,{ count: 3, type: SootLevel2 }
,{ count: 4, type: Soot }
,{ count: 3, type: Soot }
,{ count: 3, type: SootLevel2 }
,{ count: 2, type: Soot }
]
}
/* tripple closure! */
function() {
// ....
window.currentWave = 0;
var wave = window.gameTrigger[ window.currentWave ];
setTimeout( function() {
var currentTimeout = 0;
for( i = 0; i < wave.creeps.length; i++ )
{
currentTimeout += Math.random() * ( wave.delayMax - wave.delayMin ) + wave.delayMin;
/* trigger sub-waves */
setTimeout( function(a) {
/* have to return a new function using the argument so that
the for loop "i" index is evaluated correctly in the nested
setTimeout. Sweet!
*/
return function() {
for( j = 0; j < wave.creeps[ a ].count; j++ )
{
/* pause a bit between each creeps */
setTimeout( function() {
self.addCreep( 0, 0, wave.creeps[ a ].type );
}, ( Math.random() * 5 + 4 ) * 1000);
}
};
}( i ), currentTimeout * 1000 );
}
}, wave.when * 1000 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment