diff options
author | Starla Insigna <starla4444@gmail.com> | 2011-07-30 11:19:14 -0400 |
---|---|---|
committer | Starla Insigna <starla4444@gmail.com> | 2011-07-30 11:19:14 -0400 |
commit | 9cd57b731ab1c666d4a1cb725538fdc137763d12 (patch) | |
tree | 5bac45ae5157a1cb10c6e45500cbf72789917980 /Classes/GameOverLayer.m | |
download | cartcollect-9cd57b731ab1c666d4a1cb725538fdc137763d12.tar.gz cartcollect-9cd57b731ab1c666d4a1cb725538fdc137763d12.tar.bz2 cartcollect-9cd57b731ab1c666d4a1cb725538fdc137763d12.zip |
Initial commit (version 0.2.1)
Diffstat (limited to 'Classes/GameOverLayer.m')
-rwxr-xr-x | Classes/GameOverLayer.m | 196 |
1 files changed, 196 insertions, 0 deletions
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 @@ | |||
1 | // | ||
2 | // GameOverLayer.m | ||
3 | // Cart Collect | ||
4 | // | ||
5 | // Created by iD Student Account on 7/19/11. | ||
6 | // Copyright 2011 __MyCompanyName__. All rights reserved. | ||
7 | // | ||
8 | |||
9 | #import "GameOverLayer.h" | ||
10 | |||
11 | |||
12 | @implementation GameOverLayer | ||
13 | |||
14 | + (CCScene*)sceneWithScore:(int)score | ||
15 | { | ||
16 | CCScene* scene = [CCScene node]; | ||
17 | |||
18 | CCLayerColor* backgroundLayer = [CCLayerColor layerWithColor:ccc4(255, 255, 255, 255)]; | ||
19 | [scene addChild:backgroundLayer]; | ||
20 | |||
21 | GameOverLayer* layer = [[[GameOverLayer alloc] initWithScore:score] autorelease]; | ||
22 | [scene addChild:layer]; | ||
23 | |||
24 | return scene; | ||
25 | } | ||
26 | |||
27 | - (id)initWithScore:(int)score2 | ||
28 | { | ||
29 | self = [super init]; | ||
30 | |||
31 | if (nil != self) | ||
32 | { | ||
33 | CCSprite* backgroundImage = [CCSprite spriteWithFile:@"Morning1.png"]; | ||
34 | backgroundImage.position = ccp(240, 160); | ||
35 | [self addChild:backgroundImage z:0]; | ||
36 | |||
37 | score = score2; | ||
38 | |||
39 | scoreField = [[UILabel alloc] initWithFrame:CGRectMake(205, 320-200-320, 0, 0)]; | ||
40 | [scoreField setFont:[UIFont systemFontOfSize:20.0f]]; | ||
41 | [scoreField setBackgroundColor:[UIColor clearColor]]; | ||
42 | [scoreField setText:[NSString stringWithFormat:@"%d", score2]]; | ||
43 | CGSize labelSize = [scoreField.text sizeWithFont:scoreField.font constrainedToSize:CGSizeMake(160, 31) lineBreakMode:UILineBreakModeClip]; | ||
44 | [scoreField setFrame:CGRectMake(scoreField.frame.origin.x, scoreField.frame.origin.y, labelSize.width, labelSize.height)]; | ||
45 | [[[CCDirector sharedDirector] openGLView] addSubview:scoreField]; | ||
46 | |||
47 | textField = [[UITextField alloc] initWithFrame:CGRectMake(205, 320-247-320, 216, 31)]; | ||
48 | [textField setFont:[UIFont systemFontOfSize:20.0f]]; | ||
49 | [textField setBackgroundColor:[UIColor clearColor]]; | ||
50 | [textField setDelegate:self]; | ||
51 | NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; | ||
52 | [textField setText:[defaults objectForKey:@"username"]]; | ||
53 | [[[CCDirector sharedDirector] openGLView] addSubview:textField]; | ||
54 | |||
55 | submitSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(211, 320-161-320, 0, 0)]; | ||
56 | [submitSwitch setOn:[defaults boolForKey:@"submitScore"]]; | ||
57 | [[[CCDirector sharedDirector] openGLView] addSubview:submitSwitch]; | ||
58 | |||
59 | activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(480-20-10, 320-20-10, 20, 20)]; | ||
60 | [[[CCDirector sharedDirector] openGLView] addSubview:activityIndicator]; | ||
61 | |||
62 | backButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; | ||
63 | [backButton setFrame:CGRectMake(240-154/2, 320-60-15-320, 154, 31)]; | ||
64 | [backButton setTitle:@"Back to Main Menu" forState:UIControlStateNormal]; | ||
65 | [backButton addTarget:self action:@selector(newgame) forControlEvents:UIControlEventTouchUpInside]; | ||
66 | [backButton.titleLabel setFont:[UIFont systemFontOfSize:16.0]]; | ||
67 | [backButton.titleLabel setTextColor:[UIColor blackColor]]; | ||
68 | |||
69 | [[[CCDirector sharedDirector] openGLView] addSubview:backButton]; | ||
70 | } | ||
71 | |||
72 | return self; | ||
73 | } | ||
74 | |||
75 | - (void)onEnter | ||
76 | { | ||
77 | [UIView beginAnimations:nil context:NULL]; | ||
78 | [UIView setAnimationDuration:1.5f]; | ||
79 | [UIView setAnimationCurve:UIViewAnimationCurveLinear]; | ||
80 | scoreField.transform = CGAffineTransformMakeTranslation(0, 320); | ||
81 | textField.transform = CGAffineTransformMakeTranslation(0, 320); | ||
82 | submitSwitch.transform = CGAffineTransformMakeTranslation(0, 320); | ||
83 | backButton.transform = CGAffineTransformMakeTranslation(0, 320); | ||
84 | [UIView commitAnimations]; | ||
85 | } | ||
86 | |||
87 | - (void)newgame | ||
88 | { | ||
89 | backButton.enabled = NO; | ||
90 | textField.enabled = NO; | ||
91 | submitSwitch.enabled = NO; | ||
92 | |||
93 | const char* sqlQuery = [[NSString stringWithFormat:@"INSERT INTO highscores (name, score) VALUES (\"%@\",%d)", [textField text], score] UTF8String]; | ||
94 | sqlite3_stmt* compiled_statement; | ||
95 | |||
96 | if (sqlite3_prepare_v2([Cart_CollectAppDelegate database], sqlQuery, -1, &compiled_statement, NULL) == SQLITE_OK) | ||
97 | { | ||
98 | sqlite3_step(compiled_statement); | ||
99 | NSLog(@"awesome, %@", [textField text]); | ||
100 | } else { | ||
101 | NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg([Cart_CollectAppDelegate database])); | ||
102 | } | ||
103 | |||
104 | if (submitSwitch.on) | ||
105 | { | ||
106 | [self submitScore]; | ||
107 | } else { | ||
108 | [self exit]; | ||
109 | } | ||
110 | } | ||
111 | |||
112 | - (void)submitScore | ||
113 | { | ||
114 | [activityIndicator startAnimating]; | ||
115 | |||
116 | CLScoreServerPost* server = [[CLScoreServerPost alloc] initWithGameName:@"Cart Collect" gameKey:@"38f440a074b3264386455a36b2706d8f" delegate:self]; | ||
117 | NSMutableDictionary* dict = [[NSMutableDictionary alloc] init]; | ||
118 | [dict setObject:@"Classic" forKey:@"cc_category"]; | ||
119 | [dict setObject:[textField text] forKey:@"cc_playername"]; | ||
120 | [dict setObject:[NSNumber numberWithInt:score] forKey:@"cc_score"]; | ||
121 | [server sendScore:dict]; | ||
122 | [server release]; | ||
123 | [dict release]; | ||
124 | } | ||
125 | |||
126 | - (BOOL)textFieldShouldReturn:(UITextField *)m_textField | ||
127 | { | ||
128 | [m_textField resignFirstResponder]; | ||
129 | |||
130 | return YES; | ||
131 | } | ||
132 | |||
133 | - (void)scorePostOk:(id)sender | ||
134 | { | ||
135 | [activityIndicator stopAnimating]; | ||
136 | |||
137 | // Score post successful | ||
138 | [self exit]; | ||
139 | } | ||
140 | |||
141 | - (void)scorePostFail:(id)sender | ||
142 | { | ||
143 | [activityIndicator stopAnimating]; | ||
144 | |||
145 | // score post failed | ||
146 | tPostStatus status = [sender postStatus]; | ||
147 | if( status == kPostStatusPostFailed ) { | ||
148 | NSLog(@"SERVER ERROR"); | ||
149 | // an error with the server ? | ||
150 | // try again | ||
151 | }else if( status == kPostStatusConnectionFailed ) { | ||
152 | NSLog(@"CONNECTION FAILURE"); | ||
153 | // a error establishing the connection ? | ||
154 | // turn-on wifi, and then try again | ||
155 | } | ||
156 | |||
157 | 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]; | ||
158 | [alertView show]; | ||
159 | [alertView release]; | ||
160 | } | ||
161 | |||
162 | - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex | ||
163 | { | ||
164 | if (buttonIndex == 0) | ||
165 | { | ||
166 | [self submitScore]; | ||
167 | } else if (buttonIndex == 1) | ||
168 | { | ||
169 | [self exit]; | ||
170 | } | ||
171 | } | ||
172 | |||
173 | - (void)exit | ||
174 | { | ||
175 | NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; | ||
176 | [defaults setObject:[textField text] forKey:@"username"]; | ||
177 | [defaults setBool:submitSwitch.on forKey:@"submitScore"]; | ||
178 | |||
179 | [submitSwitch removeFromSuperview]; | ||
180 | [submitSwitch release]; | ||
181 | |||
182 | [textField removeFromSuperview]; | ||
183 | [textField release]; | ||
184 | |||
185 | [scoreField removeFromSuperview]; | ||
186 | [scoreField release]; | ||
187 | |||
188 | [activityIndicator removeFromSuperview]; | ||
189 | [activityIndicator release]; | ||
190 | |||
191 | [backButton removeFromSuperview]; | ||
192 | |||
193 | [[CCDirector sharedDirector] replaceScene:[MainMenuLayer scene]]; | ||
194 | } | ||
195 | |||
196 | @end | ||