Skip to content

Commit

Permalink
Fixed stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
nightninja committed Jul 1, 2011
1 parent 3712efe commit 7563d42
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 38 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
/Users/tristan/Dropbox/gitcode/iphone/SquareGame/SquareGame/OpenFeint/**/*
/Users/tristan/Dropbox/gitcode/iphone/SquareGame/SquareGame/libs/**/*
24 changes: 22 additions & 2 deletions SquareGame/AppDelegate.mm
Expand Up @@ -7,11 +7,13 @@
//

#import "cocos2d.h"
#import "OpenFeint.h"

#import "AppDelegate.h"
#import "GameConfig.h"
#import "MenuScene.h"
#import "RootViewController.h"
#import "SimpleOFDelegate.h"

@implementation AppDelegate

Expand Down Expand Up @@ -108,18 +110,34 @@ - (void) applicationDidFinishLaunching:(UIApplication*)application

// Removes the startup flicker
[self removeStartupFlicker];


//start OpenFeint

NSDictionary* settings = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight], OpenFeintSettingDashboardOrientation, [NSNumber numberWithBool:YES],OpenFeintSettingDisableUserGeneratedContent, nil];
ofDelegate = [SimpleOFDelegate new];
OFDelegatesContainer* delegates = [OFDelegatesContainer containerWithOpenFeintDelegate: ofDelegate];
[OpenFeint initializeWithProductKey:@"gm8xCkhFeSA8iFOm76IAIQ"
andSecret:@"EiBa1BNOtQ3pfSMroeJryJ24nJkRmih2uu0t9CG0"
andDisplayName:@"SquareGame"
andSettings:settings // see OpenFeintSettings.h
andDelegates:delegates]; // see OFDelegatesContainer.h


// Run the intro Scene
[[CCDirector sharedDirector] runWithScene: [MenuScene scene]];
}


- (void)applicationWillResignActive:(UIApplication *)application {
[[CCDirector sharedDirector] pause];
//[OpenFeint applicationWillResignActive]; // Add for OpenFeint

}

- (void)applicationDidBecomeActive:(UIApplication *)application {
[[CCDirector sharedDirector] resume];
//[OpenFeint applicationDidBecomeActive]; // Add for OpenFeint

}

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
Expand All @@ -143,7 +161,9 @@ - (void)applicationWillTerminate:(UIApplication *)application {

[window release];

[director end];
[director end];
[OpenFeint shutdown];
[[NSUserDefaults standardUserDefaults] synchronize]; // Add for OpenFeint
}

- (void)applicationSignificantTimeChange:(UIApplication *)application {
Expand Down
22 changes: 14 additions & 8 deletions SquareGame/MenuScene.mm
Expand Up @@ -8,7 +8,9 @@

#import "MenuScene.h"
#import "SquaresLayer.h"
#import "GameCenterManager.h"
//#import "GameCenterManager.h"
#import "ControlsMenu.h"
#import "OpenFeint.h"

@implementation MenuScene

Expand All @@ -28,8 +30,7 @@ -(id) init

if( (self=[super init] )) {
CGSize size = [[CCDirector sharedDirector] winSize];
CCSprite *menuBG = [CCSprite spriteWithFile:@"MenuBG.png"
rect:CGRectMake(0, 0, 480, 320)];
CCSprite *menuBG = [CCSprite spriteWithFile:@"MenuBG.png"];
menuBG.position = ccp(size.width/2,size.height/2);
[self addChild:menuBG];

Expand All @@ -44,11 +45,14 @@ -(id) init
CCMenu *menu = [CCMenu menuWithItems: startButton, nil];*/
[CCMenuItemFont setFontSize:90];
CCMenuItem *start = [CCMenuItemFont itemFromString:@"PLAY" target:self selector:@selector(startGame:)];
[CCMenuItemFont setFontSize:45];
CCMenuItem *controls = [CCMenuItemFont itemFromString:@"OPTIONS" target:self selector:@selector(chooseControls)];
CCMenuItem *of = [CCMenuItemFont itemFromString:@"OpenFeint" target:self selector:@selector(openOpenFeint)];


// Creating menu and adding items
CCMenu *menu = [CCMenu menuWithItems:start, nil];
menu.position = ccp(size.width/2 - 7,size.height/2);
CCMenu *menu = [CCMenu menuWithItems:start,controls,of, nil];
menu.position = ccp(size.width/2 - 7,size.height/2 - 40);
// Set menu alignment to vertical
[menu alignItemsVertically];

Expand All @@ -59,16 +63,18 @@ -(id) init
return self;
}

- (void) authenticateLocalPlayer
{
[GameCenterManager authenticateLocalUser];
- (void) openOpenFeint {
[OpenFeint launchDashboard];
}

- (void) startGame: (id) sender
{
//[[CCDirector sharedDirector] replaceScene:[SquaresLayer scene]];
[[CCDirector sharedDirector] replaceScene:[CCTransitionSplitRows transitionWithDuration:0.6 scene:[SquaresLayer node]]];
}
- (void) chooseControls {
[[CCDirector sharedDirector] replaceScene:[CCTransitionSlideInR transitionWithDuration:0.6 scene:[ControlsMenu node]]];
}

- (void) dealloc
{
Expand Down
157 changes: 129 additions & 28 deletions SquareGame/SquaresLayer.mm
Expand Up @@ -9,6 +9,9 @@

// Import the interfaces
#import "SquaresLayer.h"
#import "MenuScene.h"
#import "OpenFeint.h"
#import "OFHighScoreService.h"

// HelloWorldLayer implementation
@implementation SquaresLayer
Expand Down Expand Up @@ -38,13 +41,25 @@ -(id) init
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init])) {

size = [[CCDirector sharedDirector] winSize];
controlMode = [[NSUserDefaults standardUserDefaults] integerForKey:@"controlType"];

CCSprite *gameBG;
if(controlMode == 1) {
gameBG = [CCSprite spriteWithFile:@"GameBGSwipe.png"];
} else {
gameBG = [CCSprite spriteWithFile:@"GameBGTouch.png"];
}
gameBG.position = ccp(size.width/2,size.height/2);
[self addChild:gameBG];

// create and initialize a Label
label = [CCLabelAtlas labelAtlasWithString:@"Hello World" charMapFile:@"charmap.png" itemWidth:24 itemHeight:32 startCharMap:' '];
[label setAnchorPoint: ccp(1, 0.5f)];

// ask director the the window size
size = [[CCDirector sharedDirector] winSize];


// position the label on the center of the screen
label.position = ccp( size.width - 5 , 25 );
Expand All @@ -61,16 +76,44 @@ -(id) init
[self addChild:square];
[self resetSquare];

upGesture = [self watchForSwipe:@selector(moveUp) forDir:UISwipeGestureRecognizerDirectionUp];
downGesture = [self watchForSwipe:@selector(moveDown) forDir:UISwipeGestureRecognizerDirectionDown];
leftGesture = [self watchForSwipe:@selector(moveLeft) forDir:UISwipeGestureRecognizerDirectionLeft];
rightGesture = [self watchForSwipe:@selector(moveRight) forDir:UISwipeGestureRecognizerDirectionRight];

if(controlMode == 1) {
upGesture = [self watchForSwipe:@selector(moveUp) forDir:UISwipeGestureRecognizerDirectionUp];
downGesture = [self watchForSwipe:@selector(moveDown) forDir:UISwipeGestureRecognizerDirectionDown];
leftGesture = [self watchForSwipe:@selector(moveLeft) forDir:UISwipeGestureRecognizerDirectionLeft];
rightGesture = [self watchForSwipe:@selector(moveRight) forDir:UISwipeGestureRecognizerDirectionRight];
} else {
self.isTouchEnabled = YES;
}

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
maxScore = [prefs integerForKey:@"highscore"];



// died menu

menuLayer = [[CCLayer alloc] init];

CCSprite *menuBG = [CCSprite spriteWithFile:@"DiedMenuBG.png"];
menuBG.position = ccp(size.width/2,size.height/2);
[menuLayer addChild:menuBG];

[CCMenuItemFont setFontSize:30];
CCMenuItem *again = [CCMenuItemFont itemFromString:@"Play Again!" target:self
selector:@selector(playAgain)];
CCMenuItem *main = [CCMenuItemFont itemFromString:@"Menu" target:self
selector:@selector(goBack)];


// Creating menu and adding items
CCMenu *menu = [CCMenu menuWithItems:again,main, nil];
menu.position = ccp(size.width/2 - 7,size.height/2);
// Set menu alignment to vertical
[menu alignItemsVertically];

[menuLayer addChild: menu];

}
return self;
}
Expand All @@ -87,40 +130,96 @@ - (void)unwatch:(UIGestureRecognizer *)gr {
}

- (void) faster {
speed += 2;
speed += 1;
}

- (void) movement:(ccTime) dt {
float tf = (CACurrentMediaTime() - lastFrame) * 10;
square.position = ccp(tf*speed*xDir + square.position.x,tf*speed*yDir + square.position.y);



NSUInteger score = (CACurrentMediaTime() - sTime) * 50;
//Canvas edge
if (square.position.x > size.width - 12 || square.position.x < 0 || square.position.y > size.height - 12 || square.position.y < 0) {
[self resetSquare];
if (score > maxScore) {
maxScore = score;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:score forKey:@"highscore"];
if(playing) {
float tf = (CACurrentMediaTime() - lastFrame) * 10;
square.position = ccp(tf*speed*xDir + square.position.x,tf*speed*yDir + square.position.y);



NSUInteger score = (CACurrentMediaTime() - sTime) * 50;
//Canvas edge
if (square.position.x > size.width - 12 || square.position.x < 0 || square.position.y > size.height - 12 || square.position.y < 0) {
[self submitScore:score];
[self died];
}

[label setString:[NSString stringWithFormat:@"%d/%d", score,maxScore]];
lastFrame = CACurrentMediaTime();
}
}

- (void) submitScore:(NSUInteger)score {
if (score > maxScore) {
maxScore = score;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:score forKey:@"highscore"];
}

[label setString:[NSString stringWithFormat:@"%d/%d", score,maxScore]];
lastFrame = CACurrentMediaTime();

NSString *leaderboard;
if(controlMode == 1) {
leaderboard = @"802966";
} else {
leaderboard = @"802976";
}
[OFHighScoreService setHighScore:score forLeaderboard:leaderboard
onSuccess:OFDelegate() onFailure:OFDelegate()];
}

- (void) died {
//[self unschedule:@selector(movement:)];
playing = NO;
[self addChild:menuLayer];
}
- (void) playAgain {
[self removeChild:menuLayer cleanup:NO];
//[self schedule:@selector(movement:)];
[self resetSquare];
}
- (void) goBack {
[[CCDirector sharedDirector] replaceScene:[CCTransitionSlideInL transitionWithDuration:0.6 scene:[MenuScene node]]];
}

- (void) resetSquare {
square.position = ccp(12, size.height - 12);
xDir = 1; yDir = -1;
speed = 1;
speed = 3;
sTime = CACurrentMediaTime();
lastFrame = CACurrentMediaTime();
playing = YES;
}

#pragma mark movement
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
{
if(controlMode == 2 || controlMode == 0) {
UITouch *touch = [touches anyObject];

CGPoint location = [[CCDirector sharedDirector] convertToGL:
[touch locationInView:touch.view]];

NSUInteger halfx = size.width / 2;
NSUInteger halfy = size.height / 2;
if(location.x > halfx && location.y > halfy && xDir != yDir) { // up right
xDir = 1;
yDir = 1;
} else if(location.x < halfx && location.y < halfy && xDir != yDir) { // bottom left
xDir = -1;
yDir = -1;
} else if(location.x > halfx && location.y < halfy && xDir == yDir) { // bottom right
xDir = 1;
yDir = -1;
} else if(location.x < halfx && location.y > halfy && xDir == yDir) { // top left
xDir = -1;
yDir = 1;
}
//yDir = location.y > halfy ? 1 : -1;
//NSLog(@"touch!");
}
}


-(void)moveUp {
yDir = 1;
Expand All @@ -141,10 +240,12 @@ - (void) dealloc
// in case you have something to dealloc, do it in this method
// in this particular example nothing needs to be released.
// cocos2d will automatically release all the children (Label)
[self unwatch:upGesture];
[self unwatch:downGesture];
[self unwatch:leftGesture];
[self unwatch:rightGesture];
if(controlMode == 1) {
[self unwatch:upGesture];
[self unwatch:downGesture];
[self unwatch:leftGesture];
[self unwatch:rightGesture];
}

// don't forget to call "super dealloc"
[super dealloc];
Expand Down

0 comments on commit 7563d42

Please sign in to comment.