summary refs log blame commit diff stats
path: root/Classes/GameOverScene.m
blob: a56aeace67c94e0d5a68ec5fe4f6292634880d43 (plain) (tree)
1
2
3
4
5
6
7
8
9






                                                          
                         


                                   
                           
 
                             
 
                                                                        
 
                                                                                       
 
                                                             
 
                        

                        




                                                                                               
                                                                                      
                                                        
                               
                             
        



                                                                                    



                                                                                                                                                  
                                            
                
                                                                                                  



                                                                                 
                                           
        
                                                                                       
                                                                  
                                              
                                                                                                                     
                                                   






                                                                                                            
                                                                       
                                                                            
                                                                                 
                                                                                                            
                                                                       
                                            



                    
                                     
 
                                                          
 
                          
 


                                                           


                              
                                                                                                                                                                                       



                                                                                                                         
                                                                   
                
                                                                                                                                         














                                                                                                                                                           






                                                          




























































                                                                                                                                                                                                                                                          
                       

                                        
                    

                                     
                     

                                            
                            
    

                                     
                                     
                     

                                      
                      
        










                                                                                                                                                            

    
//
//  GameOverLayer.m
//  Cart Collect
//
//  Created by iD Student Account on 7/19/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "GameOverScene.h"
#import "Cart_CollectAppDelegate.h"
#import <sqlite3.h>
#import "cocoslive.h"
#import "MainMenuLayer.h"
#import "ClassicGameMode.h"
#import "JumpGameMode.h"

@implementation GameOverScene

+ (GameOverScene*)sceneWithScore:(int)score gameMode:(NSString*)gameMode
{
    return [[[GameOverScene alloc] initWithScore:score gameMode:gameMode] autorelease];
}

- (id)initWithScore:(int)score2 gameMode:(NSString*)gameMode2
{
    self = [super init];
	
	if (nil != self)
	{
        CCLayerColor* backgroundLayer = [CCLayerColor layerWithColor:ccc4(255, 255, 255, 255)];
        [self addChild:backgroundLayer];
        
        theLayer = [CCLayer node];
        [self addChild:theLayer];
        
		CCSprite* backgroundImage = [CCSprite spriteWithFile:@"Morning1.png"];
		backgroundImage.position = ccp(240, 160);
		[theLayer addChild:backgroundImage z:0];

		score = score2;
        gameMode = gameMode2;
        
        movingLayer = [[UIView alloc] initWithFrame:CGRectMake(0, -320, 480, 320)];
        movingLayer.backgroundColor = [UIColor clearColor];
        [[[CCDirector sharedDirector] openGLView] addSubview:movingLayer];
        
        scoreField = [[UILabel alloc] initWithFrame:CGRectMake(205, 320-200, 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)];
        [movingLayer addSubview:scoreField];
		
		textField = [[UITextField alloc] initWithFrame:CGRectMake(205, 320-247, 216, 31)];
        [textField setFont:[UIFont systemFontOfSize:20.0f]];
        [textField setBackgroundColor:[UIColor clearColor]];
		[textField setDelegate:self];
		NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
		[textField setText:[defaults objectForKey:@"username"]];
        [movingLayer addSubview:textField];
        
        submitSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(211, 320-161, 0, 0)];
        [submitSwitch setOn:[defaults boolForKey:@"submitScore"]];
        [movingLayer addSubview:submitSwitch];
        
        activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(480-20-10, 320-20-10, 20, 20)];
        [movingLayer addSubview:activityIndicator];
        
        playButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [playButton setFrame:CGRectMake(480/3-105/2, 320-60-15, 105, 31)];
        [playButton setTitle:@"Play Again" forState:UIControlStateNormal];
        [playButton addTarget:self action:@selector(newgame:) forControlEvents:UIControlEventTouchUpInside];
        [playButton.titleLabel setFont:[UIFont systemFontOfSize:16.0]];
        [playButton.titleLabel setTextColor:[UIColor blackColor]];
        [movingLayer addSubview:playButton];
		
        backButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [backButton setFrame:CGRectMake(480/3*2-154/2, 320-60-15, 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]];
        [movingLayer addSubview:backButton];
	}
	
	return self;
}

- (void)setPosition:(CGPoint)position
{
    [super setPosition:position];
    movingLayer.center = CGPointMake(240, 160-position.y);
}

- (void)newgame:(id)sender
{
    // Play again if the user pressed the play again button
    playAgain = (sender == playButton);
    
    playButton.enabled = NO;
    backButton.enabled = NO;
    textField.enabled = NO;
    submitSwitch.enabled = NO;
	
	const char* sqlQuery = [[NSString stringWithFormat:@"INSERT INTO highscores (name, score, gameMode) VALUES (\"%@\",%d,\"%@\")", [textField text], score, gameMode] 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, %@, %d", [textField text], score);
	} else {
		NSLog(@"Error while adding highscore to local highscore list. '%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];
    
    if ([gameMode isEqual:@"Collect"])
    {
        [dict setObject:@"Classic" forKey:@"cc_category"];
    } else {
        [dict setObject:gameMode 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];
    submitSwitch = nil;
	
	[textField removeFromSuperview];
	[textField release];
    textField = nil;
    
    [scoreField removeFromSuperview];
    [scoreField release];
    scoreField = nil;
    
    [activityIndicator removeFromSuperview];
    [activityIndicator release];
    activityIndicator = nil;
    
    [playButton removeFromSuperview];
    playButton = nil;
    
    [backButton removeFromSuperview];
    backButton = nil;
    
    [movingLayer removeFromSuperview];
    [movingLayer release];
    movingLayer = nil;
	
    if (playAgain)
    {
        if ([gameMode isEqual:@"Collect"])
        {
            [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:3.0f scene:[ClassicGameMode scene] withColor:ccc3(0, 0, 0)]];
        } else if ([gameMode isEqual:@"Jump"])
        {
            [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:3.0f scene:[JumpGameMode scene] withColor:ccc3(0, 0, 0)]];
        }
    } else {
        [[CCDirector sharedDirector] replaceScene:[MainMenuLayer scene]];
    }
}

@end