/Volumes/Plantain/MyDocuments/Projects/MacTrek/MacTrek/Gui/GameController.m

00001 //
00002 //  GameController.m
00003 //  MacTrek
00004 //
00005 //  Created by Aqua on 02/06/2006.
00006 //  Copyright 2006 __MyCompanyName__. 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     
00023     // or disable the subscriptions and call repaint async in the repaint loop (main event loop)
00024     /*
00025     [notificationCenter addObserver:self selector:@selector(updateDashboard:) 
00026                                name:@"SP_YOU" object:nil useLocks:NO useMainRunLoop:YES];
00027     [notificationCenter addObserver:self selector:@selector(updateDashboard:) 
00028                                name:@"SP_S_YOU" object:nil useLocks:NO useMainRunLoop:YES];
00029     [notificationCenter addObserver:self selector:@selector(updateDashboard:) 
00030                                name:@"SP_S_YOU_SS" object:nil useLocks:NO useMainRunLoop:YES];
00031     [notificationCenter addObserver:self selector:@selector(updateDashboard:) 
00032                                name:@"SP_PLAYER" object:nil useLocks:NO useMainRunLoop:YES];
00033      */
00034     universe = [Universe defaultInstance];
00035     
00036     // set up discrete bars
00037     [armiesBar setDiscrete:YES];
00038     [torpsBar setDiscrete:YES];
00039     //[phasersBar setDiscrete:YES];
00040 }
00041 
00042 - (void) repaint {
00043     // invoked by timer
00044     
00045     // repaint the dashboard here
00046     [self updateDashboard:[universe playerThatIsMe]];
00047     
00048     // and the main view
00049     //[gameView setNeedsDisplay:YES];
00050     //[mapView setNeedsDisplay:YES];
00051     // $$ see if this is faster
00052     [gameView display];
00053 }
00054 
00055 - (void) newMessage:(NSString*)message {
00056     
00057     if ([[messageTextField stringValue] isEqualToString:message]) {
00058         return; // no need to update
00059     }
00060     [messageTextField setStringValue:message];
00061 }
00062 
00063 - (void) updateBar:(LLBar*) bar andTextValue:(NSTextField*)field 
00064          withValue:(int)value max:(int)maxValue inverseWarning:(bool)inverse {
00065     [self updateBar:bar andTextValue:field withValue:value max:maxValue
00066             tempMax:maxValue inverseWarning:inverse];
00067 }
00068       
00069 - (void) updateBar:(LLBar*) bar andTextValue:(NSTextField*)field 
00070          withValue:(int)value max:(int)maxValue tempMax:(int)tempMax inverseWarning:(bool)inverse {
00071     // NOTE: as this is called from a seperate thread, we need to tell the main
00072     // thread that it needs to re-display explictly !
00073     
00074     // $$ WARNING: extremely heavy load, when using NSLevelIndicator
00075     // $$ can take up to 1sec to complete!
00076     // $$ need to create my own bar..
00077     
00078     if (bar == nil) {
00079         return;
00080     }
00081     
00082     // update only if required 
00083     if ([bar max] != maxValue) {
00084         NSLog(@"GameController.updateBar %@ setting max to %d", [bar name], maxValue);
00085         if (field != nil) {
00086             [field setStringValue:[NSString stringWithFormat:@"%d / %d", value, maxValue]];
00087         }
00088         [bar setMax:maxValue];
00089         if (inverse) {
00090             [bar setCritical:maxValue * 0.5];
00091             [bar setWarning:maxValue * 0.3];
00092         } else {
00093             [bar setCritical:maxValue * 0.3];
00094             [bar setWarning:maxValue * 0.5];
00095         }
00096         [bar setNeedsDisplay:YES];
00097     }
00098     if ([bar tempMax] != tempMax) {
00099         NSLog(@"GameController.updateBar %@ setting tempMax to %d", [bar name], tempMax);
00100         [bar setTempMax:tempMax];
00101         [bar setNeedsDisplay:YES];
00102     }    
00103     if ([bar value] != value) {
00104         //NSLog(@"GameController.updateBar %d setting value to %d", [bar tag], value);
00105         if (field != nil) {
00106             [field setStringValue:[NSString stringWithFormat:@"%d / %d", value, maxValue]];            
00107         } 
00108         [bar setValue:value]; 
00109         [bar setNeedsDisplay:YES];
00110     }         
00111 }
00112 
00113 - (void) updateDashboard:(Player*) me {
00114     
00115     if (![me isMe]) { // this is not me...
00116         return;
00117     }
00118 
00119     [self updateBar:hullBar   andTextValue:hullValue   
00120           withValue:[me hull] max:[[me ship] maxHull] inverseWarning:NO];
00121     [self updateBar:shieldBar andTextValue:shieldValue 
00122           withValue:[me shield] max:[[me ship] maxShield]
00123      inverseWarning:NO];
00124     [self updateBar:fuelBar   andTextValue:fuelValue   
00125           withValue:[me fuel] max:[[me ship] maxFuel] inverseWarning:NO];
00126     [self updateBar:eTempBar  andTextValue:eTempValue  
00127           withValue:[me engineTemp] / 10 max:[[me ship] maxEngineTemp] / 10
00128           inverseWarning:YES];
00129     [self updateBar:wTempBar  andTextValue:wTempValue  
00130           withValue:[me weaponTemp] / 10 max:[[me ship] maxWeaponTemp] / 10  
00131           inverseWarning:YES];
00132     [self updateBar:speedBar  andTextValue:speedValue  
00133           withValue:[me speed] max:[[me ship] maxSpeed]  inverseWarning:YES];
00134     // special max armies is depended on the players kills
00135     [self updateBar:armiesBar andTextValue:nil    
00136           withValue:[me armies] max:[[me ship] maxArmies] tempMax:[me maxArmies] inverseWarning:NO];
00137     [self updateBar:torpsBar andTextValue:nil     
00138           withValue:[me availableTorps] max:[me maxTorps]
00139           inverseWarning:NO];
00140     [self updateBar:phasersBar andTextValue:nil   
00141           withValue:[me availablePhaserShots] max:[me maxPhaserShots] 
00142           inverseWarning:NO];
00143 }
00144 
00145 - (void) setKeyMap:(MTKeyMap *)newKeyMap {
00146     // pass it on
00147     [gameView setKeyMap:newKeyMap];
00148 }
00149 
00150 - (void) startGame {
00151     // set focus on main view
00152     // $$ do not know how to make a view key without typing in it.
00153     //[gameView makeKeyWindow];
00154     [gameView dummyMouseAction];
00155 }
00156 
00157 @end

Generated on Fri Jul 28 19:15:21 2006 for MacTrek by  doxygen 1.4.7