From 9f4bf94a763afba795fb991006081505c3ae466b Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Thu, 25 Aug 2011 13:30:00 -0400 Subject: Added Jump to selection screen It currently uses a temporary picture and is unlockable after reaching 5000 points in Collect, at which point a little notification appears at the top of the screen telling you that you've unlocked a new game mode. Also fixed a bug in GameMode with the iteration of objects that would cause the rest of tick to not be called because objects were removed from the set while it was being iterated over. Refs #204 --- Cartographic.xcodeproj/project.pbxproj | 34 ++- Classes/Cart_CollectAppDelegate.m | 4 + Classes/ClassicGameMode.m | 12 + Classes/GameMode.m | 5 +- Classes/GameModeSelectionLayer.m | 15 + Resources/venice.png | Bin 0 -> 1329 bytes libs/cocos2d/CCNotifications.h | 141 ++++++++++ libs/cocos2d/CCNotifications.m | 494 +++++++++++++++++++++++++++++++++ libs/cocos2d/notificationDesign.h | 39 +++ libs/cocos2d/notificationDesign.m | 82 ++++++ 10 files changed, 816 insertions(+), 10 deletions(-) create mode 100644 Resources/venice.png create mode 100755 libs/cocos2d/CCNotifications.h create mode 100755 libs/cocos2d/CCNotifications.m create mode 100644 libs/cocos2d/notificationDesign.h create mode 100644 libs/cocos2d/notificationDesign.m diff --git a/Cartographic.xcodeproj/project.pbxproj b/Cartographic.xcodeproj/project.pbxproj index 58ab09b..61b9f11 100755 --- a/Cartographic.xcodeproj/project.pbxproj +++ b/Cartographic.xcodeproj/project.pbxproj @@ -112,16 +112,19 @@ 6C29041213EAEC8A0032DA0F /* framestuff.png in Resources */ = {isa = PBXBuildFile; fileRef = 6C29041113EAEC8A0032DA0F /* framestuff.png */; }; 6C39CFE013FC2708002B21AF /* tutorial.png in Resources */ = {isa = PBXBuildFile; fileRef = 6C39CFDF13FC2708002B21AF /* tutorial.png */; }; 6C39CFE213FC2713002B21AF /* tutorial2.png in Resources */ = {isa = PBXBuildFile; fileRef = 6C39CFE113FC2713002B21AF /* tutorial2.png */; }; + 6C39CFE513FC4635002B21AF /* JumpGameMode.m in Sources */ = {isa = PBXBuildFile; fileRef = 6C39CFE413FC4635002B21AF /* JumpGameMode.m */; }; + 6C39CFE713FC46CD002B21AF /* water.png in Resources */ = {isa = PBXBuildFile; fileRef = 6C39CFE613FC46CD002B21AF /* water.png */; }; + 6C39CFE913FC8AC4002B21AF /* jump2.png in Resources */ = {isa = PBXBuildFile; fileRef = 6C39CFE813FC8AC4002B21AF /* jump2.png */; }; + 6C39CFEB13FC8ACA002B21AF /* jump.png in Resources */ = {isa = PBXBuildFile; fileRef = 6C39CFEA13FC8ACA002B21AF /* jump.png */; }; 6C39CFF313FD4F8B002B21AF /* GameModeSelection.m in Sources */ = {isa = PBXBuildFile; fileRef = 6C39CFF213FD4F89002B21AF /* GameModeSelection.m */; }; 6C39CFF513FD5526002B21AF /* paintdaubs.png in Resources */ = {isa = PBXBuildFile; fileRef = 6C39CFF413FD5526002B21AF /* paintdaubs.png */; }; 6C39CFF813FD7638002B21AF /* GameModeSelectionLayer.m in Sources */ = {isa = PBXBuildFile; fileRef = 6C39CFF713FD7637002B21AF /* GameModeSelectionLayer.m */; }; 6C39D00813FD9037002B21AF /* florence.png in Resources */ = {isa = PBXBuildFile; fileRef = 6C39D00713FD9037002B21AF /* florence.png */; }; 6C39D01213FD9945002B21AF /* paris.png in Resources */ = {isa = PBXBuildFile; fileRef = 6C39D01113FD9945002B21AF /* paris.png */; }; - 6C39CFE513FC4635002B21AF /* JumpGameMode.m in Sources */ = {isa = PBXBuildFile; fileRef = 6C39CFE413FC4635002B21AF /* JumpGameMode.m */; }; - 6C39CFE713FC46CD002B21AF /* water.png in Resources */ = {isa = PBXBuildFile; fileRef = 6C39CFE613FC46CD002B21AF /* water.png */; }; - 6C39CFE913FC8AC4002B21AF /* jump2.png in Resources */ = {isa = PBXBuildFile; fileRef = 6C39CFE813FC8AC4002B21AF /* jump2.png */; }; - 6C39CFEB13FC8ACA002B21AF /* jump.png in Resources */ = {isa = PBXBuildFile; fileRef = 6C39CFEA13FC8ACA002B21AF /* jump.png */; }; 6C5179C613DF3839006F1F38 /* Morning1.png in Resources */ = {isa = PBXBuildFile; fileRef = 6C5179C513DF3839006F1F38 /* Morning1.png */; }; + 6C85834014055A44009EF100 /* CCNotifications.m in Sources */ = {isa = PBXBuildFile; fileRef = 6C85833F14055A44009EF100 /* CCNotifications.m */; }; + 6C85834414055B2E009EF100 /* notificationDesign.m in Sources */ = {isa = PBXBuildFile; fileRef = 6C85834314055B2E009EF100 /* notificationDesign.m */; }; + 6C85834614056E57009EF100 /* venice.png in Resources */ = {isa = PBXBuildFile; fileRef = 6C85834514056E57009EF100 /* venice.png */; }; 6CC89DD913F2F4E6003704F7 /* Entitlements.plist in Resources */ = {isa = PBXBuildFile; fileRef = 6CC89DD813F2F4E6003704F7 /* Entitlements.plist */; }; 6CC89DDF13F31413003704F7 /* TutorialMode.m in Sources */ = {isa = PBXBuildFile; fileRef = 6CC89DDE13F31413003704F7 /* TutorialMode.m */; }; DC6640030F83B3EA000B3E49 /* AudioToolbox.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DC6640020F83B3EA000B3E49 /* AudioToolbox.framework */; }; @@ -439,6 +442,11 @@ 6C29041113EAEC8A0032DA0F /* framestuff.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = framestuff.png; sourceTree = ""; }; 6C39CFDF13FC2708002B21AF /* tutorial.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = tutorial.png; sourceTree = ""; }; 6C39CFE113FC2713002B21AF /* tutorial2.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = tutorial2.png; sourceTree = ""; }; + 6C39CFE313FC4635002B21AF /* JumpGameMode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JumpGameMode.h; sourceTree = ""; }; + 6C39CFE413FC4635002B21AF /* JumpGameMode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JumpGameMode.m; sourceTree = ""; }; + 6C39CFE613FC46CD002B21AF /* water.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = water.png; sourceTree = ""; }; + 6C39CFE813FC8AC4002B21AF /* jump2.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = jump2.png; sourceTree = ""; }; + 6C39CFEA13FC8ACA002B21AF /* jump.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = jump.png; sourceTree = ""; }; 6C39CFF113FD4F87002B21AF /* GameModeSelection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GameModeSelection.h; sourceTree = ""; }; 6C39CFF213FD4F89002B21AF /* GameModeSelection.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GameModeSelection.m; sourceTree = ""; }; 6C39CFF413FD5526002B21AF /* paintdaubs.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = paintdaubs.png; sourceTree = ""; }; @@ -446,12 +454,12 @@ 6C39CFF713FD7637002B21AF /* GameModeSelectionLayer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GameModeSelectionLayer.m; sourceTree = ""; }; 6C39D00713FD9037002B21AF /* florence.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = florence.png; sourceTree = ""; }; 6C39D01113FD9945002B21AF /* paris.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = paris.png; sourceTree = ""; }; - 6C39CFE313FC4635002B21AF /* JumpGameMode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JumpGameMode.h; sourceTree = ""; }; - 6C39CFE413FC4635002B21AF /* JumpGameMode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JumpGameMode.m; sourceTree = ""; }; - 6C39CFE613FC46CD002B21AF /* water.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = water.png; sourceTree = ""; }; - 6C39CFE813FC8AC4002B21AF /* jump2.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = jump2.png; sourceTree = ""; }; - 6C39CFEA13FC8ACA002B21AF /* jump.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = jump.png; sourceTree = ""; }; 6C5179C513DF3839006F1F38 /* Morning1.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Morning1.png; sourceTree = ""; }; + 6C85833E14055A44009EF100 /* CCNotifications.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCNotifications.h; sourceTree = ""; }; + 6C85833F14055A44009EF100 /* CCNotifications.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCNotifications.m; sourceTree = ""; }; + 6C85834214055B2D009EF100 /* notificationDesign.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = notificationDesign.h; sourceTree = ""; }; + 6C85834314055B2E009EF100 /* notificationDesign.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = notificationDesign.m; sourceTree = ""; }; + 6C85834514056E57009EF100 /* venice.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = venice.png; sourceTree = ""; }; 6CB532581403BA4F00780A5E /* GameModeSelectionDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GameModeSelectionDelegate.h; sourceTree = ""; }; 6CC89DD813F2F4E6003704F7 /* Entitlements.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Entitlements.plist; sourceTree = ""; }; 6CC89DDD13F31413003704F7 /* TutorialMode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TutorialMode.h; sourceTree = ""; }; @@ -932,6 +940,7 @@ 6C39D00513FD901A002B21AF /* Icons */ = { isa = PBXGroup; children = ( + 6C85834514056E57009EF100 /* venice.png */, 6C39D01113FD9945002B21AF /* paris.png */, 6C39D00713FD9037002B21AF /* florence.png */, ); @@ -983,6 +992,8 @@ E02BBB19126CC2F5006E46A2 /* cocos2d */ = { isa = PBXGroup; children = ( + 6C85833E14055A44009EF100 /* CCNotifications.h */, + 6C85833F14055A44009EF100 /* CCNotifications.m */, E02BBB1A126CC2F5006E46A2 /* CCAction.h */, E02BBB1B126CC2F5006E46A2 /* CCAction.m */, E02BBB1C126CC2F5006E46A2 /* CCActionCamera.h */, @@ -1103,6 +1114,8 @@ E02BBB93126CC2F6006E46A2 /* cocos2d.m */, E02BBB94126CC2F6006E46A2 /* Platforms */, E02BBBAD126CC2F6006E46A2 /* Support */, + 6C85834214055B2D009EF100 /* notificationDesign.h */, + 6C85834314055B2E009EF100 /* notificationDesign.m */, ); name = cocos2d; path = libs/cocos2d; @@ -1454,6 +1467,7 @@ 6C39CFE713FC46CD002B21AF /* water.png in Resources */, 6C39CFE913FC8AC4002B21AF /* jump2.png in Resources */, 6C39CFEB13FC8ACA002B21AF /* jump.png in Resources */, + 6C85834614056E57009EF100 /* venice.png in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -1505,6 +1519,8 @@ 6C18C3D314033DC60005AA4C /* NMPanelMenu.m in Sources */, 6C18C3D414033DC60005AA4C /* TouchDelegatingView.m in Sources */, 6C39CFE513FC4635002B21AF /* JumpGameMode.m in Sources */, + 6C85834014055A44009EF100 /* CCNotifications.m in Sources */, + 6C85834414055B2E009EF100 /* notificationDesign.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Classes/Cart_CollectAppDelegate.m b/Classes/Cart_CollectAppDelegate.m index ba9d60b..1a6759c 100755 --- a/Classes/Cart_CollectAppDelegate.m +++ b/Classes/Cart_CollectAppDelegate.m @@ -14,6 +14,8 @@ #import "RootViewController.h" #import "MainMenuLayer.h" #import "TestFlight.h" +#import "CCNotifications.h" +#import "notificationDesign.h" @implementation Cart_CollectAppDelegate @@ -64,6 +66,8 @@ CCDirector *director = [CCDirector sharedDirector]; + CCNotifications* notifications = [CCNotifications sharedManager]; + [director setNotificationNode:notifications]; // Init the View Controller viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil]; diff --git a/Classes/ClassicGameMode.m b/Classes/ClassicGameMode.m index 88c52ce..c1d3be7 100755 --- a/Classes/ClassicGameMode.m +++ b/Classes/ClassicGameMode.m @@ -14,6 +14,7 @@ #import "Rock.h" #import "GameOverScene.h" #import "SimpleAudioEngine.h" +#import "CCNotifications.h" @implementation ClassicGameMode @@ -35,6 +36,17 @@ [self unschedule:@selector(randomlyAddObject:)]; [self schedule:@selector(randomlyAddObject:) interval:0.6f]; addSpeed = 0.6f; + } else if ((lastScore < 5000) && (score >= 5000)) + { + NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; + + if (![defaults boolForKey:@"unlockedJumpMode"]) + { + [[CCNotifications sharedManager] addWithTitle:@"Jump" message:@"You've unlocked a new game mode!" image:@"venice.png"]; + + [defaults setBool:YES forKey:@"unlockedJumpMode"]; + [defaults synchronize]; + } } else if ((lastScore < 4500) && (score >= 4500)) { [self unschedule:@selector(randomlyAddObject:)]; diff --git a/Classes/GameMode.m b/Classes/GameMode.m index 8f695d1..af9b3bd 100644 --- a/Classes/GameMode.m +++ b/Classes/GameMode.m @@ -84,13 +84,16 @@ { [cart tick]; + NSMutableSet* discardedObjects = [NSMutableSet set]; for (FallingObject* object in objects) { if ([object tick]) { - [objects removeObject:object]; + [discardedObjects addObject:object]; } } + + [objects minusSet:discardedObjects]; } - (BOOL)canPause diff --git a/Classes/GameModeSelectionLayer.m b/Classes/GameModeSelectionLayer.m index db08146..3fbf417 100644 --- a/Classes/GameModeSelectionLayer.m +++ b/Classes/GameModeSelectionLayer.m @@ -14,6 +14,7 @@ #import "TutorialMode.h" #import "ClassicGameMode.h" #import "NMPanelMenu.h" +#import "JumpGameMode.h" @implementation GameModeSelectionLayer @@ -56,6 +57,17 @@ [gameModes addObject:collectSelection]; + GameModeSelection* jumpSelection; + + if ([defaults boolForKey:@"unlockedJumpMode"]) + { + jumpSelection = [GameModeSelection selectionWithName:@"Jump" location:@"Venice" filename:@"venice" unlocked:YES]; + } else { + jumpSelection = [GameModeSelection selectionWithName:@"Jump" location:@"Venice" filename:@"venice" unlockCondition:@"Get 5000 points in Collect!"]; + } + + [gameModes addObject:jumpSelection]; + CCMenu* menu = [CCMenu menuWithItems:nil]; float onePanelWide = 128; float padding = 15; @@ -126,6 +138,9 @@ } else if ([gameMode.name isEqual:@"Collect"]) { [[CCDirector sharedDirector] replaceScene:[ClassicGameMode scene]]; + } else if ([gameMode.name isEqual:@"Jump"]) + { + [[CCDirector sharedDirector] replaceScene:[JumpGameMode scene]]; } } diff --git a/Resources/venice.png b/Resources/venice.png new file mode 100644 index 0000000..512c2b4 Binary files /dev/null and b/Resources/venice.png differ diff --git a/libs/cocos2d/CCNotifications.h b/libs/cocos2d/CCNotifications.h new file mode 100755 index 0000000..8609801 --- /dev/null +++ b/libs/cocos2d/CCNotifications.h @@ -0,0 +1,141 @@ +/* + * CCNotifications + * + * Copyright (c) 2010 ForzeField Studios S.L. + * http://forzefield.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + +#import +#import "cocos2d.h" + +#define KNOTIFICATIONMIN_SCALE 0.0001f + +@interface ccNotificationData : NSObject +{ + NSString *title_; + NSString *message_; + id media_; + int mediaType_; + int tag_; + BOOL animated_; +} +@property(nonatomic, retain) NSString *title; +@property(nonatomic, retain) NSString *message; +@property(nonatomic, retain) id media; +@property(nonatomic, readwrite, assign) int mediaType; +@property(nonatomic, readwrite, assign) int tag; +@property(nonatomic, readwrite, assign) BOOL animated; + +@end + +@protocol CCNotificationsDelegate +@optional +- (void) notification:(ccNotificationData*)notification newState:(char)state; +- (BOOL) touched:(int)tag; +- (void) notificationChangeState:(char)state tag:(int)tag DEPRECATED_ATTRIBUTE; +@end + +@protocol CCNotificationDesignProtocol +- (void) setTitle:(NSString*)title message:(NSString*)message texture:(CCTexture2D*)texture; +@end + +enum +{ + kCCNotificationStateHide, + kCCNotificationStateAnimationOut, + kCCNotificationStateShowing, + kCCNotificationStateAnimationIn, +}; + +enum +{ + kCCNotificationPositionBottom, + kCCNotificationPositionTop, +}; + +enum +{ + kCCNotificationAnimationMovement, + kCCNotificationAnimationScale, +}; + +enum +{ + kCCNotificationMediaPath, + kCCNotificationMediaTexture, +}; + +#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED +@interface CCNotifications : NSObject +#elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED) +@interface CCNotifications : NSObject +#endif +{ + id delegate_; + CCNode *template_; + char state_; + char position_; + ccTime showingTime_; + ccTime timeAnimationIn_; + ccTime timeAnimationOut_; + char typeAnimationIn_; + char typeAnimationOut_; + + //Caching + CCArray *cachedNotifications_; + ccNotificationData *currentNotification_; + + CCActionInterval *animationIn_; + CCActionInterval *animationOut_; +} +@property(nonatomic, retain) id delegate; +@property(nonatomic, retain) CCNode *notificationDesign; +@property(nonatomic, retain) CCActionInterval *animationIn; +@property(nonatomic, retain) CCActionInterval *animationOut; +@property(nonatomic, retain) ccNotificationData *currentNotification; +@property(nonatomic, readwrite, assign) char position; +@property(nonatomic, readwrite, assign) ccTime showingTime; + ++ (CCNotifications *) sharedManager; ++ (void) purgeSharedManager; ++ (id) systemWithTemplate:(CCNode *)notifications; + +- (id) initWithTemplate:(CCNode *)templates; +- (void) setAnimationIn:(char)type time:(ccTime)time; +- (void) setAnimationOut:(char)type time:(ccTime)time; +- (void) setAnimation:(char)type time:(ccTime)time; +- (void) updateAnimations; + +- (ccNotificationData*) addWithTitle:(NSString*)title message:(NSString*)message image:(NSString*)image tag:(int)tag animate:(BOOL)animate waitUntilDone:(BOOL)isCached; +- (ccNotificationData*) addWithTitle:(NSString*)title message:(NSString*)message texture:(CCTexture2D*)texture tag:(int)tag animate:(BOOL)animate waitUntilDone:(BOOL)isCached; + +- (ccNotificationData*) addWithTitle:(NSString*)title message:(NSString*)message image:(NSString*)image tag:(int)tag animate:(BOOL)animate; +- (ccNotificationData*) addWithTitle:(NSString*)title message:(NSString*)message texture:(CCTexture2D*)texture tag:(int)tag animate:(BOOL)animate; + +- (ccNotificationData*) addWithTitle:(NSString*)title message:(NSString*)message image:(NSString*)image; +- (ccNotificationData*) addWithTitle:(NSString*)title message:(NSString*)message texture:(CCTexture2D*)texture; + +- (void) addSafelyWithTitle:(NSString*)title message:(NSString*)message image:(NSString*)image tag:(int)tag animate:(BOOL)animate DEPRECATED_ATTRIBUTE; + + +- (void) visit; +@end \ No newline at end of file diff --git a/libs/cocos2d/CCNotifications.m b/libs/cocos2d/CCNotifications.m new file mode 100755 index 0000000..235a6a0 --- /dev/null +++ b/libs/cocos2d/CCNotifications.m @@ -0,0 +1,494 @@ +/* + * CCNotifications + * + * Copyright (c) 2010 ForzeField Studios S.L. + * http://forzefield.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + + +#import "CCNotifications.h" +#import "CCArray.h" +#import "notificationDesign.h" + + +@implementation ccNotificationData +@synthesize title = title_; +@synthesize message = message_; +@synthesize media = media_; +@synthesize mediaType = mediaType_; +@synthesize tag = tag_; +@synthesize animated = animated_; + + +- (void) dealloc +{ + [self setTitle:nil]; + [self setMessage:nil]; + [self setMedia:nil]; + [super dealloc]; +} + + +@end + +@interface CCNotifications (Private) + +- (void) _updateAnimationIn; +- (void) _updateAnimationOut; +- (CCActionInterval*) _animation:(char)type time:(ccTime)time; +- (void) _showNotification; +- (void) _addNotificationToArray:(ccNotificationData*)data cached:(BOOL)isCached; +- (void) _startScheduler; +- (void) _hideNotification; +- (void) _hideNotificationScheduler; +- (void) registerWithTouchDispatcher; +- (void) _setState:(char)states; + +@end + + +@implementation CCNotifications +@synthesize position = position_; +@synthesize notificationDesign = template_; +@synthesize animationIn = animationIn_; +@synthesize animationOut = animationOut_; +@synthesize delegate = delegate_; +@synthesize showingTime = showingTime_; +@synthesize currentNotification = currentNotification_; + +static CCNotifications *sharedManager; + ++ (CCNotifications *)sharedManager +{ + if (!sharedManager) + sharedManager = [[CCNotifications alloc] init]; + + return sharedManager; +} + ++ (id) alloc +{ + NSAssert(sharedManager == nil, @"Attempted to allocate a second instance of a singleton."); + return [super alloc]; +} + ++ (id) systemWithTemplate:(CCNode *)notifications +{ + NSAssert(sharedManager == nil, @"Attempted to allocate a second instance of a singleton. You should use setTemplate"); + sharedManager = [[CCNotifications alloc] initWithTemplate:notifications]; + + return sharedManager; +} + ++ (void) purgeSharedManager +{ + [sharedManager release]; +} + +- (id) init +{ + CCNode *templates = [[[CCNotificationDefaultDesign alloc] init] autorelease]; + self = [self initWithTemplate:templates]; + return self; +} + +- (id) initWithTemplate:(CCNode *)templates +{ + if( (self = [super init]) ) { + self.notificationDesign = templates; + + delegate_ = nil; + state_ = kCCNotificationStateHide; + typeAnimationIn_ = kCCNotificationAnimationMovement; + typeAnimationOut_ = kCCNotificationAnimationMovement; + timeAnimationIn_ = 0.0f; + timeAnimationOut_ = 0.0f; + + cachedNotifications_ = [[CCArray alloc] initWithCapacity:4]; + + //Default settings + showingTime_ = 4.0f; + position_ = kCCNotificationPositionTop; + + [self setAnimation:kCCNotificationAnimationMovement time:0.5f]; + //[self setAnimationIn:kCCNotificationAnimationMovement time:0.5f]; + //[self setAnimationOut:kCCNotificationAnimationScale time:0.5f]; + } + return self; +} + +- (void) _setState:(char)states +{ + if(state_==states) return; + state_ = states; + + if([delegate_ respondsToSelector:@selector(notification:newState:)]) + [delegate_ notification:currentNotification_ newState:state_]; + + if([delegate_ respondsToSelector:@selector(notificationChangeState:tag:)]) + [delegate_ notificationChangeState:state_ tag:[currentNotification_ tag]]; +} + +- (void) setPosition:(char)positions +{ + position_ = positions; + [self updateAnimations]; +} + +- (void) setNotificationDesign:(CCNode *) templates +{ + if(state_!=kCCNotificationStateHide) + [template_ stopAllActions]; + + if(state_==kCCNotificationStateShowing) + { +#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED + [[CCTouchDispatcher sharedDispatcher] removeDelegate:self]; +#endif + [[CCScheduler sharedScheduler] unscheduleSelector:@selector(_hideNotificationScheduler) forTarget:self]; + } + + [templates retain]; + [template_ release]; + template_ = templates; + [template_ setVisible:NO]; + [template_ setIsRelativeAnchorPoint:YES]; + + [self _setState:kCCNotificationStateHide]; +} +#pragma mark Notification Actions + +- (CCActionInterval*) _animation:(char)type time:(ccTime)time +{ + CCActionInterval *action = nil; + switch (type){ + case kCCNotificationAnimationMovement: + if(position_==kCCNotificationPositionBottom) + action = [CCMoveBy actionWithDuration:time position:ccp(0, template_.contentSize.height)]; + + else if(position_ == kCCNotificationPositionTop) + action = [CCMoveBy actionWithDuration:time position:ccp(0, -template_.contentSize.height)]; + + break; + case kCCNotificationAnimationScale: + action = [CCScaleBy actionWithDuration:time scale:(1.0f-KNOTIFICATIONMIN_SCALE)/KNOTIFICATIONMIN_SCALE]; + + break; + default: break; + } + return action; +} + +- (void) _updateAnimationIn +{ + self.animationIn = [CCSequence actionOne:[self _animation:typeAnimationIn_ time:timeAnimationIn_] two:[CCCallFunc actionWithTarget:self selector:@selector(_startScheduler)]]; +} + +- (void) _updateAnimationOut +{ + CCActionInterval *tempAction = [self _animation:typeAnimationOut_ time:timeAnimationOut_]; + self.animationOut = [CCSequence actionOne:[tempAction reverse] two:[CCCallFunc actionWithTarget:self selector:@selector(_hideNotification)]]; +} + +- (void) updateAnimations +{ + [self _updateAnimationIn]; + [self _updateAnimationOut]; +} + +- (void) setAnimationIn:(char)type time:(ccTime)time +{ + typeAnimationIn_ = type; + timeAnimationIn_ = time; + [self _updateAnimationIn]; +} + +- (void) setAnimationOut:(char)type time:(ccTime)time +{ + typeAnimationOut_ = type; + timeAnimationOut_ = time; + [self _updateAnimationOut]; +} + +- (void) setAnimation:(char)type time:(ccTime)time +{ + typeAnimationIn_ = typeAnimationOut_ = type; + timeAnimationIn_ = timeAnimationOut_ = time; + [self updateAnimations]; +} + +#pragma mark Notification steps + +- (void) _startScheduler +{ +#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED + [self registerWithTouchDispatcher]; +#endif + [self _setState:kCCNotificationStateShowing]; + [template_ stopAllActions]; + [[CCScheduler sharedScheduler] scheduleSelector:@selector(_hideNotificationScheduler) forTarget:self interval:showingTime_ paused:NO]; +} + +- (void) _hideNotification +{ + [self _setState:kCCNotificationStateHide]; + [template_ setVisible:NO]; + [template_ stopAllActions]; + [template_ onExit]; + + //Release current notification + [cachedNotifications_ removeObject:currentNotification_]; + self.currentNotification = nil; + + //Check next notification + [self _showNotification]; +} + +- (void) _hideNotificationScheduler +{ +#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED + [[CCTouchDispatcher sharedDispatcher] removeDelegate:self]; +#endif + [[CCScheduler sharedScheduler] unscheduleSelector:@selector(_hideNotificationScheduler) forTarget:self]; + if([currentNotification_ animated]) + { + [self _setState:kCCNotificationStateAnimationOut]; + [template_ runAction:animationOut_]; + }else + [self _hideNotification]; +} + +#pragma mark Manager Notifications + +- (void) _addNotificationToArray:(ccNotificationData*)data cached:(BOOL)isCached +{ + if(isCached) + { + [cachedNotifications_ addObject:data]; + if([cachedNotifications_ count]==1) + [self _showNotification]; + }else{ + if(currentNotification_) + { + [cachedNotifications_ removeObject:currentNotification_]; + } + [cachedNotifications_ insertObject:data atIndex:0]; + [self _showNotification]; + } +} + +- (ccNotificationData*) addWithTitle:(NSString*)title message:(NSString*)message image:(NSString*)image tag:(int)tag animate:(BOOL)animate waitUntilDone:(BOOL)isCached +{ + ccNotificationData *data = [[ccNotificationData alloc] init]; + data.title = title; + data.message = message; + data.media = image; + data.mediaType = kCCNotificationMediaPath; + data.tag = tag; + data.animated = animate; + + [self _addNotificationToArray:data cached:isCached]; + [data release]; + return data; +} + +- (ccNotificationData*) addWithTitle:(NSString*)title message:(NSString*)message texture:(CCTexture2D*)texture tag:(int)tag animate:(BOOL)animate waitUntilDone:(BOOL)isCached +{ + ccNotificationData *data = [[ccNotificationData alloc] init]; + data.title = title; + data.message = message; + data.media = texture; + data.mediaType = kCCNotificationMediaTexture; + data.tag = tag; + data.animated = animate; + + [self _addNotificationToArray:data cached:isCached]; + [data release]; + return data; +} + +- (ccNotificationData*) addWithTitle:(NSString*)title message:(NSString*)message image:(NSString*)image tag:(int)tag animate:(BOOL)animate +{ + return [self addWithTitle:title message:message image:image tag:tag animate:animate waitUntilDone:YES]; +} + +- (ccNotificationData*) addWithTitle:(NSString*)title message:(NSString*)message texture:(CCTexture2D*)texture tag:(int)tag animate:(BOOL)animate +{ + return [self addWithTitle:title message:message texture:texture tag:tag animate:animate waitUntilDone:YES]; +} + +/* Fast methods */ + +- (ccNotificationData*) addWithTitle:(NSString*)title message:(NSString*)message image:(NSString*)image +{ + return [self addWithTitle:title message:message image:image tag:-1 animate:YES waitUntilDone:YES]; +} + +- (ccNotificationData*) addWithTitle:(NSString*)title message:(NSString*)message texture:(CCTexture2D*)texture +{ + return [self addWithTitle:title message:message texture:texture tag:-1 animate:YES waitUntilDone:YES]; +} + +/* Deprecated */ +- (void) addSafelyWithTitle:(NSString*)title message:(NSString*)message image:(NSString*)image tag:(int)tag animate:(BOOL)animate +{ + [self addWithTitle:title message:message image:image tag:tag animate:animate waitUntilDone:YES]; +} + +- (void) _showNotification +{ + if([cachedNotifications_ count]==0) return; + //Get notification data + self.currentNotification = [cachedNotifications_ objectAtIndex:0]; + + //Stop system + if(state_==kCCNotificationStateShowing) + { +#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED + [[CCTouchDispatcher sharedDispatcher] removeDelegate:self]; +#else + [[CCEventDispatcher sharedDispatcher] removeMouseDelegate:self]; +#endif + } + + if(state_!=kCCNotificationStateHide) + { + [self _setState:kCCNotificationStateHide]; + + [template_ setVisible:NO]; + [template_ stopAllActions]; + [template_ onExit]; + [[CCScheduler sharedScheduler] unscheduleSelector:@selector(_hideNotificationScheduler) forTarget:self]; + } + + //Get variables + CCTexture2D *texture = (currentNotification_.media) ? ((currentNotification_.mediaType==kCCNotificationMediaTexture) ? (CCTexture2D*)currentNotification_.media : [[CCTextureCache sharedTextureCache] addImage:(NSString*)currentNotification_.media]) : nil; + + //Prepare template + [template_ setVisible:NO]; + [template_ stopAllActions]; + [template_ onExit]; + + //Prepare animation + CGSize winSize = CGSizeMake(480,320); + if(currentNotification_.animated) + { + if(position_==kCCNotificationPositionBottom){ + [template_ setAnchorPoint:ccp(0.5f, 0)]; + switch (typeAnimationIn_) { + case kCCNotificationAnimationMovement: + [template_ setScale:1.0f]; + [template_ setPosition:ccp(winSize.width/2.0f, -template_.contentSize.height)]; + + break; + case kCCNotificationAnimationScale: + [template_ setScale:KNOTIFICATIONMIN_SCALE]; + [template_ setPosition:ccp(winSize.width/2.0f, 0)]; + + break; + default: return; + } + + }else if(position_==kCCNotificationPositionTop) + { + [template_ setAnchorPoint:ccp(0.5f, 1)]; + switch (typeAnimationIn_){ + case kCCNotificationAnimationMovement: + [template_ setScale:1.0f]; + [template_ setPosition:ccp(winSize.width/2.0f, winSize.height+template_.contentSize.height)]; + + break; + case kCCNotificationAnimationScale: + [template_ setScale:KNOTIFICATIONMIN_SCALE]; + [template_ setPosition:ccp(winSize.width/2.0f, winSize.height)]; + + break; + default: return; + } + } + [self _setState:kCCNotificationStateAnimationIn]; + [template_ onEnter]; + [template_ runAction:animationIn_]; + + }else{ + if(position_==kCCNotificationPositionBottom) + { + [template_ setAnchorPoint:ccp(0.5f, 0)]; + [template_ setPosition:ccp(winSize.width/2.0f, 0)]; + }else if(position_==kCCNotificationPositionTop) + { + [template_ setAnchorPoint:ccp(0.5f, 1)]; + [template_ setPosition:ccp(winSize.width/2.0f, winSize.height)]; + } + [self _startScheduler]; + } + + //Update template + [template_ setTitle:[currentNotification_ title] message:[currentNotification_ message] texture:texture]; + [template_ setVisible:YES]; +} + +#pragma mark Touch Events + +#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED +- (void) registerWithTouchDispatcher +{ + [[CCTouchDispatcher sharedDispatcher] addStandardDelegate:self priority:INT_MIN]; +} + +- (void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event +{ + UITouch *touch = [touches anyObject]; + CGPoint point = [[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]]; + CGRect rect = [template_ boundingBox]; + if(CGRectContainsPoint(rect, point)) + if([delegate_ respondsToSelector:@selector(touched:)] && [delegate_ touched:[currentNotification_ tag]]) + [self _hideNotificationScheduler]; +} +#endif + +#pragma mark Other methods + +- (void) visit +{ + [template_ visit]; +} + +- (NSString*) description +{ + return [NSString stringWithFormat:@"<%@ = %08X>", [self class], self]; +} + +-(void) dealloc +{ + CCLOG(@"cocos2d: deallocing %@", self); + + sharedManager = nil; + [cachedNotifications_ release]; + [self setCurrentNotification:nil]; + [self setNotificationDesign:nil]; + [self setDelegate:nil]; + [self setAnimationIn:nil]; + [self setAnimationOut:nil]; + [super dealloc]; +} +@end \ No newline at end of file diff --git a/libs/cocos2d/notificationDesign.h b/libs/cocos2d/notificationDesign.h new file mode 100644 index 0000000..856fb74 --- /dev/null +++ b/libs/cocos2d/notificationDesign.h @@ -0,0 +1,39 @@ +/* + * CCNotifications + * + * Copyright (c) 2010 ForzeField Studios S.L. + * http://forzefield.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + + +#import +#import "CCNotifications.h" +#import "cocos2d.h" + +@interface CCNotificationDefaultDesign : CCLayerColor +{ + CCLabelTTF *title_; + CCLabelTTF *message_; + CCSprite *image_; +} + +@end \ No newline at end of file diff --git a/libs/cocos2d/notificationDesign.m b/libs/cocos2d/notificationDesign.m new file mode 100644 index 0000000..470d402 --- /dev/null +++ b/libs/cocos2d/notificationDesign.m @@ -0,0 +1,82 @@ +/* + * CCNotifications + * + * Copyright (c) 2010 ForzeField Studios S.L. + * http://forzefield.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + + +#import "notificationDesign.h" + +@implementation CCNotificationDefaultDesign + +- (id) init +{ + CGSize size = CGSizeMake(480,320); + self = [self initWithColor:ccc4(42, 68, 148, 180) width:size.width height:38]; + if (self != nil) { + title_ = [CCLabelTTF labelWithString:@" " fontName:@"Arial" fontSize:12]; + [title_ setIsRelativeAnchorPoint:NO]; + [title_ setAnchorPoint:CGPointZero]; + [title_ setPosition:ccp(52, 20)]; + + message_ = [CCLabelTTF labelWithString:@" " fontName:@"Arial" fontSize:15]; + [message_ setIsRelativeAnchorPoint:NO]; + [message_ setAnchorPoint:CGPointZero]; + [message_ setPosition:ccp(52, 3)]; + + image_ = [CCSprite node]; + [image_ setPosition:ccp(26, 19)]; + + [self addChild:title_]; + [self addChild:message_]; + [self addChild:image_]; + } + return self; +} + +- (void) setTitle:(NSString*)title message:(NSString*)message texture:(CCTexture2D*)texture{ + [title_ setString:title]; + [message_ setString:message]; + if(texture){ + CGRect rect = CGRectZero; + rect.size = texture.contentSize; + [image_ setTexture:texture]; + [image_ setTextureRect:rect]; + //Same size 32x32 + [image_ setScaleX:32.0f/rect.size.width]; + [image_ setScaleY:32.0f/rect.size.height]; + } +} + +- (void) updateColor +{ + //Gradient code + ccColor3B colorFinal = ccc3(0, 50, 100); + + squareColors_[0] = ccc4(color_.r, color_.g, color_.b, opacity_); + squareColors_[1] = ccc4(color_.r, color_.g, color_.b, opacity_); + squareColors_[2] = ccc4(colorFinal.r, colorFinal.g, colorFinal.b, opacity_); + squareColors_[3] = ccc4(colorFinal.r, colorFinal.g, colorFinal.b, opacity_); +} + +@end \ No newline at end of file -- cgit 1.4.1