// // GameMode.m // Cart Collect // // Created by Starla Insigna on 8/9/11. // Copyright 2011 Four Island. All rights reserved. // #import "GameMode.h" #import "FallingObject.h" #import "MainMenuLayer.h" @implementation GameMode @synthesize cart, score, lives, isPaused, pointMultiplier; + (CCScene*)scene { CCScene* scene = [CCScene node]; GameMode* layer = [self node]; layer.tag = GAME_LAYER; [scene addChild:layer]; scene.tag = GAME_SCENE; return scene; } - (id)init { self = [super init]; if (nil != self) { objects = [[NSMutableSet alloc] init]; cart = [[Cart alloc] initWithSprite:[CCSprite spriteWithFile:@"cart.png"]]; cart.sprite.position = ccp(240, 22); cart.sprite.scale = 2; cart.delegate = self; [self addChild:cart.sprite]; scoreLabel = [CCLabelBMFont labelWithString:@"Score: 0" fntFile:@"helvetica2.fnt"]; scoreLabel.position = ccp(50, 300); [self addChild:scoreLabel]; livesLabel = [CCLabelBMFont labelWithString:@"Lives: 3" fntFile:@"helvetica2.fnt"]; livesLabel.position = ccp(50, 280); [self addChild:livesLabel]; score = 0; lives = 3; pointMultiplier = 1; if ([self canPause]) { CCMenuItemImage* pauseButton = [CCMenuItemImage itemFromNormalImage:@"pause2.png" selectedImage:@"pause.png" target:self selector:@selector(pause)]; CCMenu* pauseMenu = [CCMenu menuWithItems:pauseButton, nil]; [pauseMenu setPosition:ccp(480-8-16, 320-8-16)]; [self addChild:pauseMenu]; } isPaused = NO; Class cmClass = (NSClassFromString(@"CMMotionManager")); if (cmClass) { motionManager = [[CMMotionManager alloc] init]; if (motionManager.gyroAvailable) { [motionManager setDeviceMotionUpdateInterval:1.0f/60.0f]; isAccelerometerEnabled_ = NO; hasGyroscope = YES; } else { isAccelerometerEnabled_ = YES; motionManager = nil; hasGyroscope = NO; } } else { isAccelerometerEnabled_ = YES; hasGyroscope = NO; } } return self; } - (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration { self.pitch = acceleration.y; } - (void)onEnterTransitionDidFinish { [super onEnterTransitionDidFinish]; if (hasGyroscope) { [motionManager startDeviceMotionUpdates]; } else { [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / 60)]; } [self schedule:@selector(tick:) interval:1.0f/60.0f]; } - (void)onExit { if (hasGyroscope) { [motionManager stopDeviceMotionUpdates]; } } - (void)tick:(ccTime)dt { if (hasGyroscope) { CMDeviceMotion* motion = [motionManager deviceMotion]; self.pitch = -motion.attitude.pitch; } [cart tick]; NSMutableSet* discardedObjects = [NSMutableSet set]; for (FallingObject* object in objects) { if ([object tick]) { [discardedObjects addObject:object]; } } [objects minusSet:discardedObjects]; } - (BOOL)canPause { return YES; } - (void)pause { if (([self canPause]) && (!isPaused)) { isPaused = YES; [self pauseSchedulerAndActions]; if (hasGyroscope) { [motionManager stopDeviceMotionUpdates]; } shadedLayer = [CCLayerColor layerWithColor:ccc4(0, 0, 0, 127)]; [[[CCDirector sharedDirector] runningScene] addChild:shadedLayer]; pauseLayer = [CCLayer node]; CCLabelBMFont* scoreLabel2 = [CCLabelBMFont labelWithString:@"PAUSE" fntFile:@"helvetica.fnt"]; scoreLabel2.position = ccp(240,90); [pauseLayer addChild:scoreLabel2]; CCMenuItemImage* pauseButton = [CCMenuItemImage itemFromNormalImage:@"pause2.png" selectedImage:@"pause.png" target:self selector:@selector(unpause)]; CCMenu* pauseMenu = [CCMenu menuWithItems:pauseButton, nil]; [pauseMenu setPosition:ccp(480-8-16, 320-8-16)]; [pauseLayer addChild:pauseMenu]; CCMenuItemImage* newgameMenuItem = [CCMenuItemImage itemFromNormalImage:@"back.png" selectedImage:@"back2.png" target:self selector:@selector(mainmenu)]; CCMenu* myMenu = [CCMenu menuWithItems:newgameMenuItem, nil]; myMenu.position = ccp(240, 60); [pauseLayer addChild:myMenu]; [[[CCDirector sharedDirector] runningScene] addChild:pauseLayer]; } } - (void)unpause { if (isPaused) { [[[CCDirector sharedDirector] runningScene] removeChild:shadedLayer cleanup:YES]; [[[CCDirector sharedDirector] runningScene] removeChild:pauseLayer cleanup:YES]; shadedLayer = nil; pauseLayer = nil; if (hasGyroscope) { [motionManager startDeviceMotionUpdates]; } [self resumeSchedulerAndActions]; isPaused = NO; } } - (void)mainmenu { UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Cartographic" message:@"Are you sure you want to quit the currently playing game?" delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:@"No", nil]; [alert show]; [alert release]; } - (void)setScore:(int)m_score { score = m_score; if (pointMultiplier > 1) { [scoreLabel setString:[NSString stringWithFormat:@"Score: %d x%d", score, pointMultiplier]]; } else { [scoreLabel setString:[NSString stringWithFormat:@"Score: %d", score]]; } } - (void)setLives:(int)m_lives { lives = m_lives; [livesLabel setString:[NSString stringWithFormat:@"Lives: %d", lives]]; } - (void)setPointMultiplier:(int)m_pointMultiplier { pointMultiplier = m_pointMultiplier; if (pointMultiplier > 1) { [scoreLabel setString:[NSString stringWithFormat:@"Score: %d x%d", score, pointMultiplier]]; } else { [scoreLabel setString:[NSString stringWithFormat:@"Score: %d", score]]; } } - (void)scheduleDelayedAction:(void(^)(void))m_delayedAction delay:(float)delay { delayedAction = Block_copy([m_delayedAction retain]); [self schedule:@selector(runDelayedAction) interval:delay]; } - (void)runDelayedAction { [self unschedule:@selector(runDelayedAction)]; delayedAction(); [delayedAction release]; delayedAction = nil; } - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { if (buttonIndex == 0) { [[CCDirector sharedDirector] replaceScene:[MainMenuLayer scene]]; } } - (void)setPitch:(double)m_pitch { pitch = m_pitch; [cart deviceDidRotate:pitch]; } - (void)dealloc { [objects release]; [cart release]; [super dealloc]; } @end