All Data Structures Files Functions Variables Enumerations Enumerator Properties Defines
/Projects/Cogito/src/Layers/GameplayLayer.m
Go to the documentation of this file.
00001 //
00002 //  GameplayLayer.m
00003 //  Author: Thomas Taylor
00004 //
00005 //  The main layer in the game, handles 
00006 //  the main 'gameplay' elements
00007 //
00008 //  13/11/2011: Created class
00009 //
00010 
00011 #import "GameplayLayer.h"
00012 
00013 @interface GameplayLayer()
00014 
00015 -(void)initDisplay;
00016 -(void)showPauseInstructions;
00017 -(void)update:(ccTime)deltaTime;
00018 -(NSString*)getUpdatedLemmingString;
00019 -(NSString*)getUpdatedTimeString;
00020 -(void)addLemming;
00021 -(void)createLemmingAtLocation:(CGPoint)spawnLocation withHealth:(int)health withZValue:(int)zValue withID:(int)ID;
00022 -(void)incrementGameTimer;
00023 -(void)onPauseButtonPressed;
00024 
00025 @end
00026 
00027 @implementation GameplayLayer
00028 
00029 #pragma mark -
00030 #pragma mark Memory Allocation
00031 
00032 -(void)dealloc
00033 {        
00034     [lemmingText dealloc];
00035     [timeText dealloc];
00036     [currentTerrainLayer dealloc];
00037 
00038     [super dealloc];
00039 }
00040 
00041 #pragma mark -
00042 #pragma mark Initialisation
00043 
00048 -(id)init 
00049 {            
00050     self = [super init];
00051  
00052     if (self != nil) 
00053     {
00054         self.isTouchEnabled = YES; // enable touch
00055                 
00056         // reset the relevant data
00057         [[LemmingManager sharedLemmingManager] reset];
00058         [[GameManager sharedGameManager] resetSecondCounter];
00059         
00060         // create/ add the batch node
00061         [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:[NSString stringWithFormat:@"%@.plist", kFilenameDefAtlas]];
00062         sceneSpriteBatchNode = [CCSpriteBatchNode batchNodeWithFile:[NSString stringWithFormat:@"%@.png", kFilenameDefAtlas]];
00063         [self addChild:sceneSpriteBatchNode z:0];
00064         
00065         // set up the labels/buttons
00066         //[self initDisplay];
00067         
00068         // Get the level data from GameManager
00069         [[GameManager sharedGameManager] loadRandomLevel];
00070         
00071         // initialise the terrain layer
00072         currentTerrainLayer = [[TerrainLayer alloc] init:[[GameManager sharedGameManager] currentLevel].name];
00073         [self addChild:currentTerrainLayer z:kTerrainZValue];
00074         
00075         [self schedule:@selector(addLemming) interval:kLemmingSpawnSpeed]; // create some lemmings
00076         [self scheduleUpdate]; // set the update method to be called every frame
00077         
00078         [self showPauseInstructions];
00079     }
00080             
00081     return self;
00082 }
00083 
00090 -(void)initDisplay
00091 {    
00092     CGSize winSize = [CCDirector sharedDirector].winSize;
00093     
00094     // add the pause button
00095     
00096     CCMenuItem *pauseButton = [CCMenuItemImage itemFromNormalImage:@"Pause.png" selectedImage:@"Pause_down.png" target:self selector:@selector(onPauseButtonPressed)];
00097     pauseButton.position = ccp(40,30);
00098     gameplayMenu = [CCMenu menuWithItems:pauseButton, nil];
00099     gameplayMenu.position = CGPointZero;
00100     
00101     [self addChild:gameplayMenu z:kUIZValue];
00102     
00103     // now add the labels
00104     
00105     lemmingText = [CCLabelBMFont labelWithString:[self getUpdatedLemmingString] fntFile:kFilenameDefFontSmall];
00106     [lemmingText setAnchorPoint:ccp(1,1)];
00107     [lemmingText setPosition:ccp(winSize.width-10, winSize.height-10)];
00108     [self addChild:lemmingText z:kUIZValue];
00109     
00110     timeText = [CCLabelBMFont labelWithString:[self getUpdatedTimeString] fntFile:kFilenameDefFontSmall];
00111     [timeText setAnchorPoint:ccp(1,1)];
00112     [timeText setPosition:ccp(winSize.width-10, winSize.height-30)];
00113     [self addChild:timeText z:kUIZValue];
00114 }
00115 
00119 -(void)showPauseInstructions
00120 {
00121     CGSize winSize = [CCDirector sharedDirector].winSize;
00122     
00123     // add the image
00124     pauseInstructions = [CCSprite spriteWithFile:@"PauseInstructions.png"];
00125     [pauseInstructions setAnchorPoint:ccp(0.5, 1)];
00126     [pauseInstructions setPosition:ccp(winSize.width/2, winSize.height+115)];
00127     [self addChild:pauseInstructions z:kUIZValue];
00128     
00129     // animation
00130     id delay = [CCDelayTime actionWithDuration:1.5f];
00131     id animateInAction = [CCMoveTo actionWithDuration:0.75f position:ccp(winSize.width/2, winSize.height)];
00132     id longDelay = [CCDelayTime actionWithDuration:3.0f];
00133     id animateOutAction = [CCMoveTo actionWithDuration:0.75f position:ccp(winSize.width/2, winSize.height+115)]; 
00134     id removePopup = [CCCallFunc actionWithTarget:self selector:@selector(removePauseInstructions)];
00135     id pauseInstructionsAction = [CCSequence actions:delay, animateInAction, longDelay, animateOutAction, removePopup, nil];
00136     [pauseInstructions runAction:pauseInstructionsAction];
00137 }
00138 
00139 -(void)removePauseInstructions
00140 {
00141     [pauseInstructions removeFromParentAndCleanup:YES];
00142 }
00143 
00144 #pragma mark -
00145 #pragma mark Update
00146 
00151 -(void)update:(ccTime)deltaTime
00152 {
00153     CCArray* lemmings = [[LemmingManager sharedLemmingManager] lemmings];
00154     
00155     CCArray* terrainObjects = [CCArray arrayWithArray:[currentTerrainLayer obstacles]];
00156     [terrainObjects addObjectsFromArray:[currentTerrainLayer terrain]]; 
00157     
00158     for (Lemming *tempLemming in lemmings) 
00159     {
00160         [tempLemming updateStateWithDeltaTime:deltaTime andListOfGameObjects:terrainObjects];
00161     }
00162     
00163     //update the display text
00164     [lemmingText setString:[self getUpdatedLemmingString]];
00165     [timeText setString:[self getUpdatedTimeString]];
00166     
00167     [self incrementGameTimer];
00168 }
00169 
00174 -(NSString*)getUpdatedLemmingString
00175 {
00176     return [NSString stringWithFormat:@"left: %i   saved: %i   killed: %i", 
00177                             [[LemmingManager sharedLemmingManager] lemmingCount],
00178                             [[LemmingManager sharedLemmingManager] lemmingsSaved],
00179                             [[LemmingManager sharedLemmingManager] lemmingsKilled]];
00180 }
00181 
00186 -(NSString*)getUpdatedTimeString
00187 {
00188     return [NSString stringWithFormat:@"time: %@", [[GameManager sharedGameManager] getGameTimeInMins]];
00189 }
00190 
00191 #pragma mark -
00192 #pragma mark Object Creation
00193 
00197 -(void)addLemming
00198 {
00199     int lemmingCount = [LemmingManager sharedLemmingManager].lemmingCount;
00200     CGPoint spawnPoint = [GameManager sharedGameManager].currentLevel.spawnPoint;
00201     
00202     [self createLemmingAtLocation:ccp(spawnPoint.x, spawnPoint.y) withHealth:100 withZValue:(lemmingCount+10) withID:lemmingCount];
00203 }
00204 
00211 -(void)createLemmingAtLocation:(CGPoint)spawnLocation withHealth:(int)health withZValue:(int)zValue withID:(int)ID  
00212 {
00213     if (![[LemmingManager sharedLemmingManager] lemmingsMaxed]) 
00214     {
00215         Lemming *lemming;
00216         
00217         MachineLearningType learningType = [[LemmingManager sharedLemmingManager] learningType];
00218         
00219         Class class = nil;
00220         
00221         switch(learningType) 
00222         {                
00223             case kLearningMixed:
00224                 // randomly choose a learning type
00225                 learningType = [Utils generateRandomNumberFrom:0 to:kLearningMixed]; 
00226                 break;
00227                 
00228             case kLearningReinforcement:
00229                 class = [QLearningAgent class];
00230                 break;
00231                 
00232             case kLearningTree:
00233                 class = [DecisionTreeAgent class];
00234                 break;
00235                 
00236             case kLearningShortestRoute:
00237                 class = [ShortestRouteAgent class];
00238                 break;
00239          
00240             case kLearningNone:
00241                 class = [CogitoAgent class];
00242                 break;
00243                 
00244             default:
00245                 CCLOG(@"%@.createLemmingAtLocation: Error, learning type not recognised: %@", NSStringFromClass([self class]), [Utils getLearningTypeAsString:learningType]);
00246                 break;
00247         }
00248         
00249         lemming = [[class alloc] initWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:kFilenameDefLemmingFrame]];
00250         
00251         lemming.ID = ID;
00252         lemming.health = health;
00253         
00254         // add the debug label
00255         CCLabelBMFont *debugLabel = [CCLabelBMFont labelWithString:@"" fntFile:kFilenameDefFontDebug];
00256         [self addChild:debugLabel];
00257         [lemming setDebugLabel:debugLabel];
00258         
00259         [[LemmingManager sharedLemmingManager] addLemming:lemming]; 
00260         
00261         // set the helmet/umbrella uses from the level data
00262         lemming.helmetUses = [GameManager sharedGameManager].currentLevel.helmetUses;
00263         lemming.umbrellaUses = [GameManager sharedGameManager].currentLevel.umbrellaUses;
00264         
00265         [lemming setPosition:spawnLocation]; 
00266         [sceneSpriteBatchNode addChild:lemming z:zValue tag:kLemmingSpriteTagValue];
00267         [lemming release];
00268     }
00269     else [self unschedule:@selector(addLemming)];
00270 }
00271 
00272 #pragma mark -
00273 
00277 -(void)incrementGameTimer
00278 {
00279     if(frameCounter == kFrameRate)
00280     {
00281         [[GameManager sharedGameManager] incrementSecondCounter];  
00282         frameCounter = 0;
00283     }
00284     else frameCounter++;
00285 }
00286 
00287 #pragma mark -
00288 #pragma mark Event Handling
00289 
00293 -(void)onPauseButtonPressed
00294 {    
00295     if(pauseMenu == nil) 
00296     {
00297         pauseMenu = [PauseMenuLayer node];
00298         [self addChild:pauseMenu z:kPauseMenuZValue];
00299     }
00300     
00301     if(![[GameManager sharedGameManager] gamePaused]) [pauseMenu animateIn];
00302 }
00303 
00309 -(void)ccTouchesEnded:(UITouch *)touch withEvent:(UIEvent *)event
00310 {
00311     if(![pauseMenu animating]) [self onPauseButtonPressed];
00312 }
00313 
00314 - (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { }
00315 
00316 @end