project_files/HedgewarsMobile/Classes/GameInterfaceBridge.m
changeset 6247 6dfad55fd71c
parent 6246 6b2d19ed521a
child 6259 02765411a912
equal deleted inserted replaced
6246:6b2d19ed521a 6247:6dfad55fd71c
    25 #import "StatsPageViewController.h"
    25 #import "StatsPageViewController.h"
    26 #import "AudioManagerController.h"
    26 #import "AudioManagerController.h"
    27 #import "ObjcExports.h"
    27 #import "ObjcExports.h"
    28 
    28 
    29 @implementation GameInterfaceBridge
    29 @implementation GameInterfaceBridge
    30 @synthesize parentController, savePath, overlayController, engineProtocol, ipcPort, gameType;
    30 @synthesize parentController, savePath, overlayController, engineProtocol, ipcPort;
    31 
    31 
    32 -(id) initWithController:(id) viewController {
    32 -(id) initWithController:(id) viewController {
    33     if (self = [super init]) {
    33     if (self = [super init]) {
    34         self.ipcPort = [HWUtils randomPort];
    34         self.ipcPort = [HWUtils randomPort];
    35         self.gameType = gtNone;
       
    36         self.savePath = nil;
    35         self.savePath = nil;
    37 
    36 
    38         self.parentController = (UIViewController *)viewController;
    37         self.parentController = (UIViewController *)viewController;
    39         self.engineProtocol = [[EngineProtocolNetwork alloc] initOnPort:self.ipcPort];
    38         self.engineProtocol = [[EngineProtocolNetwork alloc] initOnPort:self.ipcPort];
    40         self.engineProtocol.delegate = self;
       
    41 
    39 
    42         self.overlayController = [[OverlayViewController alloc] initWithNibName:@"OverlayViewController" bundle:nil];
    40         self.overlayController = [[OverlayViewController alloc] initWithNibName:@"OverlayViewController" bundle:nil];
    43     }
    41     }
    44     return self;
    42     return self;
    45 }
    43 }
   111     gameArgs[ 5] = [username UTF8String];                                                       //UserNick
   109     gameArgs[ 5] = [username UTF8String];                                                       //UserNick
   112     gameArgs[ 6] = [[[settings objectForKey:@"sound"] stringValue] UTF8String];                 //isSoundEnabled
   110     gameArgs[ 6] = [[[settings objectForKey:@"sound"] stringValue] UTF8String];                 //isSoundEnabled
   113     gameArgs[ 7] = [[[settings objectForKey:@"music"] stringValue] UTF8String];                 //isMusicEnabled
   111     gameArgs[ 7] = [[[settings objectForKey:@"music"] stringValue] UTF8String];                 //isMusicEnabled
   114     gameArgs[ 8] = [[[settings objectForKey:@"alternate"] stringValue] UTF8String];             //cAltDamage
   112     gameArgs[ 8] = [[[settings objectForKey:@"alternate"] stringValue] UTF8String];             //cAltDamage
   115     gameArgs[ 9] = [rotation UTF8String];                                                       //rotateQt
   113     gameArgs[ 9] = [rotation UTF8String];                                                       //rotateQt
   116     gameArgs[10] = (self.gameType == gtSave) ? [self.savePath UTF8String] : NULL;               //recordFileName
   114     gameArgs[10] = ([HWUtils gameType] == gtSave) ? [self.savePath UTF8String] : NULL;          //recordFileName
   117 
   115 
   118     [verticalSize release];
   116     [verticalSize release];
   119     [horizontalSize release];
   117     [horizontalSize release];
   120     [rotation release];
   118     [rotation release];
   121     [localeString release];
   119     [localeString release];
   122     [ipcString release];
   120     [ipcString release];
   123 
   121 
   124     [ObjcExports initialize];
   122     [HWUtils setGameStatus:gsLoading];
   125 
   123 
   126     // this is the pascal fuction that starts the game, wrapped around isInGame
   124     // this is the pascal fuction that starts the game
   127     [HedgewarsAppDelegate sharedAppDelegate].isInGame = YES;
       
   128     Game(gameArgs);
   125     Game(gameArgs);
   129     [HedgewarsAppDelegate sharedAppDelegate].isInGame = NO;
       
   130 }
   126 }
   131 
   127 
   132 // prepares the controllers for hosting a game
   128 // prepares the controllers for hosting a game
   133 -(void) prepareEngineLaunch {
   129 -(void) prepareEngineLaunch {
   134     // we add a black view hiding the background
   130     // we add a black view hiding the background
   183     [AudioManagerController playBackgroundMusic];
   179     [AudioManagerController playBackgroundMusic];
   184 }
   180 }
   185 
   181 
   186 // set up variables for a local game
   182 // set up variables for a local game
   187 -(void) startLocalGame:(NSDictionary *)withOptions {
   183 -(void) startLocalGame:(NSDictionary *)withOptions {
   188     self.gameType = gtLocal;
   184     [HWUtils setGameType:gtLocal];
   189 
   185 
   190     NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
   186     NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
   191     [outputFormatter setDateFormat:@"yyyy-MM-dd '@' HH.mm"];
   187     [outputFormatter setDateFormat:@"yyyy-MM-dd '@' HH.mm"];
   192     NSString *path = [[NSString alloc] initWithFormat:@"%@%@.hws",SAVES_DIRECTORY(),[outputFormatter stringFromDate:[NSDate date]]];
   188     NSString *path = [[NSString alloc] initWithFormat:@"%@%@.hws",SAVES_DIRECTORY(),[outputFormatter stringFromDate:[NSDate date]]];
   193     [outputFormatter release];
   189     [outputFormatter release];
   202     [self prepareEngineLaunch];
   198     [self prepareEngineLaunch];
   203 }
   199 }
   204 
   200 
   205 // set up variables for a save game
   201 // set up variables for a save game
   206 -(void) startSaveGame:(NSString *)atPath {
   202 -(void) startSaveGame:(NSString *)atPath {
   207     self.gameType = gtSave;
       
   208     self.savePath = atPath;
   203     self.savePath = atPath;
       
   204     [HWUtils setGameType:gtSave];
   209 
   205 
   210     [self.engineProtocol spawnThread:self.savePath];
   206     [self.engineProtocol spawnThread:self.savePath];
   211     [self prepareEngineLaunch];
   207     [self prepareEngineLaunch];
   212 }
   208 }
   213 
   209 
   214 -(void) startMissionGame:(NSString *)withScript {
   210 -(void) startMissionGame:(NSString *)withScript {
   215     self.gameType = gtMission;
       
   216     self.savePath = nil;
   211     self.savePath = nil;
       
   212     [HWUtils setGameType:gtMission];
   217 
   213 
   218     NSString *missionPath = [[NSString alloc] initWithFormat:@"escript Missions/Training/%@.lua",withScript];
   214     NSString *missionPath = [[NSString alloc] initWithFormat:@"escript Missions/Training/%@.lua",withScript];
   219     NSDictionary *config = [NSDictionary dictionaryWithObject:missionPath forKey:@"mission_command"];
   215     NSDictionary *config = [NSDictionary dictionaryWithObject:missionPath forKey:@"mission_command"];
   220     [missionPath release];
   216     [missionPath release];
   221     [self.engineProtocol spawnThread:nil withOptions:config];
   217     [self.engineProtocol spawnThread:nil withOptions:config];
   236         [self.parentController presentModalViewController:statsPage animated:YES];
   232         [self.parentController presentModalViewController:statsPage animated:YES];
   237         [statsPage release];
   233         [statsPage release];
   238     }
   234     }
   239 
   235 
   240     // can remove the savefile if the replay has ended
   236     // can remove the savefile if the replay has ended
   241     if (self.gameType == gtSave)
   237     if ([HWUtils gameType] == gtSave)
   242         [[NSFileManager defaultManager] removeItemAtPath:self.savePath error:nil];
   238         [[NSFileManager defaultManager] removeItemAtPath:self.savePath error:nil];
   243     [self release];
   239     [self release];
   244 }
   240 }
   245 
   241 
   246 @end
   242 @end