00001
00002
00003
00004
00005
00006
00007
00008
00009 #import "GuiManager.h"
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 @implementation GuiManager
00034
00035 bool clientRuns = NO;
00036 MetaServerEntry *currentServer = nil;
00037 Universe *universe = nil;
00038 int startUpEvents = 0;
00039
00040
00041
00042 bool multiThreaded = YES;
00043
00044 - (id) init {
00045 self = [super init];
00046 if (self != nil) {
00047
00048 universe = [Universe defaultInstance];
00049
00050
00051 client = [[ClientController alloc] initWithUniverse:universe];
00052
00053
00054 soundPlayerTheme1 = [[SoundPlayerForNetrek alloc] init];
00055 painterTheme1 = [[PainterFactoryForNetrek alloc] init];
00056 soundPlayerTheme2 = [[SoundPlayerForMacTrek alloc] init];
00057 painterTheme2 = [[PainterFactoryForMacTrek alloc] init];
00058 soundPlayerTheme3 = [[SoundPlayerForTac alloc] init];
00059 painterTheme3 = [[PainterFactoryForTac alloc] init];
00060
00061 soundPlayerActiveTheme = soundPlayerTheme1;
00062 painterActiveTheme = painterTheme1;
00063 activeTheme = -1;
00064
00065
00066 gameState = GS_NO_SERVER_SELECTED;
00067
00068
00069 [notificationCenter addObserver:self selector:@selector(serverSelected:)
00070 name:@"MS_SERVER_SELECTED" object:nil];
00071 [notificationCenter addObserver:self selector:@selector(serverSlotFound)
00072 name:@"CC_SLOT_FOUND" object:nil];
00073 [notificationCenter addObserver:self selector:@selector(serverDeSelected)
00074 name:@"LC_INVALID_SERVER" object:nil];
00075 [notificationCenter addObserver:self selector:@selector(loginComplete)
00076 name:@"LC_LOGIN_COMPLETE" object:nil];
00077 [notificationCenter addObserver:self selector:@selector(outfitAccepted)
00078 name:@"SP_PICKOK" object:nil];
00079 [notificationCenter addObserver:self selector:@selector(loginComplete)
00080 name:@"SP_PICKNOK" object:nil];
00081
00082 [notificationCenter addObserver:self selector:@selector(iDied)
00083 name:@"CC_GO_OUTFIT" object:nil];
00084 [notificationCenter addObserver:self selector:@selector(commError)
00085 name:@"COMM_TCP_WRITE_ERROR" object:nil];
00086
00087 [notificationCenter addObserver:self selector:@selector(handleTeamMask:)
00088 name:@"SP_MASK" object:nil];
00089
00090
00091
00092
00093 [notificationCenter addObserver:self selector:@selector(increaseStartUpCounter)
00094 name:@"PF_IMAGES_CACHED"];
00095 [notificationCenter addObserver:self selector:@selector(increaseStartUpCounter)
00096 name:@"SP_SOUNDS_CACHED"];
00097 }
00098 return self;
00099 }
00100
00101
00102
00103 - (void) playIntroSoundEffect {
00104 [soundPlayerActiveTheme playSoundEffect:@"INTRO_SOUND"];
00105 }
00106
00107 - (void) increaseStartUpCounter {
00108
00109 startUpEvents++;
00110 [startUpProgress setIntValue:startUpEvents];
00111 [startUpProgress setNeedsDisplay:YES];
00112 NSLog(@"GuiManager.increaseStartUpCounter got event %d of %d", startUpEvents, NR_OF_EVENTS_BEFORE_SHOWING_MENU);
00113
00114 if (startUpEvents == NR_OF_EVENTS_BEFORE_SHOWING_MENU) {
00115
00116
00117
00118 [self playIntroSoundEffect];
00119
00120
00121 [menuButton setHidden:NO];
00122 [menuButton setEnabled:YES];
00123 [menuButton setNeedsDisplay:YES];
00124
00125 [menuCntrl performSelector:@selector(leaveSplashScreen) withObject:nil afterDelay: 10];
00126 [splashView performSelector:@selector(stop:) withObject:self afterDelay: 10];
00127
00128 } else if (startUpEvents > NR_OF_EVENTS_BEFORE_SHOWING_MENU) {
00129
00130 NSLog(@"GuiManager.increaseStartUpCounter did not expect this event... %d", startUpEvents);
00131 [menuCntrl raiseMenu:self];
00132 }
00133 }
00134
00135 - (void) awakeFromNib {
00136
00137 [startUpProgress setMaxValue:NR_OF_EVENTS_BEFORE_SHOWING_MENU];
00138
00139
00140
00141
00142 [NSTimer scheduledTimerWithTimeInterval: (1 / FRAME_RATE)
00143 target:self selector:@selector(screenRefreshTimerFired:)
00144 userInfo:nil
00145 repeats:YES];
00146
00147 [loginCntrl setMultiThreaded:multiThreaded];
00148 }
00149
00150 - (void)handleTeamMask:(NSNumber *) mask {
00151
00152
00153 if ((gameState == GS_LOGIN_ACCEPTED) && (!multiThreaded)) {
00154
00155 NSLog(@"GuiManager.handleTeamMask firing up a read");
00156 [client performSelector: @selector(singleReadFromServer)
00157 withObject: self
00158 afterDelay: 1];
00159 }
00160 }
00161
00162 - (void)screenRefreshTimerFired:(NSTimer*)theTimer {
00163
00164
00165
00166
00167
00168 if (gameState == GS_GAME_ACTIVE) {
00169
00170 if (!multiThreaded) {
00171
00172 [client singleReadFromServer];
00173 }
00174
00175
00176 [gameCntrl repaint];
00177 } else {
00178
00179 }
00180
00181
00182
00183 }
00184
00185 - (void)serverSelected:(MetaServerEntry *) selectedServer {
00186
00187 switch (gameState) {
00188 case GS_NO_SERVER_SELECTED:
00189 case GS_SERVER_SELECTED:
00190
00191
00192 if (selectedServer == nil) {
00193 NSLog(@"GuiManager.serverSelected forwarding to deselect");
00194 [self serverDeSelected];
00195 return;
00196 }
00197
00198
00199 gameState = GS_SERVER_SELECTED;
00200 currentServer = selectedServer;
00201
00202
00203 [client stop];
00204
00205 if ([client startClientAt:[selectedServer address]
00206 port:[selectedServer port]
00207 seperate:multiThreaded]) {
00208
00209 NSLog(@"GuiManager.serverSelected connect to server successfull");
00210
00211 if (multiThreaded) {
00212 [self serverConnected];
00213 }
00214
00215 } else {
00216
00217 NSLog(@"GuiManager.serverSelected cannot connect to server!");
00218
00219 [self serverDeSelected];
00220
00221 [selectServerCntrl invalidServer];
00222 }
00223 break;
00224 case GS_SERVER_CONNECTED:
00225 case GS_SERVER_SLOT_FOUND:
00226 case GS_LOGIN_ACCEPTED:
00227
00228 if (currentServer != selectedServer) {
00229
00230 if (currentServer != nil) {
00231
00232 [self serverDeSelected];
00233 }
00234
00235 [self serverSelected:selectedServer];
00236 currentServer = selectedServer;
00237 }
00238 break;
00239 case GS_OUTFIT_ACCEPTED:
00240 case GS_GAME_ACTIVE:
00241 NSLog(@"GuiManager.serverSelected unexpected gameState %d, reseting", gameState);
00242 [self serverDeSelected];
00243 [menuCntrl raiseMenu:self];
00244 break;
00245 default:
00246 NSLog(@"GuiManager.serverSelected unknown gameState %d", gameState);
00247 break;
00248 }
00249
00250 NSLog(@"GuiManager.serverSelected GAMESTATE = %d", gameState);
00251 }
00252
00253 - (void) serverDeSelected {
00254
00255 gameState = GS_NO_SERVER_SELECTED;
00256
00257 [client stop];
00258
00259
00260
00261
00262 [menuCntrl disableLogin];
00263 [localServerCntrl disableLogin];
00264 [selectServerCntrl disableLogin];
00265
00266 [loginCntrl disablePlayerName];
00267 [loginCntrl reset];
00268 currentServer = nil;
00269
00270
00271 [selectServerCntrl deselectServer:self];
00272
00273 NSLog(@"GuiManager.serverDeSelected GAMESTATE = %d", gameState);
00274 }
00275
00276 - (void) serverConnected {
00277
00278 switch (gameState) {
00279
00280 case GS_SERVER_SELECTED:
00281
00282
00283 gameState = GS_SERVER_CONNECTED;
00284 break;
00285 case GS_SERVER_SLOT_FOUND:
00286
00287 break;
00288 case GS_SERVER_CONNECTED:
00289 case GS_OUTFIT_ACCEPTED:
00290 case GS_GAME_ACTIVE:
00291 case GS_LOGIN_ACCEPTED:
00292 case GS_NO_SERVER_SELECTED:
00293 NSLog(@"GuiManager.serverConnected unexpected gameState %d, reseting", gameState);
00294 [self serverDeSelected];
00295 [menuCntrl raiseMenu:self];
00296 break;
00297 default:
00298 NSLog(@"GuiManager.serverConnected unknown gameState %d", gameState);
00299 break;
00300 }
00301
00302
00303 NSLog(@"GuiManager.serverConnected GAMESTATE = %d", gameState);
00304 }
00305
00306 - (void) serverSlotFound {
00307 switch (gameState) {
00308 case GS_NO_SERVER_SELECTED:
00309
00310 case GS_OUTFIT_ACCEPTED:
00311 NSLog(@"GuiManager.serverSlotFound unexpected gameState %d, reseting", gameState);
00312 [self serverDeSelected];
00313 [menuCntrl raiseMenu:self];
00314 break;
00315 case GS_SERVER_CONNECTED:
00316 case GS_SERVER_SELECTED:
00317 case GS_GAME_ACTIVE:
00318 gameState = GS_SERVER_SLOT_FOUND;
00319
00320 [menuCntrl enableLogin];
00321 [localServerCntrl enableLogin];
00322 [selectServerCntrl enableLogin];
00323 [loginCntrl enablePlayerName];
00324 [loginCntrl startClock];
00325
00326
00327
00328
00329
00330
00331 if (![client sendSlotSettingsToServer]) {
00332 NSLog(@"GuiManager.serverSlotFound cannot send slot settings to server!");
00333 [self serverDeSelected];
00334 }
00335 break;
00336 default:
00337 NSLog(@"GuiManager.serverSlotFound unknown gameState %d", gameState);
00338 break;
00339 }
00340
00341 NSLog(@"GuiManager.serverSlotFound GAMESTATE = %d", gameState);
00342 }
00343
00344 - (void) iDied {
00345
00346 if ([NSCursor currentCursor] == [NSCursor crosshairCursor]) {
00347 [[NSCursor crosshairCursor] pop];
00348 }
00349 [self loginComplete];
00350 }
00351
00352 - (void) loginComplete {
00353 switch (gameState) {
00354 case GS_NO_SERVER_SELECTED:
00355 case GS_SERVER_SELECTED:
00356 case GS_SERVER_CONNECTED:
00357 case GS_OUTFIT_ACCEPTED:
00358 NSLog(@"GuiManager.loginComplete unexpected gameState %d, reseting", gameState);
00359 [self serverDeSelected];
00360 [menuCntrl raiseMenu:self];
00361 break;
00362 case GS_LOGIN_ACCEPTED:
00363 NSLog(@"GuiManager.loginComplete login was not accepted, try again");
00364
00365 break;
00366 case GS_SERVER_SLOT_FOUND:
00367 gameState = GS_LOGIN_ACCEPTED;
00368
00369 [menuCntrl disableLogin];
00370 [localServerCntrl disableLogin];
00371 [selectServerCntrl disableLogin];
00372
00373 [loginCntrl disablePlayerName];
00374
00375
00376 [menuCntrl raiseOutfit:self];
00377
00378 [outfitCntrl setInstructionFieldToDefault];
00379
00380
00381 [notificationCenter addObserver:outfitCntrl selector:@selector(setInstructionField:) name:@"SP_WARNING"
00382 object:nil useLocks:NO useMainRunLoop:YES];
00383
00384 break;
00385 case GS_GAME_ACTIVE:
00386
00387 [outfitCntrl setInstructionFieldToDefault];
00388
00389
00390 [notificationCenter addObserver:outfitCntrl selector:@selector(setInstructionField:) name:@"SP_WARNING"
00391 object:nil useLocks:NO useMainRunLoop:YES];
00392 gameState = GS_LOGIN_ACCEPTED;
00393
00394 [menuCntrl raiseOutfit:self];
00395
00396 break;
00397 default:
00398 NSLog(@"GuiManager.loginComplete unknown gameState %d", gameState);
00399 break;
00400 }
00401 NSLog(@"GuiManager.loginComplete GAMESTATE = %d", gameState);
00402 }
00403
00404 - (void) outfitAccepted {
00405 switch (gameState) {
00406 case GS_NO_SERVER_SELECTED:
00407 case GS_SERVER_SELECTED:
00408 case GS_SERVER_CONNECTED:
00409 case GS_OUTFIT_ACCEPTED:
00410 case GS_SERVER_SLOT_FOUND:
00411 case GS_GAME_ACTIVE:
00412 NSLog(@"GuiManager.outfitAccepted unexpected gameState %d, reseting", gameState);
00413 [self serverDeSelected];
00414 [menuCntrl raiseMenu:self];
00415 break;
00416 case GS_LOGIN_ACCEPTED:
00417 gameState = GS_OUTFIT_ACCEPTED;
00418
00419 [notificationCenter removeObserver:outfitCntrl name:@"SP_WARNING"];
00420 [outfitCntrl setInstructionFieldToDefault];
00421
00422 [self gameEntered];
00423
00424 break;
00425 default:
00426 NSLog(@"GuiManager.outfitAccepted unknown gameState %d", gameState);
00427 break;
00428 }
00429 NSLog(@"GuiManager.outfitAccepted GAMESTATE = %d", gameState);
00430 }
00431
00432 - (void) gameEntered {
00433
00434 switch (gameState) {
00435 case GS_NO_SERVER_SELECTED:
00436 case GS_SERVER_SELECTED:
00437 case GS_SERVER_CONNECTED:
00438 case GS_LOGIN_ACCEPTED:
00439 case GS_SERVER_SLOT_FOUND:
00440 case GS_GAME_ACTIVE:
00441 NSLog(@"GuiManager.gameEntered unexpected gameState %d, reseting", gameState);
00442 [self serverDeSelected];
00443 [menuCntrl raiseMenu:self];
00444 break;
00445 case GS_OUTFIT_ACCEPTED:
00446
00447 [self setTheme];
00448
00449 [[settingsCntrl keyMap] writeToDefaultFileIfChanged];
00450
00451 [gameCntrl setKeyMap:[settingsCntrl keyMap]];
00452
00453 [soundPlayerActiveTheme setVolumeFx:[settingsCntrl fxLevel]];
00454 [soundPlayerActiveTheme setVolumeMusic:[settingsCntrl musicLevel]];
00455
00456 [menuCntrl raiseGame:self];
00457 [gameCntrl startGame];
00458 gameState = GS_GAME_ACTIVE;
00459
00460 [soundPlayerActiveTheme playSoundEffect:@"ENTER_SHIP_SOUND"];
00461 break;
00462 default:
00463 NSLog(@"GuiManager.gameEntered unknown gameState %d", gameState);
00464 break;
00465 }
00466 NSLog(@"GuiManager.gameEntered GAMESTATE = %d", gameState);
00467 }
00468
00469 - (void) commError {
00470 switch (gameState) {
00471 case GS_NO_SERVER_SELECTED:
00472 case GS_SERVER_SELECTED:
00473 break;
00474 case GS_SERVER_CONNECTED:
00475 case GS_LOGIN_ACCEPTED:
00476 case GS_SERVER_SLOT_FOUND:
00477 case GS_GAME_ACTIVE:
00478 case GS_OUTFIT_ACCEPTED:
00479 NSLog(@"GuiManager.commError unexpected (gameState %d), reseting", gameState);
00480 [self serverDeSelected];
00481 [menuCntrl raiseMenu:self];
00482 break;
00483 default:
00484 NSLog(@"GuiManager.gameEntered unknown gameState %d", gameState);
00485 break;
00486 }
00487 NSLog(@"GuiManager.commError GAMESTATE = %d", gameState);
00488 }
00489
00490 - (void) setTheme {
00491
00492
00493
00494 int theme = [settingsCntrl graphicsModel] + 1;
00495
00496 if (theme != activeTheme) {
00497 NSLog(@"GuiManager.setTheme to theme %d", theme);
00498 switch (theme) {
00499 case 1:
00500 [gameCntrl setPainter:painterTheme1];
00501 [gameCntrl setSpeakComputerMessages:NO];
00502 painterActiveTheme = painterTheme1;
00503 [soundPlayerActiveTheme unSubscibeToNotifications];
00504 soundPlayerActiveTheme = soundPlayerTheme1;
00505 [soundPlayerActiveTheme subscribeToNotifications];
00506 break;
00507 case 2:
00508 [gameCntrl setPainter:painterTheme2];
00509 [gameCntrl setSpeakComputerMessages:YES];
00510 painterActiveTheme = painterTheme2;
00511 [soundPlayerActiveTheme unSubscibeToNotifications];
00512 soundPlayerActiveTheme = soundPlayerTheme2;
00513 [soundPlayerActiveTheme subscribeToNotifications];
00514 break;
00515 case 3:
00516 [gameCntrl setPainter:painterTheme3];
00517 [gameCntrl setSpeakComputerMessages:NO];
00518 painterActiveTheme = painterTheme3;
00519 [soundPlayerActiveTheme unSubscibeToNotifications];
00520 soundPlayerActiveTheme = soundPlayerTheme3;
00521 [soundPlayerActiveTheme subscribeToNotifications];
00522 break;
00523 default:
00524 NSLog(@"GuiManager.setTheme ERROR, do not know of theme %d", theme);
00525 break;
00526 }
00527 }
00528
00529 if ([settingsCntrl fxLevel] == 0.0) {
00530
00531 [soundPlayerActiveTheme unSubscibeToNotifications];
00532 NSLog(@"GuiManager.setTheme silencing SOUND");
00533 }
00534 }
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544 @end