Sorry, this isn't strictly a Sparrow-related question, but nowhere else have a found such a friendly forum!
Previously my code for adding enemies was an array full of all the possible enemies in a level and I looped through them and added them to the level one by one. There were issues with the time between enemies though, not to mention having one big loop wasn't easy for other methods to work with. I think this was the approach from the Balloon Tutorial. Last night I amended my code to separate my code so that adding a new enemy and looping over all enemies are now seperate. I've also moved from using an array to using a sprite with child objects.
The benefit of the previous approach, however, was that if I wanted 50% of the enemy types for a given level to be enemy type x, and the other 50% enemy type y, I just put half and half in the array. For other percentages, again I just adjusted the amounts of enemies. Not the most streamlined approach, but it worked.
I want to know if there's an easier way though, especially since my code is now different. Essentially I want to be able to specify a likelihood percentage for each bug type in a given level, then, when adding a new enemy, calculate a random value and add the appropriate enemy type depending on which range it falls in. My first attempt at this is shown below, but there are issues with my IF statement since as soon as one condition is met, it's using that enemy type, regardless of whether there's a lower probability for another enemy type so it's not really using ranges. I could add comparison statements (&&) but there's no guarantee enemy type x will always have a higher probability than enemy type y. I want to be able to change it to suit each level.
-(void) setupLevel:(int) x
{
currentLevel = x;
switch(currentLevel) {
case 0:
flyLikelihood = 100;
ladybirdLikelihood = 0;
waspLikelihood = 0;
break;
case 1:
flyLikelihood = 50;
ladybirdLikelihood = 100;
waspLikelihood = 30;
break;
default:
flyLikelihood = 100;
ladybirdLikelihood = 0;
waspLikelihood = 0;
}
}
-(void) dispatchBug
{
int roll = (arc4random() % (int)(100));
NSLog(@"Roll: %d", roll);
SPSprite *thisBug;
// determine the type of bug to be added
if(roll <= flyLikelihood){
thisBug = [[[Bug alloc] init] autorelease];
} else if(roll <= ladybirdLikelihood){
thisBug = [[[LadyBird alloc] init] autorelease];
} else if(roll <= waspLikelihood){
thisBug = [[[Wasp alloc] init] autorelease];
}
int randomYInt = [SPUtils randomIntBetween:90 and:270];
thisBug.x = 480;
thisBug.y = randomYInt;
[playFieldSprite addChild:thisBug];
NSLog(@"BUG ADDED");
enemyDispatched = FALSE;
}
I've been looking into loot drop percentage tables but i'm just confusing myself more to be honest. If anyone can give me any pointers, i'd appreciate it.