summary refs log tree commit diff stats
path: root/Classes/GameModeSelection.m
diff options
context:
space:
mode:
Diffstat (limited to 'Classes/GameModeSelection.m')
-rw-r--r--Classes/GameModeSelection.m195
1 files changed, 145 insertions, 50 deletions
diff --git a/Classes/GameModeSelection.m b/Classes/GameModeSelection.m index 0816828..fc6797a 100644 --- a/Classes/GameModeSelection.m +++ b/Classes/GameModeSelection.m
@@ -9,10 +9,11 @@
9#import "GameModeSelection.h" 9#import "GameModeSelection.h"
10#import "TutorialMode.h" 10#import "TutorialMode.h"
11#import "ClassicGameMode.h" 11#import "ClassicGameMode.h"
12#import "UIImage+ColorMasking.h"
12 13
13@implementation GameModeSelection 14@implementation GameModeSelection
14 15
15@synthesize name, location, unlocked, highscore, unlockCondition; 16@synthesize name, location, unlocked;
16 17
17- (id)initWithName:(NSString*)m_name location:(NSString*)m_location filename:(NSString*)filename unlocked:(BOOL)m_unlocked; 18- (id)initWithName:(NSString*)m_name location:(NSString*)m_location filename:(NSString*)filename unlocked:(BOOL)m_unlocked;
18{ 19{
@@ -26,92 +27,186 @@
26 location = m_location; 27 location = m_location;
27 unlocked = m_unlocked; 28 unlocked = m_unlocked;
28 29
29 if (!unlocked) 30 NSString* filenameMod;
31
32 if (unlocked)
30 { 33 {
34 filenameMod = [NSString stringWithFormat:@"%@-unlocked", filename];
35 } else {
36 filenameMod = [NSString stringWithFormat:@"%@-locked", filename];
31 name = [@"" stringByPaddingToLength:name.length withString:@"?" startingAtIndex:0]; 37 name = [@"" stringByPaddingToLength:name.length withString:@"?" startingAtIndex:0];
32 location = [@"" stringByPaddingToLength:location.length withString:@"?" startingAtIndex:0]; 38 location = [@"" stringByPaddingToLength:location.length withString:@"?" startingAtIndex:0];
33 } 39 }
34 40
35 nameLabel = [CCLabelBMFont labelWithString:[NSString stringWithFormat:@"%@ (%@)", location, name] fntFile:@"levelnames.fnt"]; 41 // First, create the frame that we will put the level picture inside
42 CGImageRef framestuff = [[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"framestuff" ofType:@"png"]] CGImage];
43 CGImageRef topLeftRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(0, 0, 8, 8));
44 CGImageRef topRightRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(8, 0, 8, 8));
45 CGImageRef bottomLeftRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(0, 8, 8, 8));
46 CGImageRef bottomRightRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(8, 8, 8, 8));
47 CGImageRef topBorderRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(0, 16, 8, 8));
48 CGImageRef leftBorderRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(8, 16, 8, 8));
49 CGImageRef rightBorderRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(0, 24, 8, 8));
50 CGImageRef bottomBorderRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(8, 24, 8, 8));
51 UIImage* topLeft = [UIImage imageWithCGImage:topLeftRef];
52 UIImage* topRight = [UIImage imageWithCGImage:topRightRef];
53 UIImage* bottomLeft = [UIImage imageWithCGImage:bottomLeftRef];
54 UIImage* bottomRight = [UIImage imageWithCGImage:bottomRightRef];
55 UIImage* topBorder = [UIImage imageWithCGImage:topBorderRef];
56 UIImage* leftBorder = [UIImage imageWithCGImage:leftBorderRef];
57 UIImage* rightBorder = [UIImage imageWithCGImage:rightBorderRef];
58 UIImage* bottomBorder = [UIImage imageWithCGImage:bottomBorderRef];
59 CGImageRelease(topLeftRef);
60 CGImageRelease(topRightRef);
61 CGImageRelease(bottomLeftRef);
62 CGImageRelease(bottomRightRef);
63 CGImageRelease(topBorderRef);
64 CGImageRelease(leftBorderRef);
65 CGImageRelease(rightBorderRef);
66 CGImageRelease(bottomBorderRef);
36 67
37 [self addChild:nameLabel]; 68 CGSize boxSize = CGSizeMake(128+12, 128+12);
38 nameLabel.position = ccp(0, -32-nameLabel.contentSize.height); 69 UIGraphicsBeginImageContext(boxSize);
70 CGContextRef context = UIGraphicsGetCurrentContext();
71 [topLeft drawInRect:CGRectMake(0, 0, 8, 8)];
72 [topBorder drawInRect:CGRectMake(8, 0, boxSize.width-16, 8)];
73 [topRight drawInRect:CGRectMake(8+boxSize.width-16, 0, 8, 8)];
74 [rightBorder drawInRect:CGRectMake(8+boxSize.width-16, 8, 8, boxSize.height-16)];
75 [bottomRight drawInRect:CGRectMake(8+boxSize.width-16, 8+boxSize.height-16, 8, 8)];
76 [bottomBorder drawInRect:CGRectMake(8, 8+boxSize.height-16, boxSize.width-16, 8)];
77 [bottomLeft drawInRect:CGRectMake(0, 8+boxSize.height-16, 8, 8)];
78 [leftBorder drawInRect:CGRectMake(0, 8, 8, boxSize.height-16)];
79 CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
80 CGContextFillRect(context, CGRectMake(8, 8, boxSize.width-16, boxSize.height-16));
81 UIImage* selectionBackground = UIGraphicsGetImageFromCurrentImageContext();
39 82
83 // Now we want to put the level image inside the frame without messing up the frame itself
40 UIImage* innerPicture = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:filename ofType:@"png"]]; 84 UIImage* innerPicture = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:filename ofType:@"png"]];
41 UIGraphicsBeginImageContext(CGSizeMake(128, 128)); 85 CGContextClipToMask(context, CGRectMake(0, 0, boxSize.width, boxSize.height), [[selectionBackground opaqueMaskFromWhiteImage] CGImage]);
42 CGContextRef context = UIGraphicsGetCurrentContext(); 86 [innerPicture drawInRect:CGRectMake(6, 6, 128, 128)];
43 UIGraphicsPushContext(context); 87 selectionBackground = UIGraphicsGetImageFromCurrentImageContext();
88 UIGraphicsEndImageContext();
89
90 // The frame needs a shadow, so let's redraw it in a new context
91 UIGraphicsBeginImageContext(CGSizeMake(boxSize.width+10, boxSize.height+10));
92 context = UIGraphicsGetCurrentContext();
93 CGContextSaveGState(context);
44 94
45 if (unlocked) 95 if (unlocked)
46 { 96 {
47 [innerPicture drawInRect:CGRectMake(0, 0, 128, 128)]; 97 CGContextSetShadow(context, CGSizeMake(-6, 6), 4.0f);
98 [selectionBackground drawInRect:CGRectMake(10, 0, boxSize.width, boxSize.height)];
48 } else { 99 } else {
100 // Draw the picture in grayscale if the level has not yet been unlocked
101 CGContextClipToMask(context, CGRectMake(10, 0, boxSize.width, boxSize.height), [selectionBackground CGImage]);
49 CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]); 102 CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
50 CGContextFillRect(context, CGRectMake(0, 0, 128, 128)); 103 CGContextFillRect(context, CGRectMake(10, 0, boxSize.width, boxSize.height));
51 [innerPicture drawInRect:CGRectMake(0, 0, 128, 128) blendMode:kCGBlendModeLuminosity alpha:1.0]; 104 CGContextRestoreGState(context);
105
106 CGContextSaveGState(context);
107 CGContextSetShadow(context, CGSizeMake(-6, 6), 4.0f);
108 [selectionBackground drawInRect:CGRectMake(10, 0, boxSize.width, boxSize.height) blendMode:kCGBlendModeLuminosity alpha:1.0];
52 } 109 }
53 110
54 UIGraphicsPopContext(); 111 CGContextRestoreGState(context);
55 CGImageRef innerPictureRef = [UIGraphicsGetImageFromCurrentImageContext() CGImage]; 112 CGImageRef pictureRef = [UIGraphicsGetImageFromCurrentImageContext() CGImage];
56 113
57 if (unlocked) 114 CCSprite* picture = [CCSprite spriteWithCGImage:pictureRef key:[NSString stringWithFormat:@"gms-%@", filenameMod]];
58 {
59 CCSprite* picture = [CCSprite spriteWithCGImage:innerPictureRef key:[NSString stringWithFormat:@"gms-%@", filename]];
60 UIGraphicsPushContext(context);
61 CGContextSetFillColorWithColor(context, [[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5] CGColor]);
62 CGContextFillRect(context, CGRectMake(0, 0, 128, 128));
63 UIGraphicsPopContext();
64 CGImageRef selectedButtonRef = [UIGraphicsGetImageFromCurrentImageContext() CGImage];
65 CCSprite* selectedButton = [CCSprite spriteWithCGImage:selectedButtonRef key:[NSString stringWithFormat:@"gms-%@-selected", filename]];
66
67 CCMenuItemSprite* pictureMenuItem = [CCMenuItemSprite itemFromNormalSprite:picture selectedSprite:selectedButton target:self selector:@selector(buttonTapped)];
68 CCMenu* theMenu = [CCMenu menuWithItems:pictureMenuItem, nil];
69 theMenu.position = ccp(0, 32);
70 [self addChild:theMenu];
71 } else {
72 CCSprite* picture = [CCSprite spriteWithCGImage:innerPictureRef key:[NSString stringWithFormat:@"gms-%@-locked", filename]];
73 picture.position = ccp(0, 32);
74 [self addChild:picture];
75 }
76 115
116 // We're also going to need a "selected" image state for the button
117 CGContextSaveGState(context);
118 CGContextClipToMask(context, CGRectMake(10, 0, boxSize.width, boxSize.height), [selectionBackground CGImage]);
119 CGContextSetFillColorWithColor(context, [[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5] CGColor]);
120 CGContextFillRect(context, CGRectMake(10, 0, boxSize.width, boxSize.height));
121 CGContextRestoreGState(context);
122 CGImageRef selectedButtonRef = [UIGraphicsGetImageFromCurrentImageContext() CGImage];
77 UIGraphicsEndImageContext(); 123 UIGraphicsEndImageContext();
124 CCSprite* selectedButton = [CCSprite spriteWithCGImage:selectedButtonRef key:[NSString stringWithFormat:@"gms-%@-selected", filenameMod]];
125
126 CCMenuItemSprite* pictureMenuItem = [CCMenuItemSprite itemFromNormalSprite:picture selectedSprite:selectedButton target:self selector:@selector(buttonTapped)];
127 CCMenu* theMenu = [CCMenu menuWithItems:pictureMenuItem, nil];
128 theMenu.position = ccp(-5, 0);
129 [self addChild:theMenu];
130
131 // Render the titles
132 UIFont* titleFont = [UIFont fontWithName:@"AmericanTypewriter-Bold" size:18.0f];
133 CGSize titleSize = [location sizeWithFont:titleFont constrainedToSize:CGSizeMake(128, 0)];
134 UIFont* subtitleFont = [UIFont fontWithName:@"AmericanTypewriter" size:18.0f];
135 CGSize subtitleSize = [name sizeWithFont:subtitleFont constrainedToSize:CGSizeMake(128, 0)];
136 CGSize combinedTitleSize = CGSizeMake(128, titleSize.height + 10 + subtitleSize.height + 10);
137
138 UIGraphicsBeginImageContext(combinedTitleSize);
139 context = UIGraphicsGetCurrentContext();
140 CGContextSetShadow(context, CGSizeMake(-6, 6), 4.0f);
141 [location drawInRect:CGRectMake(10, 0, 128, titleSize.height) withFont:titleFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];
142 [name drawInRect:CGRectMake(10, titleSize.height, 128, subtitleSize.height) withFont:subtitleFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];
143
144 CGImageRef titleImage = [UIGraphicsGetImageFromCurrentImageContext() CGImage];
145 UIGraphicsEndImageContext();
146 CCSprite* titleSprite = [CCSprite spriteWithCGImage:titleImage key:[NSString stringWithFormat:@"gms-%@-title", filenameMod]];
147 titleSprite.position = ccp(-10, (boxSize.height)/2+(combinedTitleSize.height)/2);
148 [self addChild:titleSprite];
78 } 149 }
79 150
80 return self; 151 return self;
81} 152}
82 153
83- (void)setHighscore:(int)m_highscore 154- (id)initWithName:(NSString *)m_name location:(NSString *)m_location filename:(NSString *)m_filename highscore:(int)m_highscore
84{ 155{
85 if (unlocked) 156 self = [self initWithName:m_name location:m_location filename:m_filename unlocked:YES];
157
158 if (nil != self)
86 { 159 {
87 highscore = m_highscore; 160 if (m_highscore != 0)
88 161 {
89 otherLabel = [CCLabelBMFont labelWithString:[NSString stringWithFormat:@"Highscore: %d", highscore] fntFile:@"leveldescriptions.fnt"]; 162 // Render the highscore label
90 otherLabel.position = ccp(0, -32-nameLabel.contentSize.height-otherLabel.contentSize.height); 163 NSString* highscoreString = [NSString stringWithFormat:@"Highscore: %d", m_highscore];
91 [self addChild:otherLabel]; 164 UIFont* highscoreFont = [UIFont fontWithName:@"AmericanTypewriter" size:16.0f];
165 CGSize highscoreSize = [highscoreString sizeWithFont:highscoreFont];
166
167 UIGraphicsBeginImageContext(CGSizeMake(highscoreSize.width+10, highscoreSize.height+10));
168 CGContextRef context = UIGraphicsGetCurrentContext();
169 CGContextSetShadow(context, CGSizeMake(-6, 6), 4.0f);
170 [highscoreString drawInRect:CGRectMake(10, 0, highscoreSize.width, highscoreSize.height) withFont:highscoreFont];
171
172 CGImageRef highscoreImage = [UIGraphicsGetImageFromCurrentImageContext() CGImage];
173 UIGraphicsEndImageContext();
174 CCSprite* highscoreSprite = [CCSprite spriteWithCGImage:highscoreImage key:[NSString stringWithFormat:@"gms-%@-highscore", m_filename]];
175 highscoreSprite.position = ccp((128-highscoreSize.width)/2, 0-64-(highscoreSize.height)/2-10);
176 [self addChild:highscoreSprite];
177 }
92 } 178 }
179
180 return self;
93} 181}
94 182
95- (void)setUnlockCondition:(NSString *)m_unlockCondition 183- (id)initWithName:(NSString *)m_name location:(NSString *)m_location filename:(NSString *)m_filename unlockCondition:(NSString*)m_unlockCondition
96{ 184{
97 if (!unlocked) 185 self = [self initWithName:m_name location:m_location filename:m_filename unlocked:NO];
186
187 if (nil != self)
98 { 188 {
99 unlockCondition = m_unlockCondition; 189 unlockCondition = m_unlockCondition;
100
101 otherLabel = [CCLabelBMFont labelWithString:unlockCondition fntFile:@"leveldescriptions.fnt"];
102 otherLabel.position = ccp(0, -32-nameLabel.contentSize.height-otherLabel.contentSize.height);
103 [self addChild:otherLabel];
104 } 190 }
191
192 return self;
105} 193}
106 194
107- (void)buttonTapped 195- (void)buttonTapped
108{ 196{
109 if ([name isEqual:@"Tutorial"]) 197 if (unlocked)
110 {
111 [[CCDirector sharedDirector] replaceScene:[TutorialMode scene]];
112 } else if ([name isEqual:@"Collect"])
113 { 198 {
114 [[CCDirector sharedDirector] replaceScene:[ClassicGameMode scene]]; 199 if ([name isEqual:@"Tutorial"])
200 {
201 [[CCDirector sharedDirector] replaceScene:[TutorialMode scene]];
202 } else if ([name isEqual:@"Collect"])
203 {
204 [[CCDirector sharedDirector] replaceScene:[ClassicGameMode scene]];
205 }
206 } else {
207 UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"To unlock this game mode:" message:unlockCondition delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
208 [alertView show];
209 [alertView release];
115 } 210 }
116} 211}
117 212