summary refs log tree commit diff stats
path: root/Classes/GameOverLayer.m
blob: 65b11e10b38f8470b9e071c45a2a78aa3f18e459 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//
//  GameOverLayer.m
//  Cart Collect
//
//  Created by iD Student Account on 7/19/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "GameOverLayer.h"


@implementation GameOverLayer

+ (CCScene*)sceneWithScore:(int)score
{
	CCScene* scene = [CCScene node];
	
	CCLayerColor* backgroundLayer = [CCLayerColor layerWithColor:ccc4(255, 255, 255, 255)];
	[scene addChild:backgroundLayer];
	
	GameOverLayer* layer = [[[GameOverLayer alloc] initWithScore:score] autorelease];
	[scene addChild:layer];
	
	return scene;
}

- (id)initWithScore:(int)score2
{
	self = [super init];
	
	if (nil != self)
	{
		CCSprite* backgroundImage = [CCSprite spriteWithFile:@"Morning1.png"];
		backgroundImage.position = ccp(240, 160);
		[self addChild:backgroundImage z:0];

		score = score2;
        
        scoreField = [[UILabel alloc] initWithFrame:CGRectMake(205, 320-200-320, 0, 0)];
        [scoreField setFont:[UIFont systemFontOfSize:20.0f]];
        [scoreField setBackgroundColor:[UIColor clearColor]];
        [scoreField setText:[NSString stringWithFormat:@"%d", score2]];
        CGSize labelSize = [scoreField.text sizeWithFont:scoreField.font constrainedToSize:CGSizeMake(160, 31) lineBreakMode:UILineBreakModeClip];
        [scoreField setFrame:CGRectMake(scoreField.frame.origin.x, scoreField.frame.origin.y, labelSize.width, labelSize.height)];
        [[[CCDirector sharedDirector] openGLView] addSubview:scoreField];
		
		textField = [[UITextField alloc] initWithFrame:CGRectMake(205, 320-247-320, 216, 31)];
        [textField setFont:[UIFont systemFontOfSize:20.0f]];
        [textField setBackgroundColor:[UIColor clearColor]];
		[textField setDelegate:self];
		NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
		[textField setText:[defaults objectForKey:@"username"]];
        [[[CCDirector sharedDirector] openGLView] addSubview:textField];
        
        submitSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(211, 320-161-320, 0, 0)];
        [submitSwitch setOn:[defaults boolForKey:@"submitScore"]];
        [[[CCDirector sharedDirector] openGLView] addSubview:submitSwitch];
        
        activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(480-20-10, 320-20-10, 20, 20)];
        [[[CCDirector sharedDirector] openGLView] addSubview:activityIndicator];
		
        backButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [backButton setFrame:CGRectMake(240-154/2, 320-60-15-320, 154, 31)];
        [backButton setTitle:@"Back to Main Menu" forState:UIControlStateNormal];
        [backButton addTarget:self action:@selector(newgame) forControlEvents:UIControlEventTouchUpInside];
        [backButton.titleLabel setFont:[UIFont systemFontOfSize:16.0]];
        [backButton.titleLabel setTextColor:[UIColor blackColor]];
        
        [[[CCDirector sharedDirector] openGLView] addSubview:backButton];
	}
	
	return self;
}

- (void)onEnter
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.5f];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    scoreField.transform = CGAffineTransformMakeTranslation(0, 320);
    textField.transform = CGAffineTransformMakeTranslation(0, 320);
    submitSwitch.transform = CGAffineTransformMakeTranslation(0, 320);
    backButton.transform = CGAffineTransformMakeTranslation(0, 320);
    [UIView commitAnimations];
}

- (void)newgame
{
    backButton.enabled = NO;
    textField.enabled = NO;
    submitSwitch.enabled = NO;
	
	const char* sqlQuery = [[NSString stringWithFormat:@"INSERT INTO highscores (name, score) VALUES (\"%@\",%d)", [textField text], score] UTF8String];
	sqlite3_stmt* compiled_statement;
	
	if (sqlite3_prepare_v2([Cart_CollectAppDelegate database], sqlQuery, -1, &compiled_statement, NULL) == SQLITE_OK)
	{
		sqlite3_step(compiled_statement);
		NSLog(@"awesome, %@", [textField text]);
	} else {
		NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg([Cart_CollectAppDelegate database]));
	}
	
    if (submitSwitch.on)
    {
        [self submitScore];
    } else {
        [self exit];
    }
}

- (void)submitScore
{	
    [activityIndicator startAnimating];
    
	CLScoreServerPost* server = [[CLScoreServerPost alloc] initWithGameName:@"Cart Collect" gameKey:@"38f440a074b3264386455a36b2706d8f" delegate:self];
	NSMutableDictionary* dict = [[NSMutableDictionary alloc] init];
	[dict setObject:@"Classic" forKey:@"cc_category"];
	[dict setObject:[textField text] forKey:@"cc_playername"];
	[dict setObject:[NSNumber numberWithInt:score] forKey:@"cc_score"];
	[server sendScore:dict];
	[server release];
    [dict release];
}

- (BOOL)textFieldShouldReturn:(UITextField *)m_textField
{
	[m_textField resignFirstResponder];
	
	return YES;
}

- (void)scorePostOk:(id)sender
{
    [activityIndicator stopAnimating];
    
    // Score post successful
	[self exit];
}

- (void)scorePostFail:(id)sender
{
    [activityIndicator stopAnimating];
    
    // score post failed
    tPostStatus status = [sender postStatus];
    if( status == kPostStatusPostFailed ) {
		NSLog(@"SERVER ERROR");
        // an error with the server ?
        // try again
    }else if( status == kPostStatusConnectionFailed ) {
		NSLog(@"CONNECTION FAILURE");
        // a error establishing the connection ?
        // turn-on wifi, and then try again
    }
	
	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];
	[alertView show];
    [alertView release];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
	if (buttonIndex == 0)
	{
		[self submitScore];
	} else if (buttonIndex == 1)
	{
		[self exit];
	}
}

- (void)exit
{
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
	[defaults setObject:[textField text] forKey:@"username"];
    [defaults setBool:submitSwitch.on forKey:@"submitScore"];
    
    [submitSwitch removeFromSuperview];
    [submitSwitch release];
	
	[textField removeFromSuperview];
	[textField release];
    
    [scoreField removeFromSuperview];
    [scoreField release];
    
    [activityIndicator removeFromSuperview];
    [activityIndicator release];
    
    [backButton removeFromSuperview];
	
	[[CCDirector sharedDirector] replaceScene:[MainMenuLayer scene]];
}

@end