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