Painter/PainterFactoryForNetrek.m

00001 //
00002 //  PainterFactoryForNetrek.m
00003 //  MacTrek
00004 //
00005 //  Created by Aqua on 20/06/2006.
00006 //  Copyright 2006 Luky Soft. All rights reserved.
00007 //
00008 
00009 #import "PainterFactoryForNetrek.h"
00010 
00011 
00012 @implementation PainterFactoryForNetrek
00013 
00014 - (id) init {
00015     self = [super init];
00016     if (self != nil) {
00017         filter = [[LLColorFilter alloc] init];
00018     }
00019     return self;
00020 }
00021 
00022 
00023 - (NSImage *)loadImage:(NSString*)imageName {
00024     
00025     // load from disk
00026     NSString *pathToResources = [[NSBundle mainBundle] resourcePath];
00027     NSString *pathToImage = [NSString stringWithFormat:@"%@/ImagesPainter1%@", pathToResources, imageName];
00028     
00029     NSImage *rawImage = [[NSImage alloc] initWithContentsOfFile:pathToImage];
00030     
00031     // make black background transparent
00032     
00033     NSImage *filterdImage = nil;
00034     /*
00035     filterdImage = [filter replaceColor:[NSColor blackColor] 
00036                                        withColor:[NSColor clearColor] 
00037                                          inImage:rawImage 
00038                                       ignoreAlha:YES]; */
00039     if (filterdImage == nil) {
00040         // something went wrong take, raw input
00041         return rawImage;
00042     }
00043     
00044     [rawImage release];
00045     
00046     return filterdImage;
00047 }
00048 
00049 - (void) cacheImages {
00050     
00051     // load them
00052     imgExplodeStarBase = [self loadImage:@"/Misc/starbaseExplode.png"]; // use transparent version 
00053     imgShipCloak       = [self loadImage:@"/Misc/cloak.bmp"]; // not yet used
00054     imgExplodePlayer   = [self loadImage:@"/Misc/shipExplode.png"]; // use transparent version 
00055     imgShipsOri        = [self loadImage:@"/Ships/oriship.png"]; // use transparent version 
00056     imgShipsFed        = [self loadImage:@"/Ships/fedship.png"]; // use transparent version 
00057     imgShipsKli        = [self loadImage:@"/Ships/kliship.png"]; // use transparent version 
00058     imgShipsRom        = [self loadImage:@"/Ships/romship.png"]; // use transparent version 
00059     imgShipsInd        = [self loadImage:@"/Ships/indship.png"]; // use transparent version 
00060     imgExplodeTorp     = [self loadImage:@"/Weapons/torpExplode.png"]; // use transparent version 
00061     imgPlasma          = [self loadImage:@"/Weapons/plasma.png"]; // use transparent version 
00062     imgTorp            = [self loadImage:@"/Weapons/torp.png"]; // use transparent version 
00063     imgExplodePlasma   = [self loadImage:@"/Weapons/plasmaExplode.png"]; // use transparent version 
00064     imgFuel            = [self loadImage:@"/Planets/fuel.bmp"]; 
00065     imgPlanetRock1     = [self loadImage:@"/Planets/rock1.bmp"]; 
00066     imgPlanetRock2     = [self loadImage:@"/Planets/rock2.bmp"];
00067     imgPlanetHomeOri   = [self loadImage:@"/Planets/orion.bmp"]; 
00068     imgPlanetHomeKli   = [self loadImage:@"/Planets/klingus.bmp"]; 
00069     imgPlanetHomeRom   = [self loadImage:@"/Planets/romulus.bmp"]; 
00070     imgPlanetUnknown   = [self loadImage:@"/Planets/unknown.bmp"];
00071     imgArmy            = [self loadImage:@"/Planets/army.bmp"]; 
00072     imgPLanetHomeFed   = [self loadImage:@"/Planets/earth.bmp"]; 
00073     imgPlanetAgri1     = [self loadImage:@"/Planets/agri1.bmp"]; 
00074     imgPlanetAgri2     = [self loadImage:@"/Planets/agri2.bmp"]; 
00075     imgRepair          = [self loadImage:@"/Planets/wrench.bmp"]; 
00076     
00077     // we are totally flipped
00078     [imgExplodeStarBase setFlipped:YES];
00079     [imgShipCloak setFlipped:YES];
00080     [imgExplodePlayer setFlipped:YES];
00081     [imgShipsOri setFlipped:YES];
00082     [imgShipsFed setFlipped:YES];
00083     [imgShipsKli setFlipped:YES];
00084     [imgShipsRom setFlipped:YES];
00085     [imgShipsInd setFlipped:YES];
00086     [imgExplodeTorp setFlipped:YES];
00087     [imgPlasma setFlipped:YES];
00088     [imgTorp setFlipped:YES];
00089     [imgExplodePlasma setFlipped:YES];
00090     [imgFuel setFlipped:YES];
00091     [imgPlanetRock1 setFlipped:YES];
00092     [imgPlanetRock2 setFlipped:YES];
00093     [imgPlanetHomeOri setFlipped:YES];
00094     [imgPlanetHomeKli setFlipped:YES];
00095     [imgPlanetHomeRom setFlipped:YES];
00096     [imgPlanetUnknown setFlipped:YES];
00097     [imgArmy setFlipped:YES];
00098     [imgPLanetHomeFed setFlipped:YES];
00099     [imgPlanetAgri1 setFlipped:YES];
00100     [imgPlanetAgri2 setFlipped:YES];
00101     [imgRepair setFlipped:YES];
00102     
00103     // force the images to be loaded
00104     [imgExplodeStarBase lockFocus];
00105     [imgExplodeStarBase unlockFocus];
00106     [imgShipCloak lockFocus];
00107     [imgShipCloak unlockFocus];
00108     [imgExplodePlayer lockFocus];
00109     [imgExplodePlayer unlockFocus];
00110     [imgShipsOri lockFocus];
00111     [imgShipsOri unlockFocus];
00112     [imgShipsFed lockFocus];
00113     [imgShipsFed unlockFocus];
00114     [imgShipsKli lockFocus];
00115     [imgShipsKli unlockFocus];
00116     [imgShipsRom lockFocus];
00117     [imgShipsRom unlockFocus];
00118     [imgShipsInd lockFocus];
00119     [imgShipsInd unlockFocus];
00120     [imgExplodeTorp lockFocus];
00121     [imgExplodeTorp unlockFocus];
00122     [imgPlasma lockFocus];
00123     [imgPlasma unlockFocus];
00124     [imgTorp lockFocus];
00125     [imgTorp unlockFocus];
00126     [imgExplodePlasma lockFocus];
00127     [imgExplodePlasma unlockFocus];
00128     [imgFuel lockFocus];
00129     [imgFuel unlockFocus];
00130     [imgPlanetRock1 lockFocus];
00131     [imgPlanetRock1 unlockFocus];
00132     [imgPlanetRock2 lockFocus];
00133     [imgPlanetRock2 unlockFocus];
00134     [imgPlanetHomeOri lockFocus];
00135     [imgPlanetHomeOri unlockFocus];
00136     [imgPlanetHomeKli lockFocus];
00137     [imgPlanetHomeKli unlockFocus];
00138     [imgPlanetHomeRom lockFocus];
00139     [imgPlanetHomeRom unlockFocus];
00140     [imgPlanetUnknown lockFocus];
00141     [imgPlanetUnknown unlockFocus];
00142     [imgArmy lockFocus];
00143     [imgArmy unlockFocus];
00144     [imgPLanetHomeFed lockFocus];
00145     [imgPLanetHomeFed unlockFocus];
00146     [imgPlanetAgri1 lockFocus];
00147     [imgPlanetAgri1 unlockFocus];
00148     [imgPlanetAgri2 lockFocus];
00149     [imgPlanetAgri2 unlockFocus];
00150     [imgRepair lockFocus];
00151     [imgRepair unlockFocus];
00152 
00153 }
00154 
00155 - (int)    maxFuseForMovingPlayer {
00156     return 1;
00157 }
00158 
00159 - (int)    maxFuseForExplodingPlayer {
00160     int frameDim = [imgExplodePlayer size].width; 
00161     
00162     return PFN_SLOW_MOTION_FACTOR_EXPLODE * [imgExplodePlayer size].height / frameDim;
00163 } 
00164 
00165 - (void)   drawPlayer:(Player*) player inRect:(NSRect) Rect {
00166       
00167     // special
00168     if ([player status] == PLAYER_EXPLODE) {
00169         
00170         NSImage *imgShips;
00171         if ([[player ship] type] == SHIP_SB) {
00172             imgShips = imgExplodeStarBase;
00173         } else {
00174             imgShips = imgExplodePlayer;
00175         }
00176         
00177         // frames are stored horz
00178         NSRect shipRect = NSMakeRect( 0, 0, [imgShips size].width, [imgShips size].width);
00179         // move to the proper frame
00180         shipRect.origin.y += ([player fuse] / PFN_SLOW_MOTION_FACTOR_EXPLODE) * [imgShips size].width ;
00181         // draw
00182         [imgShips drawInRect:Rect fromRect:shipRect operation: NSCompositeSourceOver fraction:1.0];   
00183         return;
00184     }
00185     
00186     // first get the banner of ship images
00187     NSImage *imgShips;
00188     switch ([[player team] teamId]) {
00189     case TEAM_FED:
00190         imgShips = imgShipsFed;
00191         break;
00192     case TEAM_KLI:
00193         imgShips = imgShipsKli;
00194         break;
00195     case TEAM_ROM:
00196         imgShips = imgShipsRom;
00197         break;
00198     case TEAM_ORI:
00199         imgShips = imgShipsOri;
00200         break;
00201     default:
00202         imgShips = imgShipsInd;
00203         break;
00204     }
00205     
00206     // get the rigth image 
00207     // assume the ships are in squares and heigth = width
00208     NSRect shipRect = NSMakeRect( 0, 0, [imgShips size].height ,[imgShips size].height); 
00209     // move the rect to the proper ship e.g a dd is type 1 and moves the rect 1 width to the right
00210     shipRect.origin.x += ([[player ship] type] * shipRect.size.width);
00211     
00212     // calculate the alpha for cloak
00213     float alpha = 1.0;
00214     if ([player cloakPhase] > 0) {
00215         // find out aplha value PF_MIN_ALPHA_VALUE (0.1) means fully cloaked
00216         // 1.0 means fully uncloaked
00217         alpha -= (((1.0 - PF_MIN_ALPHA_VALUE) * [player cloakPhase]) / PLAYER_CLOAK_PHASES);
00218     }
00219       
00220     [imgShips drawInRect:Rect fromRect:shipRect operation: NSCompositeSourceOver fraction:alpha];   
00221 }
00222 
00223 - (int)    maxFuseForMovingTorp {
00224     int torpDim = [imgTorp size].width / 6; 
00225     
00226     return PFN_SLOW_MOTION_FACTOR_MOVE * [imgTorp size].height / torpDim;     
00227 }
00228 
00229 - (int)    maxFuseForExplodingTorp {
00230     int torpDim = [imgExplodeTorp size].width / 6; 
00231     
00232     return PFN_SLOW_MOTION_FACTOR_EXPLODE * [imgExplodeTorp size].height / torpDim;     
00233 }
00234 
00235 - (void)   drawTorp:(Torp*) torp       inRect:(NSRect) Rect {
00236     
00237     NSImage *frames;
00238     int     fusesPerFrame;
00239     
00240     if ([torp status] == TORP_EXPLODE) {
00241         frames = imgExplodeTorp;
00242         fusesPerFrame = PFN_SLOW_MOTION_FACTOR_EXPLODE;
00243     } else {
00244         frames = imgTorp;
00245         fusesPerFrame = PFN_SLOW_MOTION_FACTOR_MOVE;
00246     }
00247     
00248     // get the rigth image 
00249     // assume the torps are in image [my|fed|indiv|kli|ori|rom] (6)
00250     int torpDim = [frames size].width / 6;   
00251     NSRect torpRect = NSMakeRect( 0, 0, torpDim, torpDim); 
00252     
00253     // move the rect to the proper team
00254     if (![[torp owner] isMe]) {
00255         switch ([[[torp owner] team] teamId]) {
00256             case TEAM_FED:
00257                 torpRect.origin.x += 1 * torpDim;
00258                 break;
00259             case TEAM_KLI:
00260                 torpRect.origin.x += 3 * torpDim;
00261                 break;
00262             case TEAM_ROM:
00263                 torpRect.origin.x += 5 * torpDim;
00264                 break;
00265             case TEAM_ORI:
00266                 torpRect.origin.x += 4 * torpDim;
00267                 break;
00268             default:
00269                 torpRect.origin.x += 2 * torpDim;
00270                 break;
00271         }
00272     }
00273     
00274     // move to the proper frame
00275     torpRect.origin.y += ([torp fuse] / fusesPerFrame) * torpDim ;
00276     
00277     [frames drawInRect:Rect fromRect:torpRect operation: NSCompositeSourceOver fraction:1.0];   
00278 }
00279 
00280 - (int)    maxFuseForMovingPlasma {
00281     int plasmaDim = [imgPlasma size].width / 6; 
00282     
00283     return PFN_SLOW_MOTION_FACTOR_MOVE * [imgPlasma size].height / plasmaDim;     
00284 }
00285 
00286 - (int)    maxFuseForExplodingPlasma {
00287     int plasmaDim = [imgExplodePlasma size].width / 6; 
00288     
00289     return PFN_SLOW_MOTION_FACTOR_EXPLODE * [imgExplodePlasma size].height / plasmaDim;     
00290 }
00291 
00292 - (void)   drawPlasma:(Plasma*) plasma       inRect:(NSRect) Rect {
00293     
00294     NSImage *frames;
00295     int     fusesPerFrame;
00296     
00297     if ([plasma status] == PLASMA_EXPLODE) {
00298         frames = imgExplodePlasma;
00299         fusesPerFrame = PFN_SLOW_MOTION_FACTOR_EXPLODE;
00300     } else {
00301         frames = imgPlasma;
00302         fusesPerFrame = PFN_SLOW_MOTION_FACTOR_MOVE;
00303     }
00304     
00305     // get the rigth image 
00306     // assume the plasmas are in image [my|fed|indiv|kli|ori|rom] (6)
00307     int plasmaDim = [frames size].width / 6;   
00308     NSRect plasmaRect = NSMakeRect( 0, 0, plasmaDim, plasmaDim); 
00309     
00310     // move the rect to the proper team
00311     if (![[plasma owner] isMe]) {
00312         switch ([[[plasma owner] team] teamId]) {
00313             case TEAM_FED:
00314                 plasmaRect.origin.x += 1 * plasmaDim;
00315                 break;
00316             case TEAM_KLI:
00317                 plasmaRect.origin.x += 3 * plasmaDim;
00318                 break;
00319             case TEAM_ROM:
00320                 plasmaRect.origin.x += 5 * plasmaDim;
00321                 break;
00322             case TEAM_ORI:
00323                 plasmaRect.origin.x += 4 * plasmaDim;
00324                 break;
00325             default:
00326                 plasmaRect.origin.x += 2 * plasmaDim;
00327                 break;
00328         }
00329     }
00330 
00331     // move to the proper frame
00332     plasmaRect.origin.y += ([plasma fuse]  / fusesPerFrame) * plasmaDim;
00333       
00334     [frames drawInRect:Rect fromRect:plasmaRect operation: NSCompositeSourceOver fraction:1.0];   
00335 }
00336 
00337 - (int)    maxFuseForPlanet {
00338     
00339     // only unknown planets have a image
00340     int frameDim = [imgPlanetUnknown size].width; 
00341     int frames = [imgPlanetUnknown size].height / frameDim;
00342     
00343     return PFN_SLOW_MOTION_FACTOR_PLANET * frames;
00344 }
00345 
00346 - (void)   drawPlanet:(Planet*) planet inRect:(NSRect) Rect { 
00347 
00348     NSImage *frames;
00349     NSRect frameRect;
00350     
00351     // do we know this planet?    
00352     int mask = [[[universe playerThatIsMe] team] bitMask];
00353     if (([planet info] & mask) == 0) {
00354         // nope
00355         frames = imgPlanetUnknown;
00356         frameRect = NSMakeRect( 0, 0, [imgPlanetUnknown size].width, [imgPlanetUnknown size].width);
00357         frameRect.origin.y += ([planet fuse] / PFN_SLOW_MOTION_FACTOR_PLANET) * [imgPlanetUnknown size].width;
00358         //NSLog(@"PainterFactoryForNetrek.drawPlanet %@ frame %d", [planet name], ([planet fuse] / PFN_SLOW_MOTION_FACTOR_PLANET));
00359     } else {
00360         // type is HOME|ARI|ROCK
00361         if ([planet flags] & PLANET_AGRI) {
00362             // pick one of the two images
00363             // more or less at random
00364             if (([planet planetId] % 2) == 0) {
00365                 frames = imgPlanetAgri1;
00366             } else {
00367                 frames = imgPlanetAgri2;
00368             }
00369         } else {
00370             // it's bare rock
00371             if (([planet planetId] % 2) == 0) {
00372                 frames = imgPlanetRock1;
00373             } else {
00374                 frames = imgPlanetRock2;
00375             }
00376         }
00377         
00378         // also check if it is a homeplanet, which would overrule the previous
00379         if        ([[planet name] isEqualToString:@"Earth"]) {
00380             frames = imgPLanetHomeFed;
00381         } else if ([[planet name] isEqualToString:@"Klingus"]){
00382             frames = imgPlanetHomeKli;
00383         } else if ([[planet name] isEqualToString:@"Romulus"]){
00384             frames = imgPlanetHomeRom;
00385         } else if ([[planet name] isEqualToString:@"Orion"]){
00386             frames = imgPlanetHomeOri;
00387         }
00388             
00389         // set the framerect
00390         frameRect = NSMakeRect( 0, 0, [frames size].height, [frames size].height);
00391         
00392         // pick the color depending on the team
00393         switch ([[planet owner] teamId]) {
00394             case TEAM_FED:
00395                 frameRect.origin.x += 0 * frameRect.size.width;
00396                 break;
00397             case TEAM_KLI:
00398                 frameRect.origin.x += 2 * frameRect.size.width;
00399                 break;
00400             case TEAM_ROM:
00401                 frameRect.origin.x += 4 * frameRect.size.width;
00402                 break;
00403             case TEAM_ORI:
00404                 frameRect.origin.x += 3 * frameRect.size.width;
00405                 break;
00406             case TEAM_IND:
00407                 frameRect.origin.x += 1 * frameRect.size.width;
00408                 break;
00409             default:
00410                 NSLog(@"PainterFactoryForNetrek.drawPlanet %@ has no team?", [planet name]);
00411                 frameRect.origin.x += 1 * frameRect.size.width;
00412                 break;
00413         }
00414     }
00415     
00416     // draw it
00417     [frames drawInRect:Rect fromRect:frameRect operation: NSCompositeSourceOver fraction:1.0];    
00418 
00419     // check special icons   
00420     if ([planet flags] & PLANET_REPAIR) {
00421         // draw a wrench on top
00422         NSRect targetRect = Rect;
00423         // get the aspect ratio
00424         float delta = [imgRepair size].height / frameRect.size.height; // eg repair is 1/4 the planet
00425         delta *= 2; // i find the icons very small
00426         targetRect.size.height *= delta;
00427         delta = [imgRepair size].width / frameRect.size.width; // eg repair is half the planet
00428         delta *= 2; // i find the icons very small
00429         targetRect.size.width *= delta;
00430         
00431         // center rect above planet
00432         targetRect.origin.y -= targetRect.size.height;
00433         // and centre
00434         targetRect.origin.x += (Rect.size.width - targetRect.size.width) / 2;
00435         // draw
00436         [imgRepair drawInRect:targetRect fromRect:NSMakeRect(0, 0, [imgRepair size].width, [imgRepair size].height) operation: NSCompositeSourceOver fraction:1.0];
00437     }
00438 
00439     if ([planet flags] & PLANET_FUEL) {
00440         // draw a fuel to the left
00441         NSRect targetRect = Rect;
00442         // get the aspect ratio
00443         float delta = [imgFuel size].height / frameRect.size.height; // eg repair is 1/4 the planet
00444         delta *= 2; // i find the icons very small
00445         targetRect.size.height *= delta;
00446         delta = [imgFuel size].width / frameRect.size.width; // eg repair is half the planet
00447         delta *= 2; // i find the icons very small
00448         targetRect.size.width *= delta;
00449         
00450         // rect next to planet
00451         targetRect.origin.x -= targetRect.size.width;
00452         // and centre
00453         targetRect.origin.y += (Rect.size.height - targetRect.size.height) / 2;
00454         // draw
00455         [imgFuel drawInRect:targetRect fromRect:NSMakeRect(0, 0, [imgFuel size].width, [imgFuel size].height) operation: NSCompositeSourceOver fraction:1.0];
00456     }
00457     
00458     if ([planet armies] > 4) {
00459         // draw a armies to the left
00460         NSRect targetRect = Rect;
00461         // get the aspect ratio
00462         float delta = [imgArmy size].height / frameRect.size.height; // eg repair is 1/4 the planet
00463         delta *= 2; // i find the icons very small
00464         targetRect.size.height *= delta;
00465         delta = [imgArmy size].width / frameRect.size.width; // eg repair is half the planet
00466         delta *= 2; // i find the icons very small
00467         targetRect.size.width *= delta;
00468         
00469         // rect next to planet
00470         targetRect.origin.x += Rect.size.width;
00471         // and centre
00472         targetRect.origin.y += (Rect.size.height - targetRect.size.height) / 2;
00473         // draw
00474         [imgArmy drawInRect:targetRect fromRect:NSMakeRect(0, 0, [imgArmy size].width, [imgArmy size].height) operation: NSCompositeSourceOver fraction:1.0];
00475     }
00476 
00477 }
00478 
00479 @end

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