diff options
author | Starla Insigna <starla4444@gmail.com> | 2011-11-30 12:57:06 -0500 |
---|---|---|
committer | Starla Insigna <starla4444@gmail.com> | 2011-11-30 12:57:06 -0500 |
commit | 6e96fb2144718722208d22f892716b55548135e1 (patch) | |
tree | 4dd550de787dd370a13039c03969442f54a3a856 /Classes/GameModeInfo.m | |
parent | fd58a0cde1bb5473e39e6cb82d28113da84b9ae0 (diff) | |
download | cartcollect-6e96fb2144718722208d22f892716b55548135e1.tar.gz cartcollect-6e96fb2144718722208d22f892716b55548135e1.tar.bz2 cartcollect-6e96fb2144718722208d22f892716b55548135e1.zip |
Created a game mode manager
There is now one location for information relating to each GameMode instead of several places, so that info can be easily updated and propagated to, for instance, GameModeSelectionLayer. GameModes can also be specified by an info instance each owns. There's also a way to get an ordered list of game modes. The three star game mode unlocking system has also been added. Closes #213
Diffstat (limited to 'Classes/GameModeInfo.m')
-rw-r--r-- | Classes/GameModeInfo.m | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/Classes/GameModeInfo.m b/Classes/GameModeInfo.m new file mode 100644 index 0000000..8b33fd8 --- /dev/null +++ b/Classes/GameModeInfo.m | |||
@@ -0,0 +1,128 @@ | |||
1 | // | ||
2 | // GameModeInfo.m | ||
3 | // Cartographic | ||
4 | // | ||
5 | // Created by Starla Insigna on 11/28/11. | ||
6 | // Copyright (c) 2011 Four Island. All rights reserved. | ||
7 | // | ||
8 | |||
9 | #import "GameModeInfo.h" | ||
10 | #import "CCNotifications.h" | ||
11 | #import "GameMode.h" | ||
12 | #import "GameModeManager.h" | ||
13 | #import "TestFlight.h" | ||
14 | |||
15 | @implementation GameModeInfo | ||
16 | |||
17 | @synthesize name, location, numOfStars, image, unlocked, unlockCondition, globalHighscoreKey, starsToUnlock; | ||
18 | |||
19 | - (id)initWithName:(NSString*)m_name location:(NSString*)m_location numOfStars:(int)m_numOfStars imageFilename:(NSString*)m_imageFilename unlocked:(BOOL)m_unlocked unlockCondition:(NSString*)m_unlockCondition gameClass:(Class)m_gameClass globalHighscoreKey:(NSString*)m_globalHighscoreKey starsToUnlock:(int)m_starsToUnlock | ||
20 | { | ||
21 | self = [super init]; | ||
22 | |||
23 | if (nil != self) | ||
24 | { | ||
25 | name = m_name; | ||
26 | location = m_location; | ||
27 | numOfStars = m_numOfStars; | ||
28 | image = [[UIImage alloc] initWithContentsOfFile:m_imageFilename]; | ||
29 | unlockCondition = m_unlockCondition; | ||
30 | globalHighscoreKey = m_globalHighscoreKey; | ||
31 | starsToUnlock = m_starsToUnlock; | ||
32 | |||
33 | NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; | ||
34 | unlocked = [defaults boolForKey:[NSString stringWithFormat:@"gameModeUnlocked-%@", name]]; | ||
35 | if (!unlocked) | ||
36 | { | ||
37 | unlocked = m_unlocked; | ||
38 | } | ||
39 | |||
40 | stars = (BOOL*) calloc(numOfStars, sizeof(BOOL)); | ||
41 | for (int i=0; i<numOfStars; i++) | ||
42 | { | ||
43 | stars[i] = [defaults boolForKey:[NSString stringWithFormat:@"gameModeStar-%@-%d", name, i]]; | ||
44 | } | ||
45 | |||
46 | if ([m_gameClass isSubclassOfClass:[GameMode class]]) | ||
47 | { | ||
48 | gameClass = m_gameClass; | ||
49 | } else { | ||
50 | [NSException raise:@"Invalid gameClass value" format:@"gameClass must be a subclass of GameMode"]; | ||
51 | } | ||
52 | } | ||
53 | |||
54 | return self; | ||
55 | } | ||
56 | |||
57 | - (void)setStar:(int)star_id withMessage:(NSString*)message | ||
58 | { | ||
59 | if (!stars[star_id]) | ||
60 | { | ||
61 | stars[star_id] = YES; | ||
62 | |||
63 | NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; | ||
64 | [defaults setBool:YES forKey:[NSString stringWithFormat:@"gameModeStar-%@-%d", name, star_id]]; | ||
65 | |||
66 | [[CCNotifications sharedManager] addWithTitle:@"Star Achieved!" message:message image:[NSString stringWithFormat:@"star_%d.png", star_id+1]]; | ||
67 | |||
68 | [GameModeManager sharedInstance].stars++; | ||
69 | } | ||
70 | } | ||
71 | |||
72 | - (BOOL)star:(int)star_id | ||
73 | { | ||
74 | return stars[star_id]; | ||
75 | } | ||
76 | |||
77 | - (int)starsCollected | ||
78 | { | ||
79 | int result = 0; | ||
80 | |||
81 | for (int i=0; i<numOfStars; i++) | ||
82 | { | ||
83 | if (stars[i]) | ||
84 | { | ||
85 | result++; | ||
86 | } | ||
87 | } | ||
88 | |||
89 | return result; | ||
90 | } | ||
91 | |||
92 | - (CCScene*)scene | ||
93 | { | ||
94 | CCScene* scene = [CCScene node]; | ||
95 | |||
96 | GameMode* layer = [[((GameMode*) [gameClass alloc]) init] autorelease]; | ||
97 | layer.tag = GAME_LAYER; | ||
98 | [scene addChild:layer]; | ||
99 | |||
100 | scene.tag = GAME_SCENE; | ||
101 | |||
102 | return scene; | ||
103 | } | ||
104 | |||
105 | - (void)unlock | ||
106 | { | ||
107 | if (!unlocked) | ||
108 | { | ||
109 | unlocked = YES; | ||
110 | |||
111 | NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; | ||
112 | [defaults setBool:YES forKey:[NSString stringWithFormat:@"gameModeUnlocked-%@", name]]; | ||
113 | |||
114 | CCTexture2D* texture = [[CCTexture2D alloc] initWithImage:image]; | ||
115 | [[CCNotifications sharedManager] addWithTitle:name message:@"You've unlocked a new game mode!" texture:texture]; | ||
116 | [texture release]; | ||
117 | |||
118 | [TestFlight passCheckpoint:[NSString stringWithFormat:@"Unlocked %@ Mode", name]]; | ||
119 | } | ||
120 | } | ||
121 | |||
122 | - (void)dealloc | ||
123 | { | ||
124 | free(stars); | ||
125 | [super dealloc]; | ||
126 | } | ||
127 | |||
128 | @end | ||