diff options
Diffstat (limited to 'Classes/GameModeSelection.m')
-rw-r--r-- | Classes/GameModeSelection.m | 123 |
1 files changed, 40 insertions, 83 deletions
diff --git a/Classes/GameModeSelection.m b/Classes/GameModeSelection.m index 40981b1..ea5f45b 100644 --- a/Classes/GameModeSelection.m +++ b/Classes/GameModeSelection.m | |||
@@ -13,42 +13,34 @@ | |||
13 | 13 | ||
14 | @implementation GameModeSelection | 14 | @implementation GameModeSelection |
15 | 15 | ||
16 | @synthesize name, location, unlocked, delegate; | 16 | @synthesize gameMode, delegate; |
17 | 17 | ||
18 | + (id)selectionWithName:(NSString*)name location:(NSString*)location filename:(NSString*)filename unlocked:(BOOL)unlocked | 18 | + (id)selectionWithGameModeInfo:(GameModeInfo*)m_gameMode |
19 | { | 19 | { |
20 | return [[[GameModeSelection alloc] initWithName:name location:location filename:filename unlocked:unlocked] autorelease]; | 20 | return [[[GameModeSelection alloc] initWithGameModeInfo:m_gameMode] autorelease]; |
21 | } | 21 | } |
22 | 22 | ||
23 | + (id)selectionWithName:(NSString *)name location:(NSString *)location filename:(NSString *)filename unlockCondition:(NSString*)unlockCondition | 23 | - (id)initWithGameModeInfo:(GameModeInfo*)m_gameMode |
24 | { | ||
25 | return [[[GameModeSelection alloc] initWithName:name location:location filename:filename unlockCondition:unlockCondition] autorelease]; | ||
26 | } | ||
27 | |||
28 | - (id)initWithName:(NSString*)m_name location:(NSString*)m_location filename:(NSString*)filename unlocked:(BOOL)m_unlocked; | ||
29 | { | 24 | { |
30 | self = [super initWithTarget:nil selector:nil]; | 25 | self = [super initWithTarget:nil selector:nil]; |
31 | 26 | ||
32 | if (nil != self) | 27 | if (nil != self) |
33 | { | 28 | { |
29 | if (!m_gameMode.unlocked) | ||
30 | { | ||
31 | @throw [NSException exceptionWithName:NSInvalidArgumentException | ||
32 | reason:@"Game mode must be unlocked to appear on selection screen" | ||
33 | userInfo:nil]; | ||
34 | } | ||
35 | |||
34 | self.anchorPoint = CGPointMake(0.5f, 0.5f); | 36 | self.anchorPoint = CGPointMake(0.5f, 0.5f); |
35 | 37 | ||
36 | name = m_name; | 38 | gameMode = m_gameMode; |
37 | location = m_location; | ||
38 | unlocked = m_unlocked; | ||
39 | 39 | ||
40 | contentSize_ = CGSizeMake(128, 320); | 40 | contentSize_ = CGSizeMake(128, 320); |
41 | 41 | ||
42 | NSString* filenameMod; | 42 | NSString* name = gameMode.name; |
43 | 43 | NSString* location = gameMode.location; | |
44 | if (unlocked) | ||
45 | { | ||
46 | filenameMod = [NSString stringWithFormat:@"%@-unlocked", filename]; | ||
47 | } else { | ||
48 | filenameMod = [NSString stringWithFormat:@"%@-locked", filename]; | ||
49 | name = [@"" stringByPaddingToLength:name.length withString:@"?" startingAtIndex:0]; | ||
50 | location = [@"" stringByPaddingToLength:location.length withString:@"?" startingAtIndex:0]; | ||
51 | } | ||
52 | 44 | ||
53 | // First, create the frame that we will put the level picture inside | 45 | // First, create the frame that we will put the level picture inside |
54 | CGImageRef framestuff = [[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"framestuff" ofType:@"png"]] CGImage]; | 46 | CGImageRef framestuff = [[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"framestuff" ofType:@"png"]] CGImage]; |
@@ -93,7 +85,7 @@ | |||
93 | UIImage* selectionBackground = UIGraphicsGetImageFromCurrentImageContext(); | 85 | UIImage* selectionBackground = UIGraphicsGetImageFromCurrentImageContext(); |
94 | 86 | ||
95 | // Now we want to put the level image inside the frame without messing up the frame itself | 87 | // Now we want to put the level image inside the frame without messing up the frame itself |
96 | UIImage* innerPicture = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:filename ofType:@"png"]]; | 88 | UIImage* innerPicture = gameMode.image; |
97 | CGContextClipToMask(context, CGRectMake(0, 0, boxSize.width, boxSize.height), [[selectionBackground opaqueMaskFromWhiteImage] CGImage]); | 89 | CGContextClipToMask(context, CGRectMake(0, 0, boxSize.width, boxSize.height), [[selectionBackground opaqueMaskFromWhiteImage] CGImage]); |
98 | [innerPicture drawInRect:CGRectMake(6, 6, 128, 128)]; | 90 | [innerPicture drawInRect:CGRectMake(6, 6, 128, 128)]; |
99 | selectionBackground = UIGraphicsGetImageFromCurrentImageContext(); | 91 | selectionBackground = UIGraphicsGetImageFromCurrentImageContext(); |
@@ -104,26 +96,13 @@ | |||
104 | context = UIGraphicsGetCurrentContext(); | 96 | context = UIGraphicsGetCurrentContext(); |
105 | CGContextSaveGState(context); | 97 | CGContextSaveGState(context); |
106 | 98 | ||
107 | if (unlocked) | 99 | CGContextSetShadow(context, CGSizeMake(-6, 6), 4.0f); |
108 | { | 100 | [selectionBackground drawInRect:CGRectMake(10, 0, boxSize.width, boxSize.height)]; |
109 | CGContextSetShadow(context, CGSizeMake(-6, 6), 4.0f); | ||
110 | [selectionBackground drawInRect:CGRectMake(10, 0, boxSize.width, boxSize.height)]; | ||
111 | } else { | ||
112 | // Draw the picture in grayscale if the level has not yet been unlocked | ||
113 | CGContextClipToMask(context, CGRectMake(10, 0, boxSize.width, boxSize.height), [selectionBackground CGImage]); | ||
114 | CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]); | ||
115 | CGContextFillRect(context, CGRectMake(10, 0, boxSize.width, boxSize.height)); | ||
116 | CGContextRestoreGState(context); | ||
117 | |||
118 | CGContextSaveGState(context); | ||
119 | CGContextSetShadow(context, CGSizeMake(-6, 6), 4.0f); | ||
120 | [selectionBackground drawInRect:CGRectMake(10, 0, boxSize.width, boxSize.height) blendMode:kCGBlendModeLuminosity alpha:1.0]; | ||
121 | } | ||
122 | 101 | ||
123 | CGContextRestoreGState(context); | 102 | CGContextRestoreGState(context); |
124 | CGImageRef pictureRef = [UIGraphicsGetImageFromCurrentImageContext() CGImage]; | 103 | CGImageRef pictureRef = [UIGraphicsGetImageFromCurrentImageContext() CGImage]; |
125 | 104 | ||
126 | CCSprite* picture = [CCSprite spriteWithCGImage:pictureRef key:[NSString stringWithFormat:@"gms-%@", filenameMod]]; | 105 | CCSprite* picture = [CCSprite spriteWithCGImage:pictureRef key:[NSString stringWithFormat:@"gms-%@", name]]; |
127 | 106 | ||
128 | // We're also going to need a "selected" image state for the button | 107 | // We're also going to need a "selected" image state for the button |
129 | CGContextSaveGState(context); | 108 | CGContextSaveGState(context); |
@@ -133,7 +112,7 @@ | |||
133 | CGContextRestoreGState(context); | 112 | CGContextRestoreGState(context); |
134 | CGImageRef selectedButtonRef = [UIGraphicsGetImageFromCurrentImageContext() CGImage]; | 113 | CGImageRef selectedButtonRef = [UIGraphicsGetImageFromCurrentImageContext() CGImage]; |
135 | UIGraphicsEndImageContext(); | 114 | UIGraphicsEndImageContext(); |
136 | CCSprite* selectedButton = [CCSprite spriteWithCGImage:selectedButtonRef key:[NSString stringWithFormat:@"gms-%@-selected", filenameMod]]; | 115 | CCSprite* selectedButton = [CCSprite spriteWithCGImage:selectedButtonRef key:[NSString stringWithFormat:@"gms-%@-selected", name]]; |
137 | 116 | ||
138 | CCMenuItemSprite* pictureMenuItem = [CCMenuItemSprite itemFromNormalSprite:picture selectedSprite:selectedButton target:self selector:@selector(buttonTapped)]; | 117 | CCMenuItemSprite* pictureMenuItem = [CCMenuItemSprite itemFromNormalSprite:picture selectedSprite:selectedButton target:self selector:@selector(buttonTapped)]; |
139 | NMPanelMenu* theMenu = [NMPanelMenu menuWithItems:pictureMenuItem, nil]; | 118 | NMPanelMenu* theMenu = [NMPanelMenu menuWithItems:pictureMenuItem, nil]; |
@@ -155,64 +134,42 @@ | |||
155 | 134 | ||
156 | CGImageRef titleImage = [UIGraphicsGetImageFromCurrentImageContext() CGImage]; | 135 | CGImageRef titleImage = [UIGraphicsGetImageFromCurrentImageContext() CGImage]; |
157 | UIGraphicsEndImageContext(); | 136 | UIGraphicsEndImageContext(); |
158 | CCSprite* titleSprite = [CCSprite spriteWithCGImage:titleImage key:[NSString stringWithFormat:@"gms-%@-title", filenameMod]]; | 137 | CCSprite* titleSprite = [CCSprite spriteWithCGImage:titleImage key:[NSString stringWithFormat:@"gms-%@-title", name]]; |
159 | titleSprite.position = ccp(-10, (boxSize.height)/2+(combinedTitleSize.height)/2); | 138 | titleSprite.position = ccp(-10, (boxSize.height)/2+(combinedTitleSize.height)/2); |
160 | [self addChild:titleSprite]; | 139 | [self addChild:titleSprite]; |
161 | 140 | ||
162 | if (unlocked) | 141 | Highscore* localHighscore = [Highscore localHighscoreForGameMode:name]; |
142 | |||
143 | if (localHighscore != nil) | ||
163 | { | 144 | { |
164 | Highscore* localHighscore = [Highscore localHighscoreForGameMode:name]; | 145 | // Render the highscore label |
146 | NSString* highscoreString = [NSString stringWithFormat:@"Highscore: %d", localHighscore.score]; | ||
147 | UIFont* highscoreFont = [UIFont fontWithName:@"AmericanTypewriter" size:16.0f]; | ||
148 | CGSize highscoreSize = [highscoreString sizeWithFont:highscoreFont]; | ||
149 | |||
150 | UIGraphicsBeginImageContext(CGSizeMake(highscoreSize.width+10, highscoreSize.height+10)); | ||
151 | CGContextRef context = UIGraphicsGetCurrentContext(); | ||
152 | CGContextSetShadow(context, CGSizeMake(-6, 6), 4.0f); | ||
153 | [highscoreString drawInRect:CGRectMake(10, 0, highscoreSize.width, highscoreSize.height) withFont:highscoreFont]; | ||
165 | 154 | ||
166 | if (localHighscore != nil) | 155 | CGImageRef highscoreImage = [UIGraphicsGetImageFromCurrentImageContext() CGImage]; |
167 | { | 156 | UIGraphicsEndImageContext(); |
168 | // Render the highscore label | 157 | CCSprite* highscoreSprite = [CCSprite spriteWithCGImage:highscoreImage key:[NSString stringWithFormat:@"gms-%@-highscore-%d", name, localHighscore.score]]; |
169 | NSString* highscoreString = [NSString stringWithFormat:@"Highscore: %d", localHighscore.score]; | 158 | highscoreSprite.position = ccp(-5, 0-64-(highscoreSize.height)/2-10); |
170 | UIFont* highscoreFont = [UIFont fontWithName:@"AmericanTypewriter" size:16.0f]; | 159 | [self addChild:highscoreSprite]; |
171 | CGSize highscoreSize = [highscoreString sizeWithFont:highscoreFont]; | ||
172 | |||
173 | UIGraphicsBeginImageContext(CGSizeMake(highscoreSize.width+10, highscoreSize.height+10)); | ||
174 | CGContextRef context = UIGraphicsGetCurrentContext(); | ||
175 | CGContextSetShadow(context, CGSizeMake(-6, 6), 4.0f); | ||
176 | [highscoreString drawInRect:CGRectMake(10, 0, highscoreSize.width, highscoreSize.height) withFont:highscoreFont]; | ||
177 | |||
178 | CGImageRef highscoreImage = [UIGraphicsGetImageFromCurrentImageContext() CGImage]; | ||
179 | UIGraphicsEndImageContext(); | ||
180 | CCSprite* highscoreSprite = [CCSprite spriteWithCGImage:highscoreImage key:[NSString stringWithFormat:@"gms-%@-highscore-%d", filename, localHighscore.score]]; | ||
181 | highscoreSprite.position = ccp(-5, 0-64-(highscoreSize.height)/2-10); | ||
182 | [self addChild:highscoreSprite]; | ||
183 | } | ||
184 | } | 160 | } |
185 | } | 161 | } |
186 | 162 | ||
187 | return self; | 163 | return self; |
188 | } | 164 | } |
189 | 165 | ||
190 | - (id)initWithName:(NSString *)m_name location:(NSString *)m_location filename:(NSString *)m_filename unlockCondition:(NSString*)m_unlockCondition | ||
191 | { | ||
192 | self = [self initWithName:m_name location:m_location filename:m_filename unlocked:NO]; | ||
193 | |||
194 | if (nil != self) | ||
195 | { | ||
196 | unlockCondition = m_unlockCondition; | ||
197 | } | ||
198 | |||
199 | return self; | ||
200 | } | ||
201 | |||
202 | - (void)buttonTapped | 166 | - (void)buttonTapped |
203 | { | 167 | { |
204 | if (unlocked) | 168 | if (delegate != nil) |
205 | { | 169 | { |
206 | if (delegate != nil) | 170 | [delegate didSelectGameMode:self]; |
207 | { | ||
208 | [delegate didSelectGameMode:self]; | ||
209 | } else { | ||
210 | NSLog(@"I don't have a GameModeSelectionDelegate to call for some reason..."); | ||
211 | } | ||
212 | } else { | 171 | } else { |
213 | UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"To unlock this game mode:" message:unlockCondition delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil]; | 172 | NSLog(@"I don't have a GameModeSelectionDelegate to call for some reason..."); |
214 | [alertView show]; | ||
215 | [alertView release]; | ||
216 | } | 173 | } |
217 | } | 174 | } |
218 | 175 | ||