Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #import "AboutScene.h"
00011 #import "GameOverScene.h"
00012 #import "GameManager.h"
00013 #import "GameScene.h"
00014 #import "InstructionsScene.h"
00015 #import "MainMenuScene.h"
00016 #import "NewGameScene.h"
00017 #import "StatsScene.h"
00018 #import "StingScene.h"
00019
00020 @implementation GameManager
00021
00022 @synthesize currentScene;
00023 @synthesize currentLevel;
00024 @synthesize gamePaused;
00025 @synthesize debug;
00026
00027 static GameManager* _instance = nil;
00028 static int secondsPlayed;
00029
00030 #pragma mark -
00031 #pragma mark Memory Allocation
00032
00033 -(void)dealloc
00034 {
00035 [levelData release];
00036 [super dealloc];
00037 }
00038
00043 +(id)alloc
00044 {
00045 @synchronized([GameManager class])
00046 {
00047
00048 NSAssert(_instance == nil, @"There should only ever be one instance of GameManager");
00049 _instance = [super alloc];
00050 return _instance;
00051 }
00052
00053 return nil;
00054 }
00055
00056 #pragma mark -
00057 #pragma mark Initialisation
00058
00063 -(id)init
00064 {
00065 self = [super init];
00066
00067 if (self != nil)
00068 {
00069
00070 [[CCDirector sharedDirector] setAnimationInterval:1.0/kFrameRate];
00071
00072 currentScene = kNoSceneUninitialised;
00073 levelData = [[CCArray alloc] init];
00074 secondsPlayed = 0;
00075 debug = NO;
00076 }
00077
00078 return self;
00079 }
00080
00081 #pragma mark -
00082
00087 +(GameManager*)sharedGameManager
00088 {
00089 @synchronized([GameManager class])
00090 {
00091 if(!_instance) [[self alloc] init];
00092 return _instance;
00093 }
00094
00095 return nil;
00096 }
00097
00098 #pragma mark -
00099 #pragma mark Level Data
00100
00104 -(void)loadLevelData
00105 {
00106
00107 NSDictionary *plistDictionary = [Utils loadPlistFromFile:kFilenameLevelData];
00108
00109 if(plistDictionary == nil) { CCLOG(@"GameManager.loadLevelData: Error loading LevelData.plist"); return; }
00110
00111 for(NSDictionary *object in plistDictionary)
00112 {
00113 NSDictionary *objectDictionary = [plistDictionary objectForKey:object];
00114
00115 NSString* name = [objectDictionary objectForKey:@"name"];
00116 int umbrellaUses = [[objectDictionary objectForKey:@"umbrellaUses"] intValue];
00117 int helmetUses = [[objectDictionary objectForKey:@"helmetUses"] intValue];
00118
00119 NSString* difficultyString = [objectDictionary objectForKey:@"difficulty"];
00120 Difficulty difficulty = kDifficultyEasy;
00121 if([difficultyString isEqualToString:@"normal"]) difficulty = kDifficultyNormal;
00122 else if([difficultyString isEqualToString:@"hard"]) difficulty = kDifficultyHard;
00123 else if(![difficultyString isEqualToString:@"easy"]) CCLOG(@"GameManager.loadLevelData: difficulty '%@' not recognised, using 'easy' as default", difficultyString);
00124
00125
00126 Level* level = [[Level alloc] init];
00127 level.name = name;
00128 level.difficulty = difficulty;
00129 level.umbrellaUses = umbrellaUses;
00130 level.helmetUses = helmetUses;
00131
00132 [levelData addObject:level];
00133 }
00134 }
00135
00139 -(void)loadRandomLevel
00140 {
00141 CCLOG(@"%@.loadRandomLevel: %@", NSStringFromClass([self class]), [Utils getDifficultyAsString:levelDifficulty]);
00142
00143
00144 CCArray* tempLevels = [CCArray arrayWithCapacity:0];
00145
00146 for (int i = 0; i < [levelData count]; i++)
00147 {
00148 Level* tempLevel = [levelData objectAtIndex:i];
00149 if(tempLevel.difficulty == levelDifficulty) [tempLevels addObject:tempLevel];
00150 }
00151
00152 if([tempLevels count] == 0) CCLOG(@"%@.loadRandomLevel: Error, no level found with difficulty: %@", NSStringFromClass([self class]), [Utils getDifficultyAsString:levelDifficulty]);
00153
00154
00155 int randomIndex = [Utils generateRandomNumberFrom:0 to:[tempLevels count]];
00156 currentLevel = [tempLevels objectAtIndex:randomIndex];
00157 }
00158
00159 #pragma mark -
00160
00165 -(void)runSceneWithID:(SceneTypes)sceneID
00166 {
00167 SceneTypes oldScene = currentScene;
00168 currentScene = sceneID;
00169 id sceneToRun = nil;
00170
00171 switch(sceneID)
00172 {
00173 case kStingScene:
00174 sceneToRun = [StingScene node];
00175 break;
00176
00177 case kMainMenuScene:
00178 sceneToRun = [MainMenuScene node];
00179 [[AgentStats sharedAgentStats] clearTempData];
00180 debug = NO;
00181 break;
00182
00183 case kNewGameScene:
00184 sceneToRun = [NewGameScene node];
00185 break;
00186
00187 case kStatsScene:
00188 sceneToRun = [StatsScene node];
00189 break;
00190
00191 case kInstructionsScene:
00192 sceneToRun = [InstructionsScene node];
00193 break;
00194
00195 case kAboutScene:
00196 sceneToRun = [AboutScene node];
00197 break;
00198
00199 case kGameOverScene:
00200 sceneToRun = [GameOverScene node];
00201 break;
00202
00203 case kGameLevelScene:
00204 sceneToRun = [GameScene node];
00205 break;
00206
00207 default:
00208 CCLOG(@"Error: Unknown scene ID");
00209 break;
00210 }
00211
00212 if(sceneToRun == nil)
00213 {
00214 currentScene = oldScene;
00215 return;
00216 }
00217
00218
00219 if([[CCDirector sharedDirector] runningScene] == nil) [[CCDirector sharedDirector] runWithScene:sceneToRun];
00220 else if(sceneID == kMainMenuScene) [[CCDirector sharedDirector] replaceScene:[CCTransitionFadeTR transitionWithDuration:0.75 scene:sceneToRun]];
00221 else [[CCDirector sharedDirector] replaceScene:[CCTransitionFadeBL transitionWithDuration:0.75 scene:sceneToRun]];
00222
00223
00224 [[KnowledgeBase sharedKnowledgeBase] clearKnowledgeBase];
00225
00226
00227 [[CCDirector sharedDirector] setDisplayFPS:debug];
00228
00229
00230 srandom(time(NULL));
00231 }
00232
00236 -(void)pauseGame
00237 {
00238 [[CCDirector sharedDirector] pause];
00239 gamePaused = YES;
00240 }
00241
00245 -(void)resumeGame
00246 {
00247 [[CCDirector sharedDirector] resume];
00248 gamePaused = NO;
00249 }
00250
00251 #pragma mark -
00252
00256 -(void)incrementSecondCounter
00257 {
00258 secondsPlayed++;
00259 }
00260
00264 -(void)resetSecondCounter
00265 {
00266 secondsPlayed = 0;
00267 }
00268
00274 -(NSString*)getGameTimeInMins
00275 {
00276 return [Utils secondsToMinutes:secondsPlayed];
00277 }
00278
00283 -(int)getGameTimeInSecs
00284 {
00285 return secondsPlayed;
00286 }
00287
00291 -(void)setLevelDifficulty:(Difficulty)_difficulty
00292 {
00293 levelDifficulty = _difficulty;
00294 }
00295
00296 @end