From fd58a0cde1bb5473e39e6cb82d28113da84b9ae0 Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Sat, 10 Sep 2011 17:07:13 -0400 Subject: Reworked falling objects Previously, every type of falling object had to have its own class that defined the object type's sprite, weight, and reaction to the cart/floor. This was pretty messy considering how many object types may only be used in one game mode--for instance, the many power ups in Power mode, once it's created, will never be used outside of Power mode. So, to increase customizability and decrease class clutter, game modes now use a FallingObjectFactory to define recipes (basically a sprite filename, a weight and an identifier) that can easily be built throughout the game mode using the identifier. FallingObjectDelegate is now used for all reactions to the cart/floor, rather than defining a standard reaction in the FallingObject subclass and then putting extra stuff in FallingObjectDelegate. --- Classes/TutorialMode.m | 90 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 60 insertions(+), 30 deletions(-) (limited to 'Classes/TutorialMode.m') diff --git a/Classes/TutorialMode.m b/Classes/TutorialMode.m index 3c70a46..156b939 100644 --- a/Classes/TutorialMode.m +++ b/Classes/TutorialMode.m @@ -8,11 +8,8 @@ #import "TutorialMode.h" #import "FallingObject.h" -#import "Cherry.h" -#import "Bottle.h" -#import "OneUp.h" -#import "Rock.h" #import "GameModeSelectionLayer.h" +#import "SimpleAudioEngine.h" // Item tags: // 2000 - first dropped item @@ -22,6 +19,13 @@ // 2009 - rock // 2010 - random item dropped after death from first rock +typedef enum { + kCherryObject = 0, + kEnergyDrinkObject, + kOneUpObject, + kRockObject +} FallingObjects; + @implementation TutorialMode @synthesize currentTutorial; @@ -38,6 +42,11 @@ showedDeathBubble = NO; randomItemsDropped = 0; + + [objectFactory createRecipeWithIdentifier:kCherryObject spriteFilename:@"cherry.png" weight:5]; + [objectFactory createRecipeWithIdentifier:kEnergyDrinkObject spriteFilename:@"bottle.png" weight:6]; + [objectFactory createRecipeWithIdentifier:kOneUpObject spriteFilename:@"oneup.png" weight:10]; + [objectFactory createRecipeWithIdentifier:kRockObject spriteFilename:@"rock.png" weight:7]; } return self; @@ -79,6 +88,24 @@ - (void)didCatchItem:(FallingObject *)item { + if (item.objectType == kCherryObject) + { + [[SimpleAudioEngine sharedEngine] playEffect:[[NSBundle mainBundle] pathForResource:@"Item1" ofType:@"wav"]]; + self.score += 10; + } else if (item.objectType == kEnergyDrinkObject) + { + [[SimpleAudioEngine sharedEngine] playEffect:[[NSBundle mainBundle] pathForResource:@"Item1" ofType:@"wav"]]; + self.score += 25; + } else if (item.objectType == kOneUpObject) + { + [[SimpleAudioEngine sharedEngine] playEffect:[[NSBundle mainBundle] pathForResource:@"1up" ofType:@"wav"]]; + self.lives++; + } else if (item.objectType == kRockObject) + { + [[SimpleAudioEngine sharedEngine] playEffect:[[NSBundle mainBundle] pathForResource:@"Damage1" ofType:@"wav"]]; + self.lives--; + } + if (item.sprite.tag == 2000) { TutorialBubble* bubble = [[TutorialBubble alloc] initWithText:@"Congratulations! If you look at your score, you'll see it increased. Catching items is good. Now, let's see what happens when you don't catch an item." name:@"caught-first"]; @@ -94,6 +121,12 @@ - (void)didMissItem:(FallingObject *)item { + if ((item.objectType == kCherryObject) || (item.objectType == kEnergyDrinkObject)) + { + [[SimpleAudioEngine sharedEngine] playEffect:[[NSBundle mainBundle] pathForResource:@"Damage1" ofType:@"wav"]]; + self.lives--; + } + if (item.sprite.tag == 2000) { TutorialBubble* bubble = [[TutorialBubble alloc] initWithText:@"Whoops, you missed it! Look at your lives counter--you lost one! If you lose all of your lives, you lose the game. Try catching the item again." name:@"missed-first"]; @@ -111,7 +144,6 @@ [self scheduleDelayedAction:^{ FallingObject* object = [self dropRandomItem]; object.sprite.tag = 2002; - [object release]; } delay:1.0f]; } } @@ -181,7 +213,6 @@ [self scheduleDelayedAction:^{ FallingObject* object = [self dropRandomItem]; object.sprite.tag = 2000; - [object release]; } delay:3.0f]; } else if ([currentTutorial.name isEqual:@"caught-first"]) { @@ -198,29 +229,24 @@ } object.sprite.tag = 2001; - - [object release]; } delay:1.0f]; } else if ([currentTutorial.name isEqual:@"missed-first"]) { [self scheduleDelayedAction:^{ FallingObject* object = [self dropRandomItem]; object.sprite.tag = 2002; - [object release]; } delay:1.0f]; } else if (([currentTutorial.name isEqual:@"caught-second"]) || ([currentTutorial.name isEqual:@"missed-second"])) { [self scheduleDelayedAction:^{ - FallingObject* object = [self dropSpecificItem:[[OneUp alloc] init]]; + FallingObject* object = [self dropSpecificItem:kOneUpObject]; object.sprite.tag = 2003; - [object release]; } delay:2.0f]; } else if ([currentTutorial.name isEqual:@"gameover-rock"]) { [self scheduleDelayedAction:^{ FallingObject* object = [self dropRandomItem]; object.sprite.tag = 2010; - [object release]; } delay:1.0f]; } else if ([currentTutorial.name isEqual:@"intense"]) { @@ -254,8 +280,9 @@ } } -- (FallingObject*)dropSpecificItem:(FallingObject*)object +- (FallingObject*)dropSpecificItem:(int)objectType { + FallingObject* object = [[objectFactory buildFallingObjectWithRecipeIdentifier:objectType] retain]; int objectX = arc4random()%448+16; object.sprite.position = ccp(objectX, 360); object.sprite.scale = 1; @@ -265,27 +292,27 @@ [objects addObject:object]; - return object; + return [object autorelease]; } - (FallingObject*)dropRandomItem { - FallingObject* object; int randomval = arc4random()%100; + int recipeIdentifier; if (randomval < 65) { - object = [[Cherry alloc] init]; + recipeIdentifier = kCherryObject; } else { - object = [[Bottle alloc] init]; + recipeIdentifier = kEnergyDrinkObject; } - return [self dropSpecificItem:object]; + return [self dropSpecificItem:recipeIdentifier]; } - (void)randomlyAddObject:(ccTime)dt { - FallingObject* object; + int recipeIdentifier; if (randomItemsDropped < 5) { @@ -293,17 +320,16 @@ if (randomval < 65) { - object = [[Cherry alloc] init]; + recipeIdentifier = kCherryObject; } else if (randomval < 98) { - object = [[Bottle alloc] init]; + recipeIdentifier = kEnergyDrinkObject; } else { - object = [[OneUp alloc] init]; + recipeIdentifier = kOneUpObject; } } else if (randomItemsDropped == 5) { - object = [[Rock alloc] init]; - object.sprite.tag = 2009; + recipeIdentifier = kRockObject; [self unschedule:@selector(randomlyAddObject:)]; } else if (randomItemsDropped < 15) { @@ -311,15 +337,15 @@ if (randomval < 40) { - object = [[Cherry alloc] init]; + recipeIdentifier = kCherryObject; } else if (randomval < 70) { - object = [[Rock alloc] init]; + recipeIdentifier = kRockObject; } else if (randomval < 98) { - object = [[Bottle alloc] init]; + recipeIdentifier = kEnergyDrinkObject; } else { - object = [[OneUp alloc] init]; + recipeIdentifier = kOneUpObject; } } else if (randomItemsDropped == 15) { @@ -341,8 +367,12 @@ return; } - [self dropSpecificItem:object]; - [object release]; + FallingObject* object = [self dropSpecificItem:recipeIdentifier]; + + if (randomItemsDropped == 5) + { + object.sprite.tag = 2009; + } randomItemsDropped++; } -- cgit 1.4.1