Skip to content

Instantly share code, notes, and snippets.

@totty90
Created January 8, 2014 00:50
Show Gist options
  • Save totty90/8309720 to your computer and use it in GitHub Desktop.
Save totty90/8309720 to your computer and use it in GitHub Desktop.
Games: generate units based on money and unit number limitation; Most elegant way to generate units for a game. Just say how many units you want, how much money (in this case hardness) you want to spend and then specify in each unit how much money (hardness) cost. This will create an array of 50 units with all the money you allow him to spend. I…
// limit max unit per wave to 50
// begin by creating the cheapest units, when it reach 50 units and you still have hardness to spend
// then remove the first in the array and build a harder one. When all of the units are of one type it migrates
// to the other level.
var toSpawn = [];
var l = 0;
var max = 50;
var currentLevel = 0;
while(remainingHardness > 0){
if(l < max){
var unit = this.__availableUnits[currentLevel];
if(!unit){break}
var haveHardness = remainingHardness >= unit.hardness;
if(haveHardness){
toSpawn.push(unit);
l++;
remainingHardness -= unit.hardness;
}else{
// exit keeping a hardness rest
break;
}
}else{
l--;
currentLevel++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment