All Data Structures Files Functions Variables Enumerations Enumerator Properties Defines
/Projects/Cogito/src/Layers/Menu/NewGameLayer.m
Go to the documentation of this file.
00001 //
00002 //  NewGameLayer.m
00003 //  Author: Thomas Taylor
00004 //
00005 //  The 'new game' layer
00006 //
00007 //  16/12/2011: Created class
00008 //
00009 
00010 #import "NewGameLayer.h"
00011 
00012 @interface NewGameLayer()
00013 
00014 -(void)initGUI;
00015 -(void)initButtons;
00016 -(void)initSegmentedControls;
00017 -(void)initSliders;
00018 -(void)initSwitches;
00019 -(void)animateOutComponents;
00020 -(void)removeComponents;
00021 -(IBAction)onSliderUpdated:(UISlider*)sender;
00022 -(IBAction)onSwitchUpdated:(UISwitch*)sender;
00023 -(IBAction)onSegmentedControlUpdated:(UISegmentedControl*)sender;
00024 -(void)onContinueButtonPressed;
00025 -(void)loadMainMenu;
00026 
00027 @end
00028 
00029 @implementation NewGameLayer
00030 
00031 #pragma mark -
00032 #pragma mark Initialisation
00033 
00038 -(id)init 
00039 {
00040  self = [super init];
00041  
00042     if (self != nil) 
00043     {
00044   CGSize winSize = [CCDirector sharedDirector].winSize; 
00045   
00046         // add the background
00047   CCSprite *background = [CCSprite spriteWithFile:@"NewGameBackground.png"];
00048   [background setPosition:ccp(winSize.width/2, winSize.height/2)];
00049   [self addChild:background];
00050 
00051         // add the facade
00052         facade = [CCSprite spriteWithFile:@"NewGameFacade.png"];
00053   [facade setPosition:ccp(winSize.width/2, winSize.height/2)];
00054   [self addChild:facade];
00055         
00056         lemmingCount = kLemmingTotal;
00057         learningEpisodes = KLearningEpisodes;
00058         sharedKnowledge = YES;
00059         debugMode = NO;
00060         learningType = kLearningReinforcement;
00061         
00062         [self performSelector:@selector(initGUI) withObject:nil afterDelay:0.6f];
00063  }
00064 
00065  return self;
00066 }
00067 
00071 -(void)initGUI
00072 {
00073     [self initButtons];
00074     [self initSegmentedControls];
00075     [self initSliders];
00076     [self initSwitches];
00077     
00078     // animate the UI components
00079     /*
00080                           * hide the facade when components are visible
00081                           * LIKE A NINJA
00082                           */[UIView animateWithDuration:0.6f 
00083                      animations:^
00084      { 
00085          [lemmingCountSlider setAlpha:1.0f];
00086          [lemmingCountSlider setAlpha:1.0f];
00087          [learningEpisodesSlider setAlpha:1.0f];
00088          [learningTypeControl setAlpha:1.0f];
00089          [sharedKnowledgeSwitch setAlpha:1.0f];
00090          [debugSwitch setAlpha:1.0f]; 
00091      }
00092                      completion:^(BOOL finished)
00093                      { 
00094                          
00095                          [facade setOpacity:0];
00096                      }
00097      ];
00098     
00099 }
00100 
00104 -(void)initButtons
00105 {    
00109     CCMenuItemImage* backButton = [CCMenuItemImage itemFromNormalImage:@"Back.png" selectedImage:@"Back_down.png" disabledImage:nil target:self selector:@selector(loadMainMenu)];
00110     CCMenuItemImage* continueButton = [CCMenuItemImage itemFromNormalImage:@"Continue.png" selectedImage:@"Continue_down.png" disabledImage:nil target:self selector:@selector(onContinueButtonPressed)];
00111     
00112     // intialise the menu
00113     menuButtons = [CCMenu menuWithItems:backButton, continueButton, nil];
00114     [menuButtons setPosition:ccp(0,0)];
00115     
00116     // position the buttons
00117     [backButton setPosition: ccp(80, 25)];
00118     [continueButton setPosition: ccp(420, 25)];
00119     
00120     // add the menu
00121     [self addChild:menuButtons z:kUIZValue];
00122 
00123 }
00124 
00128 -(void)initSegmentedControls
00129 {
00134     learningTypeControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"reinforcement", @"decision tree", @"shortest path", @"none", nil]];
00135     [learningTypeControl setAlpha:0.0f];
00136     [learningTypeControl setTintColor:[Utils getUIColourFromRed:136 green:150 blue:204]];
00137     [learningTypeControl addTarget:self action:@selector(onSegmentedControlUpdated:)forControlEvents:UIControlEventValueChanged];
00138     [learningTypeControl setFrame:CGRectMake(30, (320-148), 420, 50)];
00139     learningTypeControl.selectedSegmentIndex = kLearningType;
00140     [learningTypeControl setSegmentedControlStyle:UISegmentedControlStyleBar];
00141     [[[CCDirector sharedDirector] openGLView] addSubview:learningTypeControl];
00142 
00143 }
00144 
00148 -(void)initSliders
00149 {
00154     // slider
00155     lemmingCountSlider = [[UISlider alloc] initWithFrame:CGRectMake(202, (320-240), 220, 50)];
00156     lemmingCountSlider.minimumTrackTintColor = [Utils getUIColourFromRed:102 green:153 blue:204];
00157     [lemmingCountSlider addTarget:self action:@selector(onSliderUpdated:)forControlEvents:UIControlEventValueChanged];
00158     lemmingCountSlider.minimumValue = 1.0;
00159     lemmingCountSlider.maximumValue = kLemmingMax;
00160     [lemmingCountSlider setValue:kLemmingTotal];
00161     [[[CCDirector sharedDirector] openGLView] addSubview:lemmingCountSlider];
00162     
00163     // value label
00164     lemmingCountLabel = [CCLabelBMFont labelWithString:[NSString stringWithFormat:@"%i", kLemmingTotal] fntFile:kFilenameDefFontLarge];
00165     [lemmingCountLabel setPosition:ccp(440, 209)];
00166     [self addChild:lemmingCountLabel];
00167     
00168     // label
00169     CCLabelBMFont* lemmingCountLabel2 = [CCLabelBMFont labelWithString:@"agent count" fntFile:kFilenameDefFontLarge];
00170     [lemmingCountLabel2 setAnchorPoint:ccp(0,0)];
00171     [lemmingCountLabel2 setPosition:ccp(38, 195)];
00172     [self addChild:lemmingCountLabel2];
00173     
00178     // slider
00179     learningEpisodesSlider = [[UISlider alloc] initWithFrame:CGRectMake(202, (320-205), 220, 50)];
00180     [learningEpisodesSlider setAlpha:0.0f];
00181     learningEpisodesSlider.minimumValue = 1.0;
00182     learningEpisodesSlider.maximumValue = KLearningMaxEpisodes;
00183     [learningEpisodesSlider setValue:KLearningEpisodes];
00184     learningEpisodesSlider.minimumTrackTintColor = [Utils getUIColourFromRed:102 green:153 blue:204];
00185     [learningEpisodesSlider addTarget:self action:@selector(onSliderUpdated:)forControlEvents:UIControlEventValueChanged];
00186     [[[CCDirector sharedDirector] openGLView] addSubview:learningEpisodesSlider];
00187     
00188     // value label
00189     learningEpisodesLabel = [CCLabelBMFont labelWithString:[NSString stringWithFormat:@"%i", KLearningEpisodes] fntFile:kFilenameDefFontLarge];
00190     [learningEpisodesLabel setPosition:ccp(440, 174)];
00191     [self addChild:learningEpisodesLabel];
00192     
00193     // label
00194     CCLabelBMFont* learningEpisodesLabel2 = [CCLabelBMFont labelWithString:@"learning episodes" fntFile:kFilenameDefFontLarge];
00195     [learningEpisodesLabel2 setAnchorPoint:ccp(0,0)];
00196     [learningEpisodesLabel2 setPosition:ccp(38, 160)];
00197     [self addChild:learningEpisodesLabel2];
00198 
00199 }
00200 
00204 -(void)initSwitches
00205 {
00210     // switch
00211     sharedKnowledgeSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(178, (320-80), 200, 50)];
00212     [sharedKnowledgeSwitch setAlpha:0.0f];
00213     [sharedKnowledgeSwitch setOnTintColor:[Utils getUIColourFromRed:102 green:153 blue:204]];
00214     [sharedKnowledgeSwitch addTarget:self action:@selector(onSwitchUpdated:)forControlEvents:UIControlEventValueChanged];
00215     [sharedKnowledgeSwitch setOn:YES];
00216     [[[CCDirector sharedDirector] openGLView] addSubview:sharedKnowledgeSwitch];
00217     
00218     // label
00219     sharedKnowledgeLabel = [CCLabelBMFont labelWithString:@"shared knowledge" fntFile:kFilenameDefFontLarge];
00220     [sharedKnowledgeLabel setPosition:ccp(100, 61)];
00221     [self addChild:sharedKnowledgeLabel];
00222     
00227     // switch
00228     debugSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(374, (320-80), 200, 71)];
00229     [debugSwitch setAlpha:0.0f];
00230     [debugSwitch setOnTintColor:[Utils getUIColourFromRed:102 green:153 blue:204]];
00231     [debugSwitch addTarget:self action:@selector(onSwitchUpdated:)forControlEvents:UIControlEventValueChanged];
00232     [[[CCDirector sharedDirector] openGLView] addSubview:debugSwitch];
00233     
00234     // label
00235     debugModeLabel = [CCLabelBMFont labelWithString:@"debug mode" fntFile:kFilenameDefFontLarge];
00236     [debugModeLabel setPosition:ccp(315, 61)];
00237     [self addChild:debugModeLabel];
00238 }
00239 
00240 #pragma mark -
00241 
00245 -(void)animateOutComponents
00246 {
00247     /*
00248      * show the facade again in time for the scene transition
00249      * LIKE A NINJA
00250      */
00251     //[facade setOpacity:255];
00252     
00253     [UIView animateWithDuration:0.6f 
00254         animations:^
00255         { 
00256             [lemmingCountSlider setAlpha:0.0f];
00257             [lemmingCountSlider setAlpha:0.0f];
00258             [learningEpisodesSlider setAlpha:0.0f];
00259             [learningTypeControl setAlpha:0.0f];
00260             [sharedKnowledgeSwitch setAlpha:0.0f];
00261             [debugSwitch setAlpha:0.0f]; 
00262         }
00263         completion:^(BOOL finished)
00264         {
00265             if(finished) [self removeComponents];
00266         }
00267      ];
00268 }
00269 
00273 -(void)removeComponents
00274 {
00275     NSArray * subviews = [[CCDirector sharedDirector] openGLView].subviews;
00276     for (id sv in subviews) 
00277     {
00278         [((UIView *)sv) removeFromSuperview];
00279         [sv release];
00280     }
00281 }
00282 
00283 #pragma mark -
00284 #pragma mark Event Handling
00285 
00289 - (IBAction)onSliderUpdated:(UISlider*)sender
00290 {    
00291     if(sender == lemmingCountSlider) 
00292     {
00293         lemmingCount = sender.value;
00294         [lemmingCountLabel setString:[NSString stringWithFormat:@"%i", lemmingCount]];
00295     }
00296     else if(sender == learningEpisodesSlider) 
00297     {
00298         learningEpisodes = sender.value;
00299         [learningEpisodesLabel setString:[NSString stringWithFormat:@"%i", learningEpisodes]];
00300     }
00301 }
00302 
00306 - (IBAction)onSwitchUpdated:(UISwitch*)sender
00307 {   
00308     if(sender == debugSwitch) debugMode = sender.on;  
00309     else if(sender == sharedKnowledgeSwitch) sharedKnowledge = sender.on;
00310 }
00311 
00315 - (IBAction)onSegmentedControlUpdated:(UISegmentedControl*)sender
00316 {
00317     learningType = sender.selectedSegmentIndex;
00318     
00319     BOOL enabled = (learningType == kLearningReinforcement) ? YES : NO;
00320  
00321     sharedKnowledgeSwitch.on = enabled;
00322     sharedKnowledgeSwitch.enabled = enabled;
00323     sharedKnowledge = enabled;
00324 }
00325 
00329 -(void)onContinueButtonPressed 
00330 {
00331     CCLOG(@"Initialising game: learning: %@ lemmings: %i episodes: %i shared: %@", [Utils getLearningTypeAsString:learningType], lemmingCount, learningEpisodes, [Utils getBooleanAsString:sharedKnowledge]);
00332 
00333     // first set the data
00334     [[LemmingManager sharedLemmingManager] setLearningType:learningType];
00335     [[LemmingManager sharedLemmingManager] setTotalNumberOfLemmings:lemmingCount];
00336     [[LemmingManager sharedLemmingManager] setLearningEpisodes:learningEpisodes];
00337     [[LemmingManager sharedLemmingManager] setSharedKnowledge:sharedKnowledge];
00338     [[GameManager sharedGameManager] setDebug:debugMode];
00339     
00340     // fade out the UIComponents
00341     [self animateOutComponents];
00342     
00343     // load the game scene
00344  [[GameManager sharedGameManager] runSceneWithID:kGameLevelScene];
00345 }
00346 
00350 -(void)loadMainMenu 
00351 {
00352     [self animateOutComponents];
00353     [[GameManager sharedGameManager] runSceneWithID:kMainMenuScene];
00354 }
00355 
00356 @end