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/JumpGameMode.m | 58 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 17 deletions(-) (limited to 'Classes/JumpGameMode.m') diff --git a/Classes/JumpGameMode.m b/Classes/JumpGameMode.m index 15b0e16..a5c2b8f 100644 --- a/Classes/JumpGameMode.m +++ b/Classes/JumpGameMode.m @@ -9,12 +9,7 @@ #import "JumpGameMode.h" #import "SimpleAudioEngine.h" #import "FallingObject.h" -#import "Cherry.h" -#import "Bottle.h" -#import "OneUp.h" -#import "Rock.h" #import "GameOverScene.h" -#import "PointMultiplier.h" #define kMinimumGestureLength 25 @@ -34,6 +29,12 @@ @implementation JumpGameMode +typedef enum { + kRockObject = 0, + kOneUpObject, + kPointMultiplierObject +} FallingObjects; + - (id)init { self = [super init]; @@ -57,9 +58,9 @@ waterTick = 0; wave = NO; - factory = [[LedgeFactory alloc] init]; + ledgeFactory = [[LedgeFactory alloc] init]; ledges = [[NSMutableSet alloc] init]; - CCTexture2D* texture = [[CCTexture2D alloc] initWithImage:[factory createLedgeWithWidth:6 height:2]]; + CCTexture2D* texture = [[CCTexture2D alloc] initWithImage:[ledgeFactory createLedgeWithWidth:6 height:2]]; CCSprite* ledge = [CCSprite spriteWithTexture:texture]; ledge.position = ccp(80, 32); [self addChild:ledge]; @@ -67,7 +68,7 @@ [texture release]; CCSprite* firstLedge = ledge; - texture = [[CCTexture2D alloc] initWithImage:[factory createLedgeWithWidth:10 height:2]]; + texture = [[CCTexture2D alloc] initWithImage:[ledgeFactory createLedgeWithWidth:10 height:2]]; ledge = [CCSprite spriteWithTexture:texture]; ledge.position = ccp(80 + firstLedge.boundingBox.size.width/2 + ledge.boundingBox.size.width/2+64, 32); [self addChild:ledge]; @@ -77,6 +78,10 @@ ledgeScrollSpeed = 0; ledgeAccelerationRate = 20.0f; addSpeed = 2.5f; + + [objectFactory createRecipeWithIdentifier:kRockObject spriteFilename:@"rock.png" weight:7]; + [objectFactory createRecipeWithIdentifier:kOneUpObject spriteFilename:@"oneup.png" weight:10]; + [objectFactory createRecipeWithIdentifier:kPointMultiplierObject spriteFilename:@"multiplier.png" weight:8]; } return self; @@ -164,7 +169,7 @@ { int ledgeWidth = arc4random() % 9 + 1; int ledgeDistance = arc4random() % (ledgeScrollSpeed*ledgeScrollSpeed+1)*3/2; - CCTexture2D* texture = [[CCTexture2D alloc] initWithImage:[factory createLedgeWithWidth:ledgeWidth height:2]]; + CCTexture2D* texture = [[CCTexture2D alloc] initWithImage:[ledgeFactory createLedgeWithWidth:ledgeWidth height:2]]; CCSprite* ledge = [CCSprite spriteWithTexture:texture]; ledge.position = ccp(rightmost + rightwidth + ledge.boundingBox.size.width/2+64+ledgeDistance, 32); [self addChild:ledge]; @@ -351,23 +356,22 @@ - (void)randomlyAddObject:(ccTime)dt { - FallingObject* object; - + int recipeIdentifier; int randomval = arc4random()%100; int pmChance = (5-pointMultiplier)*5 + 30; if (randomval < 30) { - object = [[OneUp alloc] init]; + recipeIdentifier = kOneUpObject; } else if (randomval < pmChance) { - object = [[PointMultiplier alloc] init]; + recipeIdentifier = kPointMultiplierObject; } else { - object = [[Rock alloc] init]; + recipeIdentifier = kRockObject; } + FallingObject* object = [[objectFactory buildFallingObjectWithRecipeIdentifier:recipeIdentifier] retain]; int objectX; - CGSize first = [cart.sprite boundingBox].size; CGSize second = [object.sprite boundingBox].size; @@ -391,6 +395,7 @@ break; } + object.delegate = self; object.sprite.position = ccp(objectX, 360); object.sprite.scale = 1; [self addChild:object.sprite]; @@ -402,7 +407,7 @@ { if (arc4random() % 100 > 80) { - object = [[Rock alloc] init]; + object = [[objectFactory buildFallingObjectWithRecipeIdentifier:kRockObject] retain]; for (;;) { @@ -424,6 +429,7 @@ break; } + object.delegate = self; object.sprite.position = ccp(objectX, 360); object.sprite.scale = 1; [self addChild:object.sprite]; @@ -437,7 +443,7 @@ { if (arc4random() % 100 > 80) { - object = [[Rock alloc] init]; + object = [[objectFactory buildFallingObjectWithRecipeIdentifier:kRockObject] retain]; for (;;) { @@ -459,6 +465,7 @@ break; } + object.delegate = self; object.sprite.position = ccp(objectX, 360); object.sprite.scale = 1; [self addChild:object.sprite]; @@ -486,6 +493,23 @@ } } +- (void)didCatchItem:(FallingObject *)item +{ + if (item.objectType == kRockObject) + { + [[SimpleAudioEngine sharedEngine] playEffect:[[NSBundle mainBundle] pathForResource:@"Damage1" ofType:@"wav"]]; + self.lives--; + } else if (item.objectType == kOneUpObject) + { + [[SimpleAudioEngine sharedEngine] playEffect:[[NSBundle mainBundle] pathForResource:@"1up" ofType:@"wav"]]; + self.lives++; + } else if (item.objectType == kPointMultiplierObject) + { + [[SimpleAudioEngine sharedEngine] playEffect:[[NSBundle mainBundle] pathForResource:@"Absorption2" ofType:@"wav"]]; + self.pointMultiplier++; + } +} + @end @implementation LedgeFactory -- cgit 1.4.1