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/ClassicGameMode.m | 71 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 15 deletions(-) (limited to 'Classes/ClassicGameMode.m') diff --git a/Classes/ClassicGameMode.m b/Classes/ClassicGameMode.m index 46ad337..306c1d1 100755 --- a/Classes/ClassicGameMode.m +++ b/Classes/ClassicGameMode.m @@ -8,10 +8,6 @@ #import "ClassicGameMode.h" #import "FallingObject.h" -#import "Cherry.h" -#import "Bottle.h" -#import "OneUp.h" -#import "Rock.h" #import "GameOverScene.h" #import "SimpleAudioEngine.h" #import "CCNotifications.h" @@ -19,6 +15,13 @@ @implementation ClassicGameMode +typedef enum { + kCherryObject = 0, + kEnergyDrinkObject, + kOneUpObject, + kRockObject +} FallingObjects; + - (void)tick:(ccTime)dt { int lastScore = score; @@ -83,6 +86,7 @@ { FallingObject* object; int oneuppercent = 98 - (lives == 1 ? 1 : 0); + int recipeIdentifier; if (score < 1000) { @@ -90,31 +94,33 @@ if (randomval < 65) { - object = [[Cherry alloc] init]; + recipeIdentifier = kCherryObject; } else if (randomval < oneuppercent) { - object = [[Bottle alloc] init]; + recipeIdentifier = kEnergyDrinkObject; } else { - object = [[OneUp alloc] init]; + recipeIdentifier = kOneUpObject; } } else { int randomval = arc4random()%100; if (randomval < 40) { - object = [[Cherry alloc] init]; + recipeIdentifier = kCherryObject; } else if (randomval < 70) { - object = [[Rock alloc] init]; + recipeIdentifier = kRockObject; } else if (randomval < oneuppercent) { - object = [[Bottle alloc] init]; + recipeIdentifier = kEnergyDrinkObject; } else { - object = [[OneUp alloc] init]; + recipeIdentifier = kOneUpObject; } } int objectX = arc4random()%448+16; + object = [[objectFactory buildFallingObjectWithRecipeIdentifier:recipeIdentifier] retain]; + object.delegate = self; object.sprite.position = ccp(objectX, 360); object.sprite.scale = 1; [self addChild:object.sprite]; @@ -126,8 +132,8 @@ { if (arc4random() % 100 > 80) { - object = [[Rock alloc] init]; - + object = [[objectFactory buildFallingObjectWithRecipeIdentifier:kRockObject] retain]; + object.delegate = self; objectX = arc4random()%448+16; object.sprite.position = ccp(objectX, 360); object.sprite.scale = 1; @@ -142,8 +148,8 @@ { if (arc4random() % 100 > 80) { - object = [[Rock alloc] init]; - + object = [[objectFactory buildFallingObjectWithRecipeIdentifier:kRockObject] retain]; + object.delegate = self; objectX = arc4random()%448+16; object.sprite.position = ccp(objectX, 360); object.sprite.scale = 1; @@ -166,6 +172,11 @@ [self addChild:backgroundImage z:-1]; addSpeed = 2.5f; + + [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; @@ -178,4 +189,34 @@ [self schedule:@selector(randomlyAddObject:) interval:addSpeed]; } +- (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--; + } +} + +- (void)didMissItem:(FallingObject *)item +{ + if ((item.objectType == kCherryObject) || (item.objectType == kEnergyDrinkObject)) + { + [[SimpleAudioEngine sharedEngine] playEffect:[[NSBundle mainBundle] pathForResource:@"Damage1" ofType:@"wav"]]; + self.lives--; + } +} + @end -- cgit 1.4.1