From 9cd57b731ab1c666d4a1cb725538fdc137763d12 Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Sat, 30 Jul 2011 11:19:14 -0400 Subject: Initial commit (version 0.2.1) --- Classes/Bottle.h | 19 ++ Classes/Bottle.m | 32 ++++ Classes/Cart_CollectAppDelegate.h | 23 +++ Classes/Cart_CollectAppDelegate.m | 212 ++++++++++++++++++++++ Classes/Cherry.h | 19 ++ Classes/Cherry.m | 32 ++++ Classes/FallingObject.h | 21 +++ Classes/FallingObject.m | 34 ++++ Classes/GameConfig.h | 45 +++++ Classes/GameLayer.h | 38 ++++ Classes/GameLayer.m | 301 +++++++++++++++++++++++++++++++ Classes/GameOverLayer.h | 32 ++++ Classes/GameOverLayer.m | 196 ++++++++++++++++++++ Classes/Highscore.h | 25 +++ Classes/Highscore.m | 37 ++++ Classes/HighscoreListController.h | 32 ++++ Classes/HighscoreListController.m | 371 ++++++++++++++++++++++++++++++++++++++ Classes/MainMenuLayer.h | 24 +++ Classes/MainMenuLayer.m | 68 +++++++ Classes/OneUp.h | 18 ++ Classes/OneUp.m | 27 +++ Classes/PauseLayer.h | 22 +++ Classes/PauseLayer.m | 68 +++++++ Classes/Rock.h | 18 ++ Classes/Rock.m | 27 +++ Classes/RootViewController.h | 16 ++ Classes/RootViewController.m | 152 ++++++++++++++++ Classes/ValuableObject.h | 15 ++ 28 files changed, 1924 insertions(+) create mode 100755 Classes/Bottle.h create mode 100755 Classes/Bottle.m create mode 100755 Classes/Cart_CollectAppDelegate.h create mode 100755 Classes/Cart_CollectAppDelegate.m create mode 100755 Classes/Cherry.h create mode 100755 Classes/Cherry.m create mode 100755 Classes/FallingObject.h create mode 100755 Classes/FallingObject.m create mode 100755 Classes/GameConfig.h create mode 100755 Classes/GameLayer.h create mode 100755 Classes/GameLayer.m create mode 100755 Classes/GameOverLayer.h create mode 100755 Classes/GameOverLayer.m create mode 100755 Classes/Highscore.h create mode 100755 Classes/Highscore.m create mode 100755 Classes/HighscoreListController.h create mode 100755 Classes/HighscoreListController.m create mode 100755 Classes/MainMenuLayer.h create mode 100755 Classes/MainMenuLayer.m create mode 100755 Classes/OneUp.h create mode 100755 Classes/OneUp.m create mode 100755 Classes/PauseLayer.h create mode 100755 Classes/PauseLayer.m create mode 100755 Classes/Rock.h create mode 100755 Classes/Rock.m create mode 100755 Classes/RootViewController.h create mode 100755 Classes/RootViewController.m create mode 100755 Classes/ValuableObject.h (limited to 'Classes') diff --git a/Classes/Bottle.h b/Classes/Bottle.h new file mode 100755 index 0000000..37c41d5 --- /dev/null +++ b/Classes/Bottle.h @@ -0,0 +1,19 @@ +// +// Bottle.h +// Cart Collect +// +// Created by iD Student Account on 7/19/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import +#import "FallingObject.h" +#import "ValuableObject.h" + +@interface Bottle : FallingObject { + +} + +- (id)init; + +@end diff --git a/Classes/Bottle.m b/Classes/Bottle.m new file mode 100755 index 0000000..bae2716 --- /dev/null +++ b/Classes/Bottle.m @@ -0,0 +1,32 @@ +// +// Bottle.m +// Cart Collect +// +// Created by iD Student Account on 7/19/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import "Bottle.h" + + +@implementation Bottle + +- (id)init +{ + self = [super init]; + + if (nil != self) + { + sprite = [CCSprite spriteWithFile:@"bottle.png"]; + weight = 6; + } + + return self; +} + +- (int)pointValue +{ + return 25; +} + +@end diff --git a/Classes/Cart_CollectAppDelegate.h b/Classes/Cart_CollectAppDelegate.h new file mode 100755 index 0000000..0ef23a7 --- /dev/null +++ b/Classes/Cart_CollectAppDelegate.h @@ -0,0 +1,23 @@ +// +// Cart_CollectAppDelegate.h +// Cart Collect +// +// Created by iD Student Account on 7/18/11. +// Copyright __MyCompanyName__ 2011. All rights reserved. +// + +#import +#import + +@class RootViewController; + +@interface Cart_CollectAppDelegate : NSObject { + UIWindow *window; + RootViewController *viewController; +} + +@property (nonatomic, retain) UIWindow *window; +@property (nonatomic, retain) RootViewController* viewController; ++ (sqlite3*)database; + +@end diff --git a/Classes/Cart_CollectAppDelegate.m b/Classes/Cart_CollectAppDelegate.m new file mode 100755 index 0000000..201422f --- /dev/null +++ b/Classes/Cart_CollectAppDelegate.m @@ -0,0 +1,212 @@ +// +// Cart_CollectAppDelegate.m +// Cart Collect +// +// Created by iD Student Account on 7/18/11. +// Copyright __MyCompanyName__ 2011. All rights reserved. +// + +#import "cocos2d.h" + +#import "Cart_CollectAppDelegate.h" +#import "GameConfig.h" +#import "GameLayer.h" +#import "RootViewController.h" +#import "MainMenuLayer.h" + +@implementation Cart_CollectAppDelegate + +@synthesize window, viewController; + +- (void) removeStartupFlicker +{ + // + // THIS CODE REMOVES THE STARTUP FLICKER + // + // Uncomment the following code if you Application only supports landscape mode + // +#if GAME_AUTOROTATION == kGameAutorotationUIViewController + + // CC_ENABLE_DEFAULT_GL_STATES(); + // CCDirector *director = [CCDirector sharedDirector]; + // CGSize size = [director winSize]; + // CCSprite *sprite = [CCSprite spriteWithFile:@"Default.png"]; + // sprite.position = ccp(size.width/2, size.height/2); + // sprite.rotation = -90; + // [sprite visit]; + // [[director openGLView] swapBuffers]; + // CC_ENABLE_DEFAULT_GL_STATES(); + +#endif // GAME_AUTOROTATION == kGameAutorotationUIViewController +} + +- (void) applicationDidFinishLaunching:(UIApplication*)application +{ + [[UIApplication sharedApplication] setIdleTimerDisabled:YES]; + + NSMutableDictionary* registerDefaults = [NSMutableDictionary dictionaryWithObjectsAndKeys: + @"", @"username", + [NSNumber numberWithBool:YES], @"submitScore", + nil]; + [[NSUserDefaults standardUserDefaults] registerDefaults:registerDefaults]; + + // Init the window + window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; + + // Try to use CADisplayLink director + // if it fails (SDK < 3.1) use the default director + if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] ) + [CCDirector setDirectorType:kCCDirectorTypeDefault]; + + + CCDirector *director = [CCDirector sharedDirector]; + + // Init the View Controller + viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil]; + viewController.wantsFullScreenLayout = YES; + + // + // Create the EAGLView manually + // 1. Create a RGB565 format. Alternative: RGBA8 + // 2. depth format of 0 bit. Use 16 or 24 bit for 3d effects, like CCPageTurnTransition + // + // + EAGLView *glView = [EAGLView viewWithFrame:[window bounds] + pixelFormat:kEAGLColorFormatRGB565 // kEAGLColorFormatRGBA8 + depthFormat:0 // GL_DEPTH_COMPONENT16_OES + ]; + + // attach the openglView to the director + [director setOpenGLView:glView]; + +// // Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices + //if( ! [director enableRetinaDisplay:YES] ) + // CCLOG(@"Retina Display Not supported"); + + // + // VERY IMPORTANT: + // If the rotation is going to be controlled by a UIViewController + // then the device orientation should be "Portrait". + // + // IMPORTANT: + // By default, this template only supports Landscape orientations. + // Edit the RootViewController.m file to edit the supported orientations. + // +#if GAME_AUTOROTATION == kGameAutorotationUIViewController + [director setDeviceOrientation:kCCDeviceOrientationPortrait]; +#else + [director setDeviceOrientation:kCCDeviceOrientationPortrait]; +#endif + + [director setAnimationInterval:1.0/60]; + //[director setDisplayFPS:YES]; + + + // make the OpenGLView a child of the view controller + [viewController setView:glView]; + + // make the View Controller a child of the main window + [window addSubview: viewController.view]; + + [window makeKeyAndVisible]; + + // Default texture format for PNG/BMP/TIFF/JPEG/GIF images + // It can be RGBA8888, RGBA4444, RGB5_A1, RGB565 + // You can change anytime. + [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888]; + + + // Removes the startup flicker + [self removeStartupFlicker]; + + // Run the intro Scene + [[CCDirector sharedDirector] runWithScene: [MainMenuLayer scene]]; +} + + +- (void)applicationWillResignActive:(UIApplication *)application { + [[CCDirector sharedDirector] pause]; +} + +- (void)applicationDidBecomeActive:(UIApplication *)application { + [[CCDirector sharedDirector] resume]; +} + +- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application { + [[CCDirector sharedDirector] purgeCachedData]; +} + +-(void) applicationDidEnterBackground:(UIApplication*)application { + [[CCDirector sharedDirector] stopAnimation]; + + if ([[CCDirector sharedDirector] runningScene].tag == 436) + { + [[CCDirector sharedDirector] replaceScene:[PauseLayer sceneWithScene:[[CCDirector sharedDirector] runningScene]]]; + } +} + +-(void) applicationWillEnterForeground:(UIApplication*)application { + [[CCDirector sharedDirector] startAnimation]; +} + +- (void)applicationWillTerminate:(UIApplication *)application { + CCDirector *director = [CCDirector sharedDirector]; + + sqlite3_close([Cart_CollectAppDelegate database]); + + [[director openGLView] removeFromSuperview]; + + [viewController release]; + + [window release]; + + [director end]; +} + +- (void)applicationSignificantTimeChange:(UIApplication *)application { + [[CCDirector sharedDirector] setNextDeltaTimeZero:YES]; +} + +- (void)dealloc { + //[[CCDirector sharedDirector] release]; + [window release]; + [super dealloc]; +} + ++ (sqlite3*)database +{ + static sqlite3* database = nil; + + if (database == nil) + { + NSString* databaseName = @"cartdata.sqlite3"; + NSArray* documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); + NSString* documentsDir = [documentPaths objectAtIndex:0]; + NSString* databasePath = [documentsDir stringByAppendingPathComponent:databaseName]; + NSFileManager* fileManager = [NSFileManager defaultManager]; + NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults]; + + if (![fileManager fileExistsAtPath:databasePath]) + { + NSString* databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName]; + [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil]; + //[fileManager release]; + + [userDefaults setInteger:1 forKey:@"databaseVersion"]; + } + + if ([userDefaults integerForKey:@"databaseVersion"] < 1) + { + // Upgrade the database to version 1, which is completely unnecessary as no prior version exists + } + + if (sqlite3_open([databasePath UTF8String], &database) != SQLITE_OK) + { + NSLog(@"Failed to open database"); + } + } + + return database; +} + +@end diff --git a/Classes/Cherry.h b/Classes/Cherry.h new file mode 100755 index 0000000..65437f5 --- /dev/null +++ b/Classes/Cherry.h @@ -0,0 +1,19 @@ +// +// Cherry.h +// Cart Collect +// +// Created by iD Student Account on 7/19/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import +#import "FallingObject.h" +#import "ValuableObject.h" + +@interface Cherry : FallingObject { + +} + +- (id)init; + +@end diff --git a/Classes/Cherry.m b/Classes/Cherry.m new file mode 100755 index 0000000..f53c86e --- /dev/null +++ b/Classes/Cherry.m @@ -0,0 +1,32 @@ +// +// Cherry.m +// Cart Collect +// +// Created by iD Student Account on 7/19/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import "Cherry.h" + + +@implementation Cherry + +- (id)init +{ + self = [super init]; + + if (nil != self) + { + sprite = [CCSprite spriteWithFile:@"cherry.png"]; + weight = 5; + } + + return self; +} + +- (int)pointValue +{ + return 10; +} + +@end diff --git a/Classes/FallingObject.h b/Classes/FallingObject.h new file mode 100755 index 0000000..a1ac56e --- /dev/null +++ b/Classes/FallingObject.h @@ -0,0 +1,21 @@ +// +// GameObject.h +// Cart Collect +// +// Created by iD Student Account on 7/19/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import +#import "cocos2d.h" + +@interface FallingObject : NSObject { + CCSprite* sprite; + int weight; +} + +@property (readonly) CCSprite* sprite; +@property (readonly) int weight; +- (id)init; + +@end diff --git a/Classes/FallingObject.m b/Classes/FallingObject.m new file mode 100755 index 0000000..297b426 --- /dev/null +++ b/Classes/FallingObject.m @@ -0,0 +1,34 @@ +// +// GameObject.m +// Cart Collect +// +// Created by iD Student Account on 7/19/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import "FallingObject.h" + + +@implementation FallingObject + +@synthesize sprite, weight; + +- (id)init +{ + self = [super init]; + + if (nil != self) + { + + } + + return self; +} + +- (void)dealloc +{ + [sprite release]; + [super dealloc]; +} + +@end \ No newline at end of file diff --git a/Classes/GameConfig.h b/Classes/GameConfig.h new file mode 100755 index 0000000..1dd3e99 --- /dev/null +++ b/Classes/GameConfig.h @@ -0,0 +1,45 @@ +// +// GameConfig.h +// Cart Collect +// +// Created by iD Student Account on 7/18/11. +// Copyright __MyCompanyName__ 2011. All rights reserved. +// + +#ifndef __GAME_CONFIG_H +#define __GAME_CONFIG_H + +// +// Supported Autorotations: +// None, +// UIViewController, +// CCDirector +// +#define kGameAutorotationNone 0 +#define kGameAutorotationCCDirector 1 +#define kGameAutorotationUIViewController 2 + +// +// Define here the type of autorotation that you want for your game +// + +// 3rd generation and newer devices: Rotate using UIViewController. Rotation should be supported on iPad apps. +// TIP: +// To improve the performance, you should set this value to "kGameAutorotationNone" or "kGameAutorotationCCDirector" +#if defined(__ARM_NEON__) || TARGET_IPHONE_SIMULATOR +#define GAME_AUTOROTATION kGameAutorotationUIViewController + +// ARMv6 (1st and 2nd generation devices): Don't rotate. It is very expensive +#elif __arm__ +#define GAME_AUTOROTATION kGameAutorotationNone + + +// Ignore this value on Mac +#elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED) + +#else +#error(unknown architecture) +#endif + +#endif // __GAME_CONFIG_H + diff --git a/Classes/GameLayer.h b/Classes/GameLayer.h new file mode 100755 index 0000000..88a1406 --- /dev/null +++ b/Classes/GameLayer.h @@ -0,0 +1,38 @@ +// +// GameLayer.h +// Cart Collect +// +// Created by iD Student Account on 7/18/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#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 "PauseLayer.h" +#import "CocosDenshion.h" +#import "SimpleAudioEngine.h" + +@interface GameLayer : CCLayer { + NSMutableSet* objects; + float accelX; + CCLabelBMFont* scoreLabel; + CCLabelBMFont* livesLabel; + int score; + int lives; + float addSpeed; + CCSprite* cartSprite; +} + ++ (CCScene*)scene; +- (id)init; +- (void)updateLabels; +- (void)pause; + +@end diff --git a/Classes/GameLayer.m b/Classes/GameLayer.m new file mode 100755 index 0000000..32ca483 --- /dev/null +++ b/Classes/GameLayer.m @@ -0,0 +1,301 @@ +// +// GameLayer.m +// Cart Collect +// +// Created by iD Student Account on 7/18/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import "GameLayer.h" + + +@implementation GameLayer + ++ (CCScene*)scene +{ + CCScene* scene = [CCScene node]; + + CCLayerColor* backgroundLayer = [CCLayerColor layerWithColor:ccc4(255, 255, 255, 255)]; + [scene addChild:backgroundLayer]; + + GameLayer* layer = [GameLayer node]; + [scene addChild:layer]; + + scene.tag = 436; + + 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]; + } + } + } + + if (lives == 0) + { + [self unschedule:@selector(randomlyAddObject:)]; + [self unschedule:@selector(tick:)]; + [[CCDirector sharedDirector] replaceScene:[CCTransitionSlideInT transitionWithDuration:1.5f scene:[GameOverLayer sceneWithScore:score]]]; + } else if (score > lastScore) + { + if ((lastScore < 6500) && (score >= 6500)) + { + [self unschedule:@selector(randomlyAddObject:)]; + [self schedule:@selector(randomlyAddObject:) interval:0.6f]; + addSpeed = 0.6f; + } else if ((lastScore < 4500) && (score >= 4500)) + { + [self unschedule:@selector(randomlyAddObject:)]; + [self schedule:@selector(randomlyAddObject:) interval:0.7f]; + addSpeed = 0.7f; + } else if ((lastScore < 2500) && (score >= 2500)) + { + [self unschedule:@selector(randomlyAddObject:)]; + [self schedule:@selector(randomlyAddObject:) interval:0.8f]; + addSpeed = 0.8f; + } else if ((lastScore < 1500) && (score >= 1500)) + { + [self unschedule:@selector(randomlyAddObject:)]; + [self schedule:@selector(randomlyAddObject:) interval:0.9f]; + addSpeed = 0.9f; + } else if ((lastScore < 500) && (score >= 500)) + { + [self unschedule:@selector(randomlyAddObject:)]; + [self schedule:@selector(randomlyAddObject:) interval:1.0f]; + addSpeed = 1.0f; + } else if ((lastScore < 150) && (score >= 150)) + { + [self unschedule:@selector(randomlyAddObject:)]; + [self schedule:@selector(randomlyAddObject:) interval:2.0f]; + addSpeed = 2.0f; + } + } +} + +- (void)randomlyAddObject:(ccTime)dt +{ + FallingObject* object; + int oneuppercent = 98 - (lives == 1 ? 1 : 0); + + if (score < 1000) + { + int randomval = arc4random()%100; + + if (randomval < 65) + { + object = [[Cherry alloc] init]; + } else if (randomval < oneuppercent) + { + object = [[Bottle alloc] init]; + } else { + object = [[OneUp alloc] init]; + } + } else { + int randomval = arc4random()%100; + + if (randomval < 40) + { + object = [[Cherry alloc] init]; + } else if (randomval < 70) + { + object = [[Rock alloc] init]; + } else if (randomval < oneuppercent) + { + object = [[Bottle alloc] init]; + } else { + object = [[OneUp alloc] init]; + } + } + + int objectX = arc4random()%448+16; + object.sprite.position = ccp(objectX, 360); + object.sprite.scale = 1; + [self addChild:object.sprite]; + + [objects addObject:object]; + [object release]; + + if (score >= 2000) + { + if (arc4random() % 100 > 80) + { + object = [[Rock alloc] init]; + + objectX = arc4random()%448+16; + object.sprite.position = ccp(objectX, 360); + object.sprite.scale = 1; + [self addChild:object.sprite]; + + [objects addObject:object]; + [object release]; + } + } + + if (score >= 4000) + { + if (arc4random() % 100 > 80) + { + object = [[Rock alloc] init]; + + objectX = arc4random()%448+16; + object.sprite.position = ccp(objectX, 360); + object.sprite.scale = 1; + [self addChild:object.sprite]; + + [objects addObject:object]; + [object release]; + } + } +} + +- (id)init +{ + self = [super init]; + + int winWidth = [CCDirector sharedDirector].winSize.width; + //int winHeight = [CCDirector sharedDirector].winSize.height; + int cartScale = 2; + + if (self != nil) + { + CCSprite* backgroundImage = [CCSprite spriteWithFile:@"SeaBeach.png"]; + 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"]; + scoreLabel.position = ccp(50, 300); + [self addChild:scoreLabel]; + + livesLabel = [CCLabelBMFont labelWithString:@"Lives: 3" fntFile:@"helvetica2.fnt"]; + livesLabel.position = ccp(50, 280); + [self addChild:livesLabel]; + + 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)]; + [self addChild:pauseMenu]; + + addSpeed = 2.5f; + } + + return self; +} + +-(void) onEnter +{ + [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)updateLabels +{ + [scoreLabel setString:[NSString stringWithFormat:@"Score: %d", score]]; + [livesLabel setString:[NSString stringWithFormat:@"Lives: %d", lives]]; +} + +- (void)pause +{ + [[CCDirector sharedDirector] replaceScene:[PauseLayer sceneWithScene:[[CCDirector sharedDirector] runningScene]]]; +} + +@end diff --git a/Classes/GameOverLayer.h b/Classes/GameOverLayer.h new file mode 100755 index 0000000..ea533ac --- /dev/null +++ b/Classes/GameOverLayer.h @@ -0,0 +1,32 @@ +// +// GameOverLayer.h +// Cart Collect +// +// Created by iD Student Account on 7/19/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import +#import "cocos2d.h" +#import "Cart_CollectAppDelegate.h" +#import +#import "cocoslive.h" + +@class MainMenuLayer; + +@interface GameOverLayer : CCLayer { + UILabel* scoreField; + UITextField* textField; + UISwitch* submitSwitch; + UIActivityIndicatorView* activityIndicator; + UIButton* backButton; + int score; +} + ++ (CCScene*)sceneWithScore:(int)score; +- (id)initWithScore:(int)score; +- (void)newgame; +- (void)submitScore; +- (void)exit; + +@end diff --git a/Classes/GameOverLayer.m b/Classes/GameOverLayer.m new file mode 100755 index 0000000..65b11e1 --- /dev/null +++ b/Classes/GameOverLayer.m @@ -0,0 +1,196 @@ +// +// GameOverLayer.m +// Cart Collect +// +// Created by iD Student Account on 7/19/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import "GameOverLayer.h" + + +@implementation GameOverLayer + ++ (CCScene*)sceneWithScore:(int)score +{ + CCScene* scene = [CCScene node]; + + CCLayerColor* backgroundLayer = [CCLayerColor layerWithColor:ccc4(255, 255, 255, 255)]; + [scene addChild:backgroundLayer]; + + GameOverLayer* layer = [[[GameOverLayer alloc] initWithScore:score] autorelease]; + [scene addChild:layer]; + + return scene; +} + +- (id)initWithScore:(int)score2 +{ + self = [super init]; + + if (nil != self) + { + CCSprite* backgroundImage = [CCSprite spriteWithFile:@"Morning1.png"]; + backgroundImage.position = ccp(240, 160); + [self addChild:backgroundImage z:0]; + + score = score2; + + scoreField = [[UILabel alloc] initWithFrame:CGRectMake(205, 320-200-320, 0, 0)]; + [scoreField setFont:[UIFont systemFontOfSize:20.0f]]; + [scoreField setBackgroundColor:[UIColor clearColor]]; + [scoreField setText:[NSString stringWithFormat:@"%d", score2]]; + CGSize labelSize = [scoreField.text sizeWithFont:scoreField.font constrainedToSize:CGSizeMake(160, 31) lineBreakMode:UILineBreakModeClip]; + [scoreField setFrame:CGRectMake(scoreField.frame.origin.x, scoreField.frame.origin.y, labelSize.width, labelSize.height)]; + [[[CCDirector sharedDirector] openGLView] addSubview:scoreField]; + + textField = [[UITextField alloc] initWithFrame:CGRectMake(205, 320-247-320, 216, 31)]; + [textField setFont:[UIFont systemFontOfSize:20.0f]]; + [textField setBackgroundColor:[UIColor clearColor]]; + [textField setDelegate:self]; + NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; + [textField setText:[defaults objectForKey:@"username"]]; + [[[CCDirector sharedDirector] openGLView] addSubview:textField]; + + submitSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(211, 320-161-320, 0, 0)]; + [submitSwitch setOn:[defaults boolForKey:@"submitScore"]]; + [[[CCDirector sharedDirector] openGLView] addSubview:submitSwitch]; + + activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(480-20-10, 320-20-10, 20, 20)]; + [[[CCDirector sharedDirector] openGLView] addSubview:activityIndicator]; + + backButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; + [backButton setFrame:CGRectMake(240-154/2, 320-60-15-320, 154, 31)]; + [backButton setTitle:@"Back to Main Menu" forState:UIControlStateNormal]; + [backButton addTarget:self action:@selector(newgame) forControlEvents:UIControlEventTouchUpInside]; + [backButton.titleLabel setFont:[UIFont systemFontOfSize:16.0]]; + [backButton.titleLabel setTextColor:[UIColor blackColor]]; + + [[[CCDirector sharedDirector] openGLView] addSubview:backButton]; + } + + return self; +} + +- (void)onEnter +{ + [UIView beginAnimations:nil context:NULL]; + [UIView setAnimationDuration:1.5f]; + [UIView setAnimationCurve:UIViewAnimationCurveLinear]; + scoreField.transform = CGAffineTransformMakeTranslation(0, 320); + textField.transform = CGAffineTransformMakeTranslation(0, 320); + submitSwitch.transform = CGAffineTransformMakeTranslation(0, 320); + backButton.transform = CGAffineTransformMakeTranslation(0, 320); + [UIView commitAnimations]; +} + +- (void)newgame +{ + backButton.enabled = NO; + textField.enabled = NO; + submitSwitch.enabled = NO; + + const char* sqlQuery = [[NSString stringWithFormat:@"INSERT INTO highscores (name, score) VALUES (\"%@\",%d)", [textField text], score] UTF8String]; + sqlite3_stmt* compiled_statement; + + if (sqlite3_prepare_v2([Cart_CollectAppDelegate database], sqlQuery, -1, &compiled_statement, NULL) == SQLITE_OK) + { + sqlite3_step(compiled_statement); + NSLog(@"awesome, %@", [textField text]); + } else { + NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg([Cart_CollectAppDelegate database])); + } + + if (submitSwitch.on) + { + [self submitScore]; + } else { + [self exit]; + } +} + +- (void)submitScore +{ + [activityIndicator startAnimating]; + + CLScoreServerPost* server = [[CLScoreServerPost alloc] initWithGameName:@"Cart Collect" gameKey:@"38f440a074b3264386455a36b2706d8f" delegate:self]; + NSMutableDictionary* dict = [[NSMutableDictionary alloc] init]; + [dict setObject:@"Classic" forKey:@"cc_category"]; + [dict setObject:[textField text] forKey:@"cc_playername"]; + [dict setObject:[NSNumber numberWithInt:score] forKey:@"cc_score"]; + [server sendScore:dict]; + [server release]; + [dict release]; +} + +- (BOOL)textFieldShouldReturn:(UITextField *)m_textField +{ + [m_textField resignFirstResponder]; + + return YES; +} + +- (void)scorePostOk:(id)sender +{ + [activityIndicator stopAnimating]; + + // Score post successful + [self exit]; +} + +- (void)scorePostFail:(id)sender +{ + [activityIndicator stopAnimating]; + + // score post failed + tPostStatus status = [sender postStatus]; + if( status == kPostStatusPostFailed ) { + NSLog(@"SERVER ERROR"); + // an error with the server ? + // try again + }else if( status == kPostStatusConnectionFailed ) { + NSLog(@"CONNECTION FAILURE"); + // a error establishing the connection ? + // turn-on wifi, and then try again + } + + UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Whoops" message:@"There was an error posting your score. Please make sure you have Internet access." delegate:self cancelButtonTitle:@"Try Again" otherButtonTitles:@"Cancel", nil]; + [alertView show]; + [alertView release]; +} + +- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex +{ + if (buttonIndex == 0) + { + [self submitScore]; + } else if (buttonIndex == 1) + { + [self exit]; + } +} + +- (void)exit +{ + NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; + [defaults setObject:[textField text] forKey:@"username"]; + [defaults setBool:submitSwitch.on forKey:@"submitScore"]; + + [submitSwitch removeFromSuperview]; + [submitSwitch release]; + + [textField removeFromSuperview]; + [textField release]; + + [scoreField removeFromSuperview]; + [scoreField release]; + + [activityIndicator removeFromSuperview]; + [activityIndicator release]; + + [backButton removeFromSuperview]; + + [[CCDirector sharedDirector] replaceScene:[MainMenuLayer scene]]; +} + +@end diff --git a/Classes/Highscore.h b/Classes/Highscore.h new file mode 100755 index 0000000..4cab75f --- /dev/null +++ b/Classes/Highscore.h @@ -0,0 +1,25 @@ +// +// Highscore.h +// Cart Collect +// +// Created by iD Student Account on 7/20/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import +#import +#import "Cart_CollectAppDelegate.h" +#import "cocoslive.h" + +@interface Highscore : NSObject { + NSString* name; + int score; + NSDate* date; +} + +@property (readonly) NSString* name; +@property (readonly) int score; +@property (readonly) NSDate* date; +- (id)initWithName:(NSString*)name score:(int)score date:(NSDate*)date; + +@end diff --git a/Classes/Highscore.m b/Classes/Highscore.m new file mode 100755 index 0000000..7239cca --- /dev/null +++ b/Classes/Highscore.m @@ -0,0 +1,37 @@ +// +// Highscore.m +// Cart Collect +// +// Created by iD Student Account on 7/20/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import "Highscore.h" + + +@implementation Highscore + +@synthesize name, score, date; + +- (id)initWithName:(NSString*)m_name score:(int)m_score date:(NSDate*)m_date +{ + self = [super init]; + + if (nil != self) + { + name = [m_name retain]; + score = m_score; + date = [m_date retain]; + } + + return self; +} + +- (void)dealloc +{ + [name release]; + [date release]; + [super dealloc]; +} + +@end diff --git a/Classes/HighscoreListController.h b/Classes/HighscoreListController.h new file mode 100755 index 0000000..f673609 --- /dev/null +++ b/Classes/HighscoreListController.h @@ -0,0 +1,32 @@ +// +// HighscoreListController.h +// Cart Collect +// +// Created by iD Student Account on 7/20/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import +#import "Highscore.h" +#import "cocoslive.h" +#import "RootViewController.h" + +@interface HighscoreListController : UITableViewController { + UINavigationBar* navigationBar; + UINavigationItem* myNavigationItem; + NSArray* localHighscores; + NSArray* globalHighscores; + BOOL showGlobal; + BOOL loadingGlobal; + UIView* loadingView; + UIActivityIndicatorView* activity; + UILabel* statusText; + UITableView* tableView; +} + +- (void)back; +- (void)switchLists:(id)sender; +- (void)scoreRequestOk:(id)sender; +- (void)scoreRequestFail:(id)sender; + +@end diff --git a/Classes/HighscoreListController.m b/Classes/HighscoreListController.m new file mode 100755 index 0000000..9980727 --- /dev/null +++ b/Classes/HighscoreListController.m @@ -0,0 +1,371 @@ +// +// HighscoreListController.m +// Cart Collect +// +// Created by iD Student Account on 7/20/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import "HighscoreListController.h" + + +@implementation HighscoreListController + + +#pragma mark - +#pragma mark Initialization + +- (id)initWithStyle:(UITableViewStyle)style { + // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. + self = [super initWithStyle:style]; + if (self) { + NSMutableArray* highscores = [NSMutableArray arrayWithCapacity:15]; + const char* sqlQuery = "SELECT * FROM highscores ORDER BY score DESC LIMIT 15"; + sqlite3_stmt* compiled_statement; + + if (sqlite3_prepare_v2([Cart_CollectAppDelegate database], sqlQuery, -1, &compiled_statement, NULL) == SQLITE_OK) + { + while (sqlite3_step(compiled_statement) == SQLITE_ROW) + { + NSString* name = [NSString stringWithUTF8String:(char*)sqlite3_column_text(compiled_statement, 1)]; + int score = sqlite3_column_int(compiled_statement, 2); + + NSDate* date = nil; + char* dateStr = (char*)sqlite3_column_text(compiled_statement, 3); + if (dateStr != NULL) + { + NSString* theDate = [NSString stringWithUTF8String:dateStr]; + NSDateComponents* comps = [[NSDateComponents alloc] init]; + [comps setYear:[[theDate substringToIndex:4] intValue]]; + [comps setMonth:[[theDate substringWithRange:NSMakeRange(5, 2)] intValue]]; + [comps setDay:[[theDate substringWithRange:NSMakeRange(8, 2)] intValue]]; + [comps setHour:[[theDate substringWithRange:NSMakeRange(11, 2)] intValue]]; + [comps setMinute:[[theDate substringWithRange:NSMakeRange(14, 2)] intValue]]; + [comps setSecond:[[theDate substringWithRange:NSMakeRange(17, 2)] intValue]]; + date = [[NSCalendar currentCalendar] dateFromComponents:comps]; + date = [date dateByAddingTimeInterval:[[NSTimeZone localTimeZone] secondsFromGMT]]; + [comps release]; + } + + Highscore* highscore = [[Highscore alloc] initWithName:name score:score date:date]; + [highscores addObject:highscore]; + [highscore release]; + } + } + + localHighscores = [highscores copy]; + + navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; + myNavigationItem = [[UINavigationItem alloc] initWithTitle:@"Highscores"]; + UIBarButtonItem* barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(back)]; + myNavigationItem.leftBarButtonItem = barButton; + [barButton release]; + + UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Local", @"Global", nil]]; + segmentedControl.selectedSegmentIndex = 0; + segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; + [segmentedControl addTarget:self action:@selector(switchLists:) forControlEvents:UIControlEventValueChanged]; + UIBarButtonItem* barButton2 = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl]; + [segmentedControl release]; + myNavigationItem.rightBarButtonItem = barButton2; + [barButton2 release]; + + [navigationBar pushNavigationItem:myNavigationItem animated:NO]; + + showGlobal = NO; + loadingGlobal = NO; + + tableView = [(UITableView*)self.view retain]; + UIView* parentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; + [tableView setFrame:CGRectMake(0, 44, 320, 480-44)]; + [parentView addSubview:navigationBar]; + [parentView addSubview:tableView]; + self.view = parentView; + [parentView release]; + } + return self; +} + + +#pragma mark - +#pragma mark View lifecycle + +/* +- (void)viewDidLoad { + [super viewDidLoad]; + + // Uncomment the following line to display an Edit button in the navigation bar for this view controller. + // self.navigationItem.rightBarButtonItem = self.editButtonItem; +} +*/ + +/* +- (void)viewWillAppear:(BOOL)animated { + [super viewWillAppear:animated]; +} +*/ +/* +- (void)viewDidAppear:(BOOL)animated { + [super viewDidAppear:animated]; +} +*/ +/* +- (void)viewWillDisappear:(BOOL)animated { + [super viewWillDisappear:animated]; +} +*/ +/* +- (void)viewDidDisappear:(BOOL)animated { + [super viewDidDisappear:animated]; +} +*/ +/* +// Override to allow orientations other than the default portrait orientation. +- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { + // Return YES for supported orientations. + return YES; +} +*/ + + +#pragma mark - +#pragma mark Table view data source + +- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { + // Return the number of sections. + return 1; +} + + +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { + // Return the number of rows in the section. + if (showGlobal) + { + return [globalHighscores count]; + } else { + return [localHighscores count]; + } +} + + +// Customize the appearance of table view cells. +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath +{ + NSString* cellIdentifier; + Highscore* highscore; + + // Configure the cell... + if (showGlobal) + { + cellIdentifier = [NSString stringWithFormat:@"Global %d", [indexPath row]]; + highscore = [globalHighscores objectAtIndex:[indexPath row]]; + } else { + cellIdentifier = [NSString stringWithFormat:@"Local %d", [indexPath row]]; + highscore = [localHighscores objectAtIndex:[indexPath row]]; + } + + UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; + if (cell == nil) { + cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] autorelease]; + } + + cell.textLabel.text = highscore.name; + + if (highscore.date != nil) + { + NSDate* todayDate = [NSDate date]; + double ti = [highscore.date timeIntervalSinceDate:todayDate]; + ti = ti * -1; + + if (ti < 1) + { + cell.detailTextLabel.text = @"What is this I don't even"; + } else if (ti < 60) + { + cell.detailTextLabel.text = @"Less than a minute ago"; + } else if (ti < 3600) + { + int diff = round(ti / 60); + cell.detailTextLabel.text = [NSString stringWithFormat:@"%d minutes ago", diff]; + } else if (ti < 86400) + { + int diff = round(ti / 60 / 60); + cell.detailTextLabel.text = [NSString stringWithFormat:@"%d hours ago", diff]; + } else { + NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; + [dateFormatter setTimeStyle:NSDateFormatterNoStyle]; + [dateFormatter setDateStyle:NSDateFormatterLongStyle]; + cell.detailTextLabel.text = [dateFormatter stringFromDate:highscore.date]; + [dateFormatter release]; + } + } + + UILabel* scoreLabel = [[UILabel alloc] init]; + scoreLabel.text = [NSString stringWithFormat:@"%d", highscore.score]; + CGSize labelSize = [scoreLabel.text sizeWithFont:scoreLabel.font constrainedToSize:CGSizeMake(160, 44) lineBreakMode:UILineBreakModeClip]; + scoreLabel.frame = CGRectMake(320-10-labelSize.width, 22-labelSize.height/2, labelSize.width, labelSize.height); + [cell addSubview:scoreLabel]; + [scoreLabel release]; + + cell.selectionStyle = UITableViewCellSelectionStyleNone; + + return cell; +} + + +/* +// Override to support conditional editing of the table view. +- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { + // Return NO if you do not want the specified item to be editable. + return YES; +} +*/ + + +/* +// Override to support editing the table view. +- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { + + if (editingStyle == UITableViewCellEditingStyleDelete) { + // Delete the row from the data source. + [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; + } + else if (editingStyle == UITableViewCellEditingStyleInsert) { + // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. + } +} +*/ + + +/* +// Override to support rearranging the table view. +- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { +} +*/ + + +/* +// Override to support conditional rearranging of the table view. +- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { + // Return NO if you do not want the item to be re-orderable. + return YES; +} +*/ + + +#pragma mark - +#pragma mark Table view delegate + +- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { + // Navigation logic may go here. Create and push another view controller. + /* + <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil]; + // ... + // Pass the selected object to the new view controller. + [self.navigationController pushViewController:detailViewController animated:YES]; + [detailViewController release]; + */ +} + + +#pragma mark - +#pragma mark Memory management + +- (void)didReceiveMemoryWarning { + // Releases the view if it doesn't have a superview. + [super didReceiveMemoryWarning]; + + // Relinquish ownership any cached data, images, etc. that aren't in use. +} + +- (void)viewDidUnload { + // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand. + // For example: self.myOutlet = nil; +} + + +- (void)dealloc { + [super dealloc]; +} + +- (void)back +{ + RootViewController* viewController = [[[UIApplication sharedApplication] delegate] viewController]; + [[[[UIApplication sharedApplication] delegate] window] setRootViewController:viewController]; +} + +- (void)switchLists:(id)sender +{ + if ([(UISegmentedControl*)sender selectedSegmentIndex] == 0) + { + if (loadingGlobal) + { + [loadingView removeFromSuperview]; + [self.view addSubview:tableView]; + } + + showGlobal = NO; + [tableView reloadData]; + } else { + if (globalHighscores == nil) + { + loadingGlobal = YES; + + loadingView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 480-44)]; + activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(150, 228-44, 20, 20)]; + activity.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray; + statusText = [[UILabel alloc] initWithFrame:CGRectMake(0, 256-44, 320, 21)]; + statusText.text = @"Downloading highscores..."; + statusText.textAlignment = UITextAlignmentCenter; + + [loadingView addSubview:activity]; + [loadingView addSubview:statusText]; + [loadingView setBackgroundColor:[UIColor whiteColor]]; + [activity startAnimating]; + [tableView removeFromSuperview]; + [self.view addSubview:loadingView]; + + CLScoreServerRequest* request = [[CLScoreServerRequest alloc] initWithGameName:@"Cart Collect" delegate:self]; + tQueryFlags flags = kQueryFlagIgnore; + [request requestScores:kQueryAllTime limit:15 offset:0 flags:flags category:@"Classic"]; + [request release]; + } else { + showGlobal = YES; + [tableView reloadData]; + } + } +} + +- (void)scoreRequestOk:(id)sender +{ + NSArray* highscores = [sender parseScores]; + NSMutableArray* highscoreTemp = [[NSMutableArray alloc] init]; + + for (NSDictionary* data in highscores) + { + NSString* name = [data objectForKey:@"cc_playername"]; + int score = [[data objectForKey:@"cc_score"] intValue]; + NSDate* date = [NSDate dateWithTimeIntervalSince1970:[[data objectForKey:@"cc_when"] intValue]]; + Highscore* highscore = [[Highscore alloc] initWithName:name score:score date:date]; + [highscoreTemp addObject:highscore]; + [highscore release]; + } + + globalHighscores = [highscoreTemp copy]; + [highscoreTemp release]; + + loadingGlobal = NO; + [loadingView removeFromSuperview]; + [self.view addSubview:tableView]; + showGlobal = YES; + [tableView reloadData]; +} + +- (void)scoreRequestFail:(id)sender +{ + [activity stopAnimating]; + [statusText setText:@"Unable to download highscores."]; +} + +@end + diff --git a/Classes/MainMenuLayer.h b/Classes/MainMenuLayer.h new file mode 100755 index 0000000..fc4f82f --- /dev/null +++ b/Classes/MainMenuLayer.h @@ -0,0 +1,24 @@ +// +// MainMenuLayer.h +// Cart Collect +// +// Created by iD Student Account on 7/20/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import +#import "cocos2d.h" +#import "HighscoreListController.h" + +@class GameLayer; + +@interface MainMenuLayer : CCLayer { + +} + ++ (CCScene*)scene; +- (id)init; +- (void)newgame; +- (void)highscores; + +@end diff --git a/Classes/MainMenuLayer.m b/Classes/MainMenuLayer.m new file mode 100755 index 0000000..ac7f4a7 --- /dev/null +++ b/Classes/MainMenuLayer.m @@ -0,0 +1,68 @@ +// +// MainMenuLayer.m +// Cart Collect +// +// Created by iD Student Account on 7/20/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import "MainMenuLayer.h" + + +@implementation MainMenuLayer + ++ (CCScene*)scene +{ + CCScene* scene = [CCScene node]; + + MainMenuLayer* layer = [MainMenuLayer node]; + [scene addChild:layer]; + + return scene; +} + +- (id)init +{ + self = [super init]; + + if (nil != self) + { + CCSprite* backgroundImage = [CCSprite spriteWithFile:@"SeaBeach.png"]; + backgroundImage.position = ccp(240,160); + [self addChild:backgroundImage]; + + CCLabelBMFont* titleText = [CCLabelBMFont labelWithString:@"Cart Collect - The Game!" fntFile:@"getoffthatboatrightnowyounglady.fnt"]; + titleText.position = ccp(240, 320-64); + [self addChild:titleText]; + + //CCLabelBMFont* menuItemLabel1 = [CCLabelBMFont labelWithString:@"New Game" fntFile:@"getoffthatboatrightnowyounglady.fnt"]; + //CCMenuItemLabel* menuItem1 = [CCMenuItemLabel itemWithLabel:menuItemLabel1 target:self selector:@selector(newgame)]; + + //CCLabelBMFont* menuItemLabel2 = [CCLabelBMFont labelWithString:@"Highscores" fntFile:@"getoffthatboatrightnowyounglady.fnt"]; + //CCMenuItemLabel* menuItem2 = [CCMenuItemLabel itemWithLabel:menuItemLabel2 target:self selector:@selector(highscores)]; + + CCMenuItemImage* menuItem1 = [CCMenuItemImage itemFromNormalImage:@"newgame.png" selectedImage:@"newgame2.png" target:self selector:@selector(newgame)]; + CCMenuItemImage* menuItem2 = [CCMenuItemImage itemFromNormalImage:@"highscores.png" selectedImage:@"highscores2.png" target:self selector:@selector(highscores)]; + + CCMenu* menu = [CCMenu menuWithItems:menuItem1, menuItem2, nil]; + [menu alignItemsVertically]; + menu.position = ccp(240, 100); + [self addChild:menu]; + } + + return self; +} + +- (void)newgame +{ + [[CCDirector sharedDirector] replaceScene:[GameLayer scene]]; +} + +- (void)highscores +{ + HighscoreListController* listController = [[HighscoreListController alloc] initWithStyle:UITableViewStylePlain]; + [[[[UIApplication sharedApplication] delegate] window] setRootViewController:listController]; + [listController release]; +} + +@end diff --git a/Classes/OneUp.h b/Classes/OneUp.h new file mode 100755 index 0000000..8bd4c66 --- /dev/null +++ b/Classes/OneUp.h @@ -0,0 +1,18 @@ +// +// OneUp.h +// Cart Collect +// +// Created by iD Student Account on 7/19/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import +#import "FallingObject.h" + +@interface OneUp : FallingObject { + +} + +- (id)init; + +@end diff --git a/Classes/OneUp.m b/Classes/OneUp.m new file mode 100755 index 0000000..e09935c --- /dev/null +++ b/Classes/OneUp.m @@ -0,0 +1,27 @@ +// +// OneUp.m +// Cart Collect +// +// Created by iD Student Account on 7/19/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import "OneUp.h" + + +@implementation OneUp + +- (id)init +{ + self = [super init]; + + if (nil != self) + { + sprite = [CCSprite spriteWithFile:@"oneup.png"]; + weight = 10; + } + + return self; +} + +@end diff --git a/Classes/PauseLayer.h b/Classes/PauseLayer.h new file mode 100755 index 0000000..aae5d6c --- /dev/null +++ b/Classes/PauseLayer.h @@ -0,0 +1,22 @@ +// +// PauseLayer.h +// Cart Collect +// +// Created by iD Student Account on 7/20/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import +#import "cocos2d.h" +#import "GameLayer.h" + +@interface PauseLayer : CCLayer { + CCScene* game; +} + ++ (CCScene*)sceneWithScene:(CCScene*)scene; +- (id)initWithScene:(CCScene*)scene; +- (void)unpause; +- (void)newgame; + +@end diff --git a/Classes/PauseLayer.m b/Classes/PauseLayer.m new file mode 100755 index 0000000..53574b2 --- /dev/null +++ b/Classes/PauseLayer.m @@ -0,0 +1,68 @@ +// +// PauseLayer.m +// Cart Collect +// +// Created by iD Student Account on 7/20/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import "PauseLayer.h" + +@implementation PauseLayer + ++ (CCScene*)sceneWithScene:(CCScene*)scene2 +{ + CCScene* scene = [CCScene node]; + + CCLayerColor* backgroundLayer = [CCLayerColor layerWithColor:ccc4(255, 255, 255, 255)]; + CCSprite* backgroundImage = [CCSprite spriteWithFile:@"SeaBeach.png"]; + backgroundImage.position = ccp(240,160); + [backgroundLayer addChild:backgroundImage]; + [scene addChild:backgroundLayer]; + + CCLayerColor* backgroundLayer2 = [CCLayerColor layerWithColor:ccc4(0, 0, 0, 127)]; + [scene addChild:backgroundLayer2]; + + PauseLayer* layer = [[[PauseLayer alloc] initWithScene:scene2] autorelease]; + [scene addChild:layer]; + + return scene; +} + +- (id)initWithScene:(CCScene*)scene +{ + self = [super init]; + + if (nil != self) + { + game = [scene retain]; + + CCLabelBMFont* scoreLabel = [CCLabelBMFont labelWithString:@"PAUSE" fntFile:@"helvetica.fnt"]; + scoreLabel.position = ccp(240,90); + [self addChild:scoreLabel]; + + CCMenuItemImage* pauseButton = [CCMenuItemImage itemFromNormalImage:@"pause2.png" selectedImage:@"pause.png" target:self selector:@selector(unpause)]; + CCMenu* pauseMenu = [CCMenu menuWithItems:pauseButton, nil]; + [pauseMenu setPosition:ccp(480-8-16, 320-8-16)]; + [self addChild:pauseMenu]; + + CCMenuItemImage* newgameMenuItem = [CCMenuItemImage itemFromNormalImage:@"back.png" selectedImage:@"back2.png" target:self selector:@selector(newgame)]; + CCMenu* myMenu = [CCMenu menuWithItems:newgameMenuItem, nil]; + myMenu.position = ccp(240, 60); + [self addChild:myMenu]; + } + + return self; +} + +- (void)unpause +{ + [[CCDirector sharedDirector] replaceScene:game]; +} + +- (void)newgame +{ + [[CCDirector sharedDirector] replaceScene:[MainMenuLayer scene]]; +} + +@end diff --git a/Classes/Rock.h b/Classes/Rock.h new file mode 100755 index 0000000..7382d99 --- /dev/null +++ b/Classes/Rock.h @@ -0,0 +1,18 @@ +// +// Rock.h +// Cart Collect +// +// Created by iD Student Account on 7/19/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import +#import "FallingObject.h" + +@interface Rock : FallingObject { + +} + +- (id)init; + +@end diff --git a/Classes/Rock.m b/Classes/Rock.m new file mode 100755 index 0000000..a3b3b9e --- /dev/null +++ b/Classes/Rock.m @@ -0,0 +1,27 @@ +// +// Rock.m +// Cart Collect +// +// Created by iD Student Account on 7/19/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import "Rock.h" + + +@implementation Rock + +- (id)init +{ + self = [super init]; + + if (nil != self) + { + sprite = [CCSprite spriteWithFile:@"rock.png"]; + weight = 7; + } + + return self; +} + +@end diff --git a/Classes/RootViewController.h b/Classes/RootViewController.h new file mode 100755 index 0000000..956c133 --- /dev/null +++ b/Classes/RootViewController.h @@ -0,0 +1,16 @@ +// +// RootViewController.h +// Cart Collect +// +// Created by iD Student Account on 7/18/11. +// Copyright __MyCompanyName__ 2011. All rights reserved. +// + +#import + + +@interface RootViewController : UIViewController { + +} + +@end diff --git a/Classes/RootViewController.m b/Classes/RootViewController.m new file mode 100755 index 0000000..edd67cb --- /dev/null +++ b/Classes/RootViewController.m @@ -0,0 +1,152 @@ +// +// RootViewController.m +// Cart Collect +// +// Created by iD Student Account on 7/18/11. +// Copyright __MyCompanyName__ 2011. All rights reserved. +// + +// +// RootViewController + iAd +// If you want to support iAd, use this class as the controller of your iAd +// + +#import "cocos2d.h" + +#import "RootViewController.h" +#import "GameConfig.h" + +@implementation RootViewController + +/* + // The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. + - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { + if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { + // Custom initialization + } + return self; + } + */ + +/* + // Implement loadView to create a view hierarchy programmatically, without using a nib. + - (void)loadView { + } + */ + +/* + // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. + - (void)viewDidLoad { + [super viewDidLoad]; + } + */ + + +// Override to allow orientations other than the default portrait orientation. +- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { + + // + // There are 2 ways to support auto-rotation: + // - The OpenGL / cocos2d way + // - Faster, but doesn't rotate the UIKit objects + // - The ViewController way + // - A bit slower, but the UiKit objects are placed in the right place + // + +#if GAME_AUTOROTATION==kGameAutorotationNone + // + // EAGLView won't be autorotated. + // Since this method should return YES in at least 1 orientation, + // we return YES only in the Portrait orientation + // + return ( interfaceOrientation == kCCDeviceOrientationLandscapeLeft ); + +#elif GAME_AUTOROTATION==kGameAutorotationCCDirector + // + // EAGLView will be rotated by cocos2d + // + // Sample: Autorotate only in landscape mode + // + if( interfaceOrientation == UIInterfaceOrientationLandscapeLeft ) { + [[CCDirector sharedDirector] setDeviceOrientation: kCCDeviceOrientationLandscapeRight]; + } else if( interfaceOrientation == UIInterfaceOrientationLandscapeRight) { + [[CCDirector sharedDirector] setDeviceOrientation: kCCDeviceOrientationLandscapeLeft]; + } + + // Since this method should return YES in at least 1 orientation, + // we return YES only in the Portrait orientation + return ( interfaceOrientation == UIInterfaceOrientationPortrait ); + +#elif GAME_AUTOROTATION == kGameAutorotationUIViewController + // + // EAGLView will be rotated by the UIViewController + // + // Sample: Autorotate only in landscpe mode + // + // return YES for the supported orientations + + return ( UIInterfaceOrientationIsLandscape( interfaceOrientation ) ); + +#else +#error Unknown value in GAME_AUTOROTATION + +#endif // GAME_AUTOROTATION + + + // Shold not happen + return NO; +} + +// +// This callback only will be called when GAME_AUTOROTATION == kGameAutorotationUIViewController +// +#if GAME_AUTOROTATION == kGameAutorotationUIViewController +-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration +{ + // + // Assuming that the main window has the size of the screen + // BUG: This won't work if the EAGLView is not fullscreen + /// + CGRect screenRect = [[UIScreen mainScreen] bounds]; + CGRect rect; + + if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) + rect = screenRect; + + else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) + rect.size = CGSizeMake( screenRect.size.height, screenRect.size.width ); + + CCDirector *director = [CCDirector sharedDirector]; + EAGLView *glView = [director openGLView]; + float contentScaleFactor = [director contentScaleFactor]; + + if( contentScaleFactor != 1 ) { + rect.size.width *= contentScaleFactor; + rect.size.height *= contentScaleFactor; + } + glView.frame = rect; +} +#endif // GAME_AUTOROTATION == kGameAutorotationUIViewController + + +- (void)didReceiveMemoryWarning { + // Releases the view if it doesn't have a superview. + [super didReceiveMemoryWarning]; + + // Release any cached data, images, etc that aren't in use. +} + +- (void)viewDidUnload { + [super viewDidUnload]; + // Release any retained subviews of the main view. + // e.g. self.myOutlet = nil; +} + + +- (void)dealloc { + [super dealloc]; +} + + +@end + diff --git a/Classes/ValuableObject.h b/Classes/ValuableObject.h new file mode 100755 index 0000000..ccefbbd --- /dev/null +++ b/Classes/ValuableObject.h @@ -0,0 +1,15 @@ +// +// ValuableObject.h +// Cart Collect +// +// Created by iD Student Account on 7/20/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import + +@protocol ValuableObject + +- (int)pointValue; + +@end -- cgit 1.4.1