From 002dc99a071a4a8f7d2afc15d1fdf2ae0312faed Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Tue, 9 Aug 2011 21:36:09 -0400 Subject: Abstracted a lot of GameLayer's functionality out MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In order to prepare for tutorial mode (and eventually, the other game modes), a lot of GameLayer's functionality (reading the accelerometer, moving the cart, moving falling objects, reacting to collisions…) has been abstracted out to FallingObject and subclasses of FallingObject, as well as a new Cart class, and GameMode, a new superclass of GameLayer. The only things that were not abstracted out that probably will be in the future are the pause functionality and the tutorial bubble functionality (which will in fact be moved to the tutorial mode class). A lot of work was also done to cut down on compiler warnings and a lot of #imports were moved from header files to implementation files because I realized THAT'S HOW YOU'RE SUPPOSED TO DO IT. Refs #193 --- Cart Collect.xcodeproj/project.pbxproj | 32 ++++++- Classes/Bottle.h | 3 +- Classes/Cart.h | 22 +++++ Classes/Cart.m | 45 ++++++++++ Classes/Cherry.h | 3 +- Classes/FallingObject.h | 3 + Classes/FallingObject.m | 50 ++++++++++- Classes/GameLayer.h | 23 +---- Classes/GameLayer.m | 149 +++++++-------------------------- Classes/GameMode.h | 27 ++++++ Classes/GameMode.m | 78 +++++++++++++++++ Classes/GameOverLayer.h | 5 -- Classes/GameOverLayer.m | 5 +- Classes/Highscore.h | 3 - Classes/Highscore.m | 1 - Classes/HighscoreListController.h | 3 - Classes/HighscoreListController.m | 13 ++- Classes/MainMenuLayer.h | 3 - Classes/MainMenuLayer.m | 7 +- Classes/OneUp.m | 11 ++- Classes/Rock.m | 12 ++- Classes/ValuableObject.h | 8 +- Classes/ValuableObject.m | 46 ++++++++++ 23 files changed, 374 insertions(+), 178 deletions(-) create mode 100644 Classes/Cart.h create mode 100644 Classes/Cart.m create mode 100644 Classes/GameMode.h create mode 100644 Classes/GameMode.m mode change 100755 => 100644 Classes/ValuableObject.h create mode 100644 Classes/ValuableObject.m diff --git a/Cart Collect.xcodeproj/project.pbxproj b/Cart Collect.xcodeproj/project.pbxproj index b3c5dfa..231e838 100755 --- a/Cart Collect.xcodeproj/project.pbxproj +++ b/Cart Collect.xcodeproj/project.pbxproj @@ -84,6 +84,9 @@ 50F4144910692EE7002A0D5E /* Default.png in Resources */ = {isa = PBXBuildFile; fileRef = 50F4144410692EE7002A0D5E /* Default.png */; }; 50F4144A10692EE7002A0D5E /* fps_images.png in Resources */ = {isa = PBXBuildFile; fileRef = 50F4144510692EE7002A0D5E /* fps_images.png */; }; 50F4144B10692EE7002A0D5E /* Icon.png in Resources */ = {isa = PBXBuildFile; fileRef = 50F4144610692EE7002A0D5E /* Icon.png */; }; + 6C0C140E13F1C3EC003A31B2 /* ValuableObject.m in Sources */ = {isa = PBXBuildFile; fileRef = 6C0C140D13F1C3EC003A31B2 /* ValuableObject.m */; }; + 6C0C141113F2099B003A31B2 /* Cart.m in Sources */ = {isa = PBXBuildFile; fileRef = 6C0C141013F2099B003A31B2 /* Cart.m */; }; + 6C0C141413F20E98003A31B2 /* GameMode.m in Sources */ = {isa = PBXBuildFile; fileRef = 6C0C141313F20E98003A31B2 /* GameMode.m */; }; 6C29041013EAEB590032DA0F /* TutorialBubble.m in Sources */ = {isa = PBXBuildFile; fileRef = 6C29040F13EAEB590032DA0F /* TutorialBubble.m */; }; 6C29041213EAEC8A0032DA0F /* framestuff.png in Resources */ = {isa = PBXBuildFile; fileRef = 6C29041113EAEC8A0032DA0F /* framestuff.png */; }; 6C5179C613DF3839006F1F38 /* Morning1.png in Resources */ = {isa = PBXBuildFile; fileRef = 6C5179C513DF3839006F1F38 /* Morning1.png */; }; @@ -328,7 +331,6 @@ 3F7D0F4913D8F17C00B6CE14 /* highscores2.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = highscores2.png; sourceTree = ""; }; 3F7D0F4A13D8F17C00B6CE14 /* newgame.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = newgame.png; sourceTree = ""; }; 3F7D0F4B13D8F17C00B6CE14 /* newgame2.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = newgame2.png; sourceTree = ""; }; - 3F8394AC13D72E2C0059AEE8 /* ValuableObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ValuableObject.h; sourceTree = ""; }; 3F8394F213D732330059AEE8 /* pause.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = pause.png; sourceTree = ""; }; 3F8394F413D7328E0059AEE8 /* pause2.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = pause2.png; sourceTree = ""; }; 3F83955113D739B10059AEE8 /* MainMenuLayer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MainMenuLayer.h; sourceTree = ""; }; @@ -373,6 +375,12 @@ 50F4144510692EE7002A0D5E /* fps_images.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = fps_images.png; sourceTree = ""; }; 50F4144610692EE7002A0D5E /* Icon.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Icon.png; sourceTree = ""; }; 50F4144710692EE7002A0D5E /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 6C0C140C13F1C3EC003A31B2 /* ValuableObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ValuableObject.h; sourceTree = ""; }; + 6C0C140D13F1C3EC003A31B2 /* ValuableObject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ValuableObject.m; sourceTree = ""; }; + 6C0C140F13F2099B003A31B2 /* Cart.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Cart.h; sourceTree = ""; }; + 6C0C141013F2099B003A31B2 /* Cart.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Cart.m; sourceTree = ""; }; + 6C0C141213F20E98003A31B2 /* GameMode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GameMode.h; sourceTree = ""; }; + 6C0C141313F20E98003A31B2 /* GameMode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GameMode.m; sourceTree = ""; }; 6C29040E13EAEB590032DA0F /* TutorialBubble.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TutorialBubble.h; sourceTree = ""; }; 6C29040F13EAEB590032DA0F /* TutorialBubble.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TutorialBubble.m; sourceTree = ""; }; 6C29041113EAEC8A0032DA0F /* framestuff.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = framestuff.png; sourceTree = ""; }; @@ -656,14 +664,13 @@ 2D500B1D0D5A766B00DBA0E3 /* Classes */ = { isa = PBXGroup; children = ( + 6C0C141513F211C7003A31B2 /* Game Modes */, 6C5887E313EE3ED900B5A80A /* Items */, E0F81035120A173C005866B8 /* GameConfig.h */, E0F81036120A173C005866B8 /* RootViewController.h */, E0F81037120A173C005866B8 /* RootViewController.m */, 1F3B9A2C0EF2145700286867 /* Cart_CollectAppDelegate.h */, 507022A2107672FA00393637 /* Cart_CollectAppDelegate.m */, - 3FE79CD213D4DE37001A6B93 /* GameLayer.h */, - 3FE79CD313D4DE37001A6B93 /* GameLayer.m */, 3F6C7EE713D6377D00C038FE /* GameOverLayer.h */, 3F6C7EE813D6377D00C038FE /* GameOverLayer.m */, 3F83955213D739B10059AEE8 /* MainMenuLayer.m */, @@ -821,6 +828,17 @@ path = Resources; sourceTree = ""; }; + 6C0C141513F211C7003A31B2 /* Game Modes */ = { + isa = PBXGroup; + children = ( + 6C0C141213F20E98003A31B2 /* GameMode.h */, + 6C0C141313F20E98003A31B2 /* GameMode.m */, + 3FE79CD213D4DE37001A6B93 /* GameLayer.h */, + 3FE79CD313D4DE37001A6B93 /* GameLayer.m */, + ); + name = "Game Modes"; + sourceTree = ""; + }; 6C5887E313EE3ED900B5A80A /* Items */ = { isa = PBXGroup; children = ( @@ -834,7 +852,10 @@ 3F6C7C7213D5E57100C038FE /* OneUp.m */, 3F6C7EE213D636CF00C038FE /* Rock.h */, 3F6C7EE313D636CF00C038FE /* Rock.m */, - 3F8394AC13D72E2C0059AEE8 /* ValuableObject.h */, + 6C0C140C13F1C3EC003A31B2 /* ValuableObject.h */, + 6C0C140D13F1C3EC003A31B2 /* ValuableObject.m */, + 6C0C140F13F2099B003A31B2 /* Cart.h */, + 6C0C141013F2099B003A31B2 /* Cart.m */, ); name = Items; sourceTree = ""; @@ -1341,6 +1362,9 @@ 3F03221613D78F8C00E6A708 /* HighscoreListController.m in Sources */, 3F03221913D7904E00E6A708 /* Highscore.m in Sources */, 6C29041013EAEB590032DA0F /* TutorialBubble.m in Sources */, + 6C0C140E13F1C3EC003A31B2 /* ValuableObject.m in Sources */, + 6C0C141113F2099B003A31B2 /* Cart.m in Sources */, + 6C0C141413F20E98003A31B2 /* GameMode.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Classes/Bottle.h b/Classes/Bottle.h index 37c41d5..71e94e9 100755 --- a/Classes/Bottle.h +++ b/Classes/Bottle.h @@ -7,10 +7,9 @@ // #import -#import "FallingObject.h" #import "ValuableObject.h" -@interface Bottle : FallingObject { +@interface Bottle : ValuableObject { } diff --git a/Classes/Cart.h b/Classes/Cart.h new file mode 100644 index 0000000..d708d73 --- /dev/null +++ b/Classes/Cart.h @@ -0,0 +1,22 @@ +// +// Cart.h +// Cart Collect +// +// Created by Starla Insigna on 8/9/11. +// Copyright 2011 Four Island. All rights reserved. +// + +#import +#import "cocos2d.h" + +@interface Cart : NSObject { + CCSprite* sprite; + float accelX; +} + +@property (readonly) CCSprite* sprite; +- (id)initWithSprite:(CCSprite*)sprite; +- (void)tick; +- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration; + +@end diff --git a/Classes/Cart.m b/Classes/Cart.m new file mode 100644 index 0000000..3046be3 --- /dev/null +++ b/Classes/Cart.m @@ -0,0 +1,45 @@ +// +// Cart.m +// Cart Collect +// +// Created by Starla Insigna on 8/9/11. +// Copyright 2011 Four Island. All rights reserved. +// + +#import "Cart.h" + +@implementation Cart + +@synthesize sprite; + +- (id)initWithSprite:(CCSprite*)m_sprite +{ + self = [super init]; + + if (nil != self) + { + sprite = m_sprite; + } + + return self; +} + +- (void)tick +{ + // Move the cart based on acceleration gathered from accelerometer + sprite.position = ccp(MIN(MAX(sprite.position.x+accelX, 16),464), sprite.position.y); +} + +- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration +{ + static float prevY=0; + +#define kFilterFactor 0.05f + + float accelY = -((float) acceleration.y * kFilterFactor + (1- kFilterFactor)*prevY); + + prevY = accelY; + accelX = accelY * 750; +} + +@end diff --git a/Classes/Cherry.h b/Classes/Cherry.h index 65437f5..0a7e450 100755 --- a/Classes/Cherry.h +++ b/Classes/Cherry.h @@ -7,10 +7,9 @@ // #import -#import "FallingObject.h" #import "ValuableObject.h" -@interface Cherry : FallingObject { +@interface Cherry : ValuableObject { } diff --git a/Classes/FallingObject.h b/Classes/FallingObject.h index a1ac56e..0bda787 100755 --- a/Classes/FallingObject.h +++ b/Classes/FallingObject.h @@ -17,5 +17,8 @@ @property (readonly) CCSprite* sprite; @property (readonly) int weight; - (id)init; +- (BOOL)tick; +- (void)collideWithCart; +- (void)collideWithFloor; @end diff --git a/Classes/FallingObject.m b/Classes/FallingObject.m index 297b426..515948e 100755 --- a/Classes/FallingObject.m +++ b/Classes/FallingObject.m @@ -7,7 +7,7 @@ // #import "FallingObject.h" - +#import "GameMode.h" @implementation FallingObject @@ -25,6 +25,54 @@ return self; } +- (BOOL)tick +{ + GameMode* gameLayer = ((GameMode*) sprite.parent); + + // Move objects down + sprite.position = ccp(sprite.position.x, sprite.position.y-weight); + + // Cart collision detection + CGSize first = [gameLayer.cart.sprite boundingBox].size; + CGSize second = [sprite boundingBox].size; + + if (gameLayer.cart.sprite.position.x > (sprite.position.x - second.width/2 - first.width/2)) + { + if (gameLayer.cart.sprite.position.x < (sprite.position.x + second.width/2 + first.width/2)) + { + if (gameLayer.cart.sprite.position.y > (sprite.position.y - second.height/2 - first.height/2)) + { + if (gameLayer.cart.sprite.position.y < (sprite.position.y + second.height/2 + first.height/2)) + { + [self collideWithCart]; + + return YES; + } + } + } + } + + // Collision detection with floor + if (sprite.position.y - (sprite.contentSize.height/2) < 0) + { + [self collideWithFloor]; + + return YES; + } + + return NO; +} + +- (void)collideWithCart +{ + +} + +- (void)collideWithFloor +{ + +} + - (void)dealloc { [sprite release]; diff --git a/Classes/GameLayer.h b/Classes/GameLayer.h index e72b551..128cbf2 100755 --- a/Classes/GameLayer.h +++ b/Classes/GameLayer.h @@ -8,30 +8,13 @@ #import #import "cocos2d.h" -#import "FallingObject.h" -#import "Cherry.h" -#import "Bottle.h" -#import "OneUp.h" -#import "Rock.h" -#import "GameOverLayer.h" -#import "ValuableObject.h" -#import "CocosDenshion.h" -#import "SimpleAudioEngine.h" #import "TutorialBubble.h" -#import "MainMenuLayer.h" +#import "GameMode.h" -#define GAME_SCENE 436 -#define GAME_LAYER 437 - -@interface GameLayer : CCLayer { - NSMutableSet* objects; - float accelX; +@interface GameLayer : GameMode { CCLabelBMFont* scoreLabel; CCLabelBMFont* livesLabel; - int score; - int lives; float addSpeed; - CCSprite* cartSprite; TutorialBubble* currentTutorial; CCLayerColor* shadedLayer; @@ -39,9 +22,7 @@ } @property (nonatomic,retain) TutorialBubble* currentTutorial; -+ (CCScene*)scene; - (id)init; -- (void)updateLabels; - (void)pause; - (void)unpause; - (void)mainmenu; diff --git a/Classes/GameLayer.m b/Classes/GameLayer.m index 2ec8ef5..95bc83b 100755 --- a/Classes/GameLayer.m +++ b/Classes/GameLayer.m @@ -7,109 +7,29 @@ // #import "GameLayer.h" - +#import "FallingObject.h" +#import "Cherry.h" +#import "Bottle.h" +#import "OneUp.h" +#import "Rock.h" +#import "GameOverLayer.h" +#import "SimpleAudioEngine.h" +#import "MainMenuLayer.h" @implementation GameLayer @synthesize currentTutorial; -+ (CCScene*)scene -{ - CCScene* scene = [CCScene node]; - - CCLayerColor* backgroundLayer = [CCLayerColor layerWithColor:ccc4(255, 255, 255, 255)]; - [scene addChild:backgroundLayer]; - - GameLayer* layer = [GameLayer node]; - layer.tag = GAME_LAYER; - [scene addChild:layer]; - - scene.tag = GAME_SCENE; - - return scene; -} - - (void)tick:(ccTime)dt { - // Move the cart based on acceleration gathered from accelerometer - cartSprite.position = ccp(MIN(MAX(cartSprite.position.x+accelX, 16),464), cartSprite.position.y); - int lastScore = score; - for (FallingObject* object in objects) - { - // Move objects down - object.sprite.position = ccp(object.sprite.position.x, object.sprite.position.y-object.weight); - - // Cart collision detection - CGSize first = [cartSprite boundingBox].size; - CGSize second = [object.sprite boundingBox].size; - - if (cartSprite.position.x > (object.sprite.position.x - second.width/2 - first.width/2)) - { - if (cartSprite.position.x < (object.sprite.position.x + second.width/2 + first.width/2)) - { - if (cartSprite.position.y > (object.sprite.position.y - second.height/2 - first.height/2)) - { - if (cartSprite.position.y < (object.sprite.position.y + second.height/2 + first.height/2)) - { - [object retain]; - [objects removeObject:object]; - - // If a cart collides with an object, it's going to vanish no matter what - [object.sprite.parent removeChild:object.sprite cleanup:YES]; - - NSString* audioFile = nil; - if ([object isKindOfClass:[OneUp class]]) - { - audioFile = [[NSBundle mainBundle] pathForResource:@"1up" ofType:@"wav"]; - lives++; - } else if ([object isKindOfClass:[Rock class]]) - { - audioFile = [[NSBundle mainBundle] pathForResource:@"Damage1" ofType:@"wav"]; - lives--; - } else if ([object conformsToProtocol:@protocol(ValuableObject)]) { - audioFile = [[NSBundle mainBundle] pathForResource:@"Item1" ofType:@"wav"]; - score += [((FallingObject*)object) pointValue]; - } - - if (audioFile != nil) - { - [[SimpleAudioEngine sharedEngine] playEffect:audioFile]; - } - - [self updateLabels]; - - continue; // Don't check for collision with floor - } - } - } - } - - // Collision detection with floor - if (object.sprite.position.y - (object.sprite.contentSize.height/2) < 0) - { - [object retain]; - [objects removeObject:object]; - - [object.sprite.parent removeChild:object.sprite cleanup:YES]; - - if ([object conformsToProtocol:@protocol(ValuableObject)]) - { - NSString* audioFile = [[NSBundle mainBundle] pathForResource:@"Damage1" ofType:@"wav"]; - [[SimpleAudioEngine sharedEngine] playEffect:audioFile]; - - lives--; - - [self updateLabels]; - } - } - } + [super tick:dt]; if (lives == 0) { - [self unschedule:@selector(randomlyAddObject:)]; - [self unschedule:@selector(tick:)]; + [self unscheduleAllSelectors]; + [[CCDirector sharedDirector] replaceScene:[CCTransitionSlideInT transitionWithDuration:1.5f scene:[GameOverLayer sceneWithScore:score]]]; } else if (score > lastScore) { @@ -237,20 +157,12 @@ backgroundImage.position = ccp(240, 160); [self addChild:backgroundImage z:0]; - isAccelerometerEnabled_ = YES; - - //cart = [[Cart alloc] init]; - cartSprite = [CCSprite spriteWithFile:@"cart.png"]; - cartSprite.position = ccp(winWidth/2, 22); - cartSprite.scale = cartScale; - [self addChild:cartSprite]; - - objects = [[NSMutableSet alloc] init]; - - score = 0; - lives = 3; - - scoreLabel = [CCLabelBMFont labelWithString:@"Score: 0" fntFile:@"helvetica2.fnt"]; + cart = [[Cart alloc] initWithSprite:[CCSprite spriteWithFile:@"cart.png"]]; + cart.sprite.position = ccp(winWidth/2, 22); + cart.sprite.scale = cartScale; + [self addChild:cart.sprite]; + + scoreLabel = [CCLabelBMFont labelWithString:@"Score: 0" fntFile:@"helvetica2.fnt"]; scoreLabel.position = ccp(50, 300); [self addChild:scoreLabel]; @@ -258,6 +170,9 @@ livesLabel.position = ccp(50, 280); [self addChild:livesLabel]; + score = 0; + lives = 3; + CCMenuItemImage* pauseButton = [CCMenuItemImage itemFromNormalImage:@"pause2.png" selectedImage:@"pause.png" target:self selector:@selector(pause)]; CCMenu* pauseMenu = [CCMenu menuWithItems:pauseButton, nil]; [pauseMenu setPosition:ccp(480-8-16, 320-8-16)]; @@ -273,27 +188,21 @@ { [super onEnter]; - [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / 60)]; - [self schedule:@selector(tick:) interval:1.0f/60.0f]; [self schedule:@selector(randomlyAddObject:) interval:addSpeed]; } -- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration -{ - static float prevY=0; - -#define kFilterFactor 0.05f - - float accelY = -((float) acceleration.y * kFilterFactor + (1- kFilterFactor)*prevY); - - prevY = accelY; - accelX = accelY * 750; +- (void)setScore:(int)m_score +{ + score = m_score; + + [scoreLabel setString:[NSString stringWithFormat:@"Score: %d", score]]; } -- (void)updateLabels +- (void)setLives:(int)m_lives { - [scoreLabel setString:[NSString stringWithFormat:@"Score: %d", score]]; - [livesLabel setString:[NSString stringWithFormat:@"Lives: %d", lives]]; + lives = m_lives; + + [livesLabel setString:[NSString stringWithFormat:@"Lives: %d", lives]]; } - (void)pause diff --git a/Classes/GameMode.h b/Classes/GameMode.h new file mode 100644 index 0000000..bd47c90 --- /dev/null +++ b/Classes/GameMode.h @@ -0,0 +1,27 @@ +// +// GameMode.h +// Cart Collect +// +// Created by Starla Insigna on 8/9/11. +// Copyright 2011 Four Island. All rights reserved. +// + +#import "CCLayer.h" +#import "Cart.h" + +#define GAME_SCENE 436 +#define GAME_LAYER 437 + +@interface GameMode : CCLayer { + NSMutableSet* objects; + int score; + int lives; + Cart* cart; +} + +@property (readonly) Cart* cart; +@property (assign) int score; +@property (assign) int lives; +- (void)tick:(ccTime)dt; + +@end diff --git a/Classes/GameMode.m b/Classes/GameMode.m new file mode 100644 index 0000000..6fa31e5 --- /dev/null +++ b/Classes/GameMode.m @@ -0,0 +1,78 @@ +// +// GameMode.m +// Cart Collect +// +// Created by Starla Insigna on 8/9/11. +// Copyright 2011 Four Island. All rights reserved. +// + +#import "GameMode.h" +#import "FallingObject.h" + +@implementation GameMode + +@synthesize cart, score, lives; + ++ (CCScene*)scene +{ + CCScene* scene = [CCScene node]; + + GameMode* layer = [self node]; + layer.tag = GAME_LAYER; + [scene addChild:layer]; + + scene.tag = GAME_SCENE; + + return scene; +} + +- (id)init +{ + self = [super init]; + + if (nil != self) + { + isAccelerometerEnabled_ = YES; + + objects = [[NSMutableSet alloc] init]; + } + + return self; +} + +- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration +{ + [cart accelerometer:accelerometer didAccelerate:acceleration]; +} + +- (void)onEnter +{ + [super onEnter]; + + [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / 60)]; + [self schedule:@selector(tick:) interval:1.0f/60.0f]; +} + +- (void)tick:(ccTime)dt +{ + [cart tick]; + + for (FallingObject* object in objects) + { + if ([object tick]) + { + [object retain]; + [self removeChild:object.sprite cleanup:YES]; + [objects removeObject:object]; + } + } +} + +- (void)dealloc +{ + [objects release]; + [cart release]; + [super dealloc]; +} + +@end diff --git a/Classes/GameOverLayer.h b/Classes/GameOverLayer.h index ea533ac..c800e82 100755 --- a/Classes/GameOverLayer.h +++ b/Classes/GameOverLayer.h @@ -8,11 +8,6 @@ #import #import "cocos2d.h" -#import "Cart_CollectAppDelegate.h" -#import -#import "cocoslive.h" - -@class MainMenuLayer; @interface GameOverLayer : CCLayer { UILabel* scoreField; diff --git a/Classes/GameOverLayer.m b/Classes/GameOverLayer.m index 65b11e1..9f58f01 100755 --- a/Classes/GameOverLayer.m +++ b/Classes/GameOverLayer.m @@ -7,7 +7,10 @@ // #import "GameOverLayer.h" - +#import "Cart_CollectAppDelegate.h" +#import +#import "cocoslive.h" +#import "MainMenuLayer.h" @implementation GameOverLayer diff --git a/Classes/Highscore.h b/Classes/Highscore.h index 4cab75f..bc6a8fb 100755 --- a/Classes/Highscore.h +++ b/Classes/Highscore.h @@ -7,9 +7,6 @@ // #import -#import -#import "Cart_CollectAppDelegate.h" -#import "cocoslive.h" @interface Highscore : NSObject { NSString* name; diff --git a/Classes/Highscore.m b/Classes/Highscore.m index 7239cca..20ce56f 100755 --- a/Classes/Highscore.m +++ b/Classes/Highscore.m @@ -8,7 +8,6 @@ #import "Highscore.h" - @implementation Highscore @synthesize name, score, date; diff --git a/Classes/HighscoreListController.h b/Classes/HighscoreListController.h index f673609..7b0069d 100755 --- a/Classes/HighscoreListController.h +++ b/Classes/HighscoreListController.h @@ -7,9 +7,6 @@ // #import -#import "Highscore.h" -#import "cocoslive.h" -#import "RootViewController.h" @interface HighscoreListController : UITableViewController { UINavigationBar* navigationBar; diff --git a/Classes/HighscoreListController.m b/Classes/HighscoreListController.m index 4d108e4..932b147 100755 --- a/Classes/HighscoreListController.m +++ b/Classes/HighscoreListController.m @@ -7,7 +7,11 @@ // #import "HighscoreListController.h" - +#import "Highscore.h" +#import "RootViewController.h" +#import "cocoslive.h" +#import +#import "Cart_CollectAppDelegate.h" @implementation HighscoreListController @@ -290,9 +294,10 @@ - (void)back { - RootViewController* viewController = [[[UIApplication sharedApplication] delegate] viewController]; - [[[[UIApplication sharedApplication] delegate] window] setRootViewController:nil]; - [[[[UIApplication sharedApplication] delegate] window] addSubview:viewController.view]; + Cart_CollectAppDelegate* appDelegate = ((Cart_CollectAppDelegate*) [[UIApplication sharedApplication] delegate]); + RootViewController* viewController = [appDelegate viewController]; + [[appDelegate window] setRootViewController:nil]; + [[appDelegate window] addSubview:viewController.view]; } - (void)switchLists:(id)sender diff --git a/Classes/MainMenuLayer.h b/Classes/MainMenuLayer.h index fc4f82f..d8483b3 100755 --- a/Classes/MainMenuLayer.h +++ b/Classes/MainMenuLayer.h @@ -8,9 +8,6 @@ #import #import "cocos2d.h" -#import "HighscoreListController.h" - -@class GameLayer; @interface MainMenuLayer : CCLayer { diff --git a/Classes/MainMenuLayer.m b/Classes/MainMenuLayer.m index ac7f4a7..11cffd7 100755 --- a/Classes/MainMenuLayer.m +++ b/Classes/MainMenuLayer.m @@ -7,7 +7,9 @@ // #import "MainMenuLayer.h" - +#import "HighscoreListController.h" +#import "GameLayer.h" +#import "Cart_CollectAppDelegate.h" @implementation MainMenuLayer @@ -61,7 +63,8 @@ - (void)highscores { HighscoreListController* listController = [[HighscoreListController alloc] initWithStyle:UITableViewStylePlain]; - [[[[UIApplication sharedApplication] delegate] window] setRootViewController:listController]; + Cart_CollectAppDelegate* appDelegate = ((Cart_CollectAppDelegate*) [[UIApplication sharedApplication] delegate]); + [[appDelegate window] setRootViewController:listController]; [listController release]; } diff --git a/Classes/OneUp.m b/Classes/OneUp.m index e09935c..9e0d4f0 100755 --- a/Classes/OneUp.m +++ b/Classes/OneUp.m @@ -7,7 +7,8 @@ // #import "OneUp.h" - +#import "GameMode.h" +#import "SimpleAudioEngine.h" @implementation OneUp @@ -24,4 +25,12 @@ return self; } +- (void)collideWithCart +{ + GameMode* gameLayer = ((GameMode*) sprite.parent); + [gameLayer setLives:gameLayer.lives+1]; + + [[SimpleAudioEngine sharedEngine] playEffect:[[NSBundle mainBundle] pathForResource:@"1up" ofType:@"wav"]]; +} + @end diff --git a/Classes/Rock.m b/Classes/Rock.m index a3b3b9e..aac1aaf 100755 --- a/Classes/Rock.m +++ b/Classes/Rock.m @@ -7,7 +7,8 @@ // #import "Rock.h" - +#import "GameMode.h" +#import "SimpleAudioEngine.h" @implementation Rock @@ -24,4 +25,13 @@ return self; } +- (void)collideWithCart +{ + GameMode* gameLayer = ((GameMode*) sprite.parent); + [gameLayer setLives:gameLayer.lives-1]; + + [[SimpleAudioEngine sharedEngine] playEffect:[[NSBundle mainBundle] pathForResource:@"Damage1" ofType:@"wav"]]; +} + + @end diff --git a/Classes/ValuableObject.h b/Classes/ValuableObject.h old mode 100755 new mode 100644 index ccefbbd..7b484d9 --- a/Classes/ValuableObject.h +++ b/Classes/ValuableObject.h @@ -2,13 +2,13 @@ // ValuableObject.h // Cart Collect // -// Created by iD Student Account on 7/20/11. -// Copyright 2011 __MyCompanyName__. All rights reserved. +// Created by Starla Insigna on 8/9/11. +// Copyright 2011 Four Island. All rights reserved. // -#import +#import "FallingObject.h" -@protocol ValuableObject +@interface ValuableObject : FallingObject - (int)pointValue; diff --git a/Classes/ValuableObject.m b/Classes/ValuableObject.m new file mode 100644 index 0000000..f1a036f --- /dev/null +++ b/Classes/ValuableObject.m @@ -0,0 +1,46 @@ +// +// ValuableObject.m +// Cart Collect +// +// Created by Starla Insigna on 8/9/11. +// Copyright 2011 Four Island. All rights reserved. +// + +#import "ValuableObject.h" +#import "GameMode.h" +#import "SimpleAudioEngine.h" + +@implementation ValuableObject + +- (id)init +{ + self = [super init]; + if (self) { + // Initialization code here. + } + + return self; +} + +- (void)collideWithCart +{ + GameMode* gameLayer = ((GameMode*) sprite.parent); + [gameLayer setScore:gameLayer.score+self.pointValue]; + + [[SimpleAudioEngine sharedEngine] playEffect:[[NSBundle mainBundle] pathForResource:@"Item1" ofType:@"wav"]]; +} + +- (void)collideWithFloor +{ + GameMode* gameLayer = ((GameMode*) sprite.parent); + [gameLayer setLives:gameLayer.lives-1]; + + [[SimpleAudioEngine sharedEngine] playEffect:[[NSBundle mainBundle] pathForResource:@"Damage1" ofType:@"wav"]]; +} + +- (int)pointValue +{ + @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)] userInfo:nil]; +} + +@end -- cgit 1.4.1