Gui/GameController.m

00001 //
00002 //  GameController.m
00003 //  MacTrek
00004 //
00005 //  Created by Aqua on 02/06/2006.
00006 //  Copyright 2006 Luky Soft. All rights reserved.
00007 //
00008 
00009 #import "GameController.h"
00010 
00011 
00012 @implementation GameController
00013 
00014 - (void) awakeFromNib {
00015       
00016     // watch my shields and stuff
00017     // but beware that the events are received in the main thread
00018     
00019     // when we receive status messages, we update the textfield
00020     [notificationCenter addObserver:self selector:@selector(newMessage:) name:@"SP_WARNING"
00021                              object:nil useLocks:NO useMainRunLoop:YES];
00022     [notificationCenter addObserver:self selector:@selector(newMessage:) name:@"PM_WARNING"
00023                              object:nil useLocks:NO useMainRunLoop:NO]; // is fired in main loop
00024     [notificationCenter addObserver:self selector:@selector(newInfoMessage:) name:@"GV_MODE_INFO"
00025                              object:nil useLocks:NO useMainRunLoop:NO]; // is fired in main loop
00026     universe = [Universe defaultInstance];
00027     
00028     // set up discrete bars
00029     [armiesBar setDiscrete:YES];
00030     [torpsBar setDiscrete:YES];
00031     //[phasersBar setDiscrete:YES];
00032     
00033     // add speech
00034     //synth = [[NSSpeechSynthesizer alloc] initWithVoice:@"com.apple.speech.synthesis.voice.Zarvox"];
00035     synth = [[NSSpeechSynthesizer alloc] initWithVoice:@"com.apple.speech.synthesis.voice.Trinoids"]; 
00036     shouldSpeak = YES;
00037     
00038     //synth is an ivar
00039     
00040     [synth setDelegate:self];
00041 }
00042 
00043 - (void) setSpeakComputerMessages:(bool)speak {
00044     shouldSpeak = speak;
00045 }
00046 
00047 - (void) repaint {
00048     // invoked by timer
00049     
00050     // repaint the dashboard here
00051     [self updateDashboard:[universe playerThatIsMe]];
00052     
00053     // do the messages list if an update occured
00054     if ([messages hasChanged]) {
00055         //NSLog(@"GameController.repaint repainting messages view");
00056         [messages setNeedsDisplay:YES];
00057     }
00058     // do the playerList list if an update occured
00059     if ([playerList hasChanged]) {
00060         NSLog(@"GameController.repaint repainting playerList view");
00061         [playerList setNeedsDisplay:YES];
00062     }
00063     
00064     // and the main view    
00065     [mapView setNeedsDisplay:YES];
00066     // $$ see if this is faster
00067     [gameView setNeedsDisplay:YES];
00068     //[gameView display];
00069 }
00070 
00071 - (void) newInfoMessage:(NSString*)message {
00072     
00073     if ([[messageTextField stringValue] isEqualToString:message]) {
00074         return; // no need to update
00075     }
00076     [messageTextField setStringValue:message];
00077 }
00078 
00079 - (void) newMessage:(NSString*)message {
00080 
00081     [self newInfoMessage:message];
00082     
00083     // add speech...
00084     if (shouldSpeak && (![synth isSpeaking])) {
00085         [synth startSpeakingString:message];
00086     }
00087     
00088 }
00089 
00090 - (void) updateBar:(LLBar*) bar andTextValue:(NSTextField*)field 
00091          withValue:(int)value max:(int)maxValue inverseWarning:(bool)inverse {
00092     [self updateBar:bar andTextValue:field withValue:value max:maxValue
00093             tempMax:maxValue inverseWarning:inverse];
00094 }
00095       
00096 - (void) updateBar:(LLBar*) bar andTextValue:(NSTextField*)field 
00097          withValue:(int)value max:(int)maxValue tempMax:(int)tempMax inverseWarning:(bool)inverse {
00098     // NOTE: as this is called from a seperate thread, we need to tell the main
00099     // thread that it needs to re-display explictly !
00100     
00101     if (bar == nil) {
00102         return;
00103     }
00104     
00105     // update only if required 
00106     if ([bar max] != maxValue) {
00107         NSLog(@"GameController.updateBar %@ setting max to %d", [bar name], maxValue);
00108         if (field != nil) { // overrules max
00109             [field setStringValue:[NSString stringWithFormat:@"%d / %d", value, tempMax]];
00110         }
00111         [bar setMax:maxValue * 1.0];
00112         if (inverse) {
00113             [bar setCritical:maxValue * 0.5];
00114             [bar setWarning:maxValue * 0.3];
00115         } else {
00116             [bar setCritical:maxValue * 0.3];
00117             [bar setWarning:maxValue * 0.5];
00118         }
00119         [bar setNeedsDisplay:YES];
00120     }
00121     if ([bar tempMax] != tempMax) {
00122         NSLog(@"GameController.updateBar %@ setting tempMax to %d", [bar name], tempMax);
00123         [bar setTempMax:tempMax * 1.0];
00124         [bar setNeedsDisplay:YES];
00125         if (field != nil) { // overrules max
00126             [field setStringValue:[NSString stringWithFormat:@"%d / %d", value, tempMax]];
00127         }
00128     }    
00129     if ([bar value] != value) {
00130         //NSLog(@"GameController.updateBar %d setting value to %d", [bar tag], value);
00131         if (field != nil) {              
00132             [field setStringValue:[NSString stringWithFormat:@"%d / %d", value, tempMax]];                            
00133         } 
00134         [bar setValue:value * 1.0]; 
00135         [bar setNeedsDisplay:YES];
00136     }         
00137 }
00138 
00139 - (void) updateDashboard:(Player*) me {
00140     
00141     if (![me isMe]) { // this is not me...
00142         return;
00143     }
00144 
00145     [self updateBar:hullBar   andTextValue:hullValue   
00146           withValue:[me hull] max:[[me ship] maxHull] inverseWarning:NO];
00147     [self updateBar:shieldBar andTextValue:shieldValue 
00148           withValue:[me shield] max:[[me ship] maxShield]
00149      inverseWarning:NO];
00150     [self updateBar:fuelBar   andTextValue:fuelValue   
00151           withValue:[me fuel] max:[[me ship] maxFuel] inverseWarning:NO];
00152     [self updateBar:eTempBar  andTextValue:eTempValue  
00153           withValue:[me engineTemp] / 10 max:[[me ship] maxEngineTemp] / 10
00154           inverseWarning:YES];
00155     [self updateBar:wTempBar  andTextValue:wTempValue  
00156           withValue:[me weaponTemp] / 10 max:[[me ship] maxWeaponTemp] / 10  
00157           inverseWarning:YES];
00158     [self updateBar:speedBar  andTextValue:speedValue  
00159           withValue:[me speed] max:[[me ship] maxSpeed] tempMax:[me maxSpeed] inverseWarning:YES];
00160     // special max armies is depended on the players kills
00161     [self updateBar:armiesBar andTextValue:nil    
00162           withValue:[me armies] max:[[me ship] maxArmies] tempMax:[me maxArmies] inverseWarning:NO];
00163     [self updateBar:torpsBar andTextValue:nil     
00164           withValue:[me availableTorps] max:[me maxTorps]
00165           inverseWarning:NO];
00166     [self updateBar:phasersBar andTextValue:nil   
00167           withValue:[me availablePhaserShots] max:[me maxPhaserShots] 
00168           inverseWarning:NO];
00169 }
00170 
00171 - (void) setKeyMap:(MTKeyMap *)newKeyMap {
00172     // pass it on
00173     [gameView setKeyMap:newKeyMap];
00174 }
00175 
00176 - (void) setPainter:(PainterFactory*)newPainter {
00177     [gameView setPainter:newPainter];
00178 }
00179 
00180 - (void) startGame {
00181     // set focus on main view
00182     // the view now reacts to mouse movements
00183     //[gameView dummyMouseAction];
00184 }
00185 
00186 @end

Generated on Sat Aug 26 21:14:15 2006 for MacTrek by  doxygen 1.4.7