2009-07-22 56 views
1

我正在使用Objective-C一roguelike /可可瞭解更多信息。我已經獲得了大部分基本功能,但我仍然遇到了一個我一直在想的問題。在Objective-C roguelike中實現多個級別的最佳方法?

下面是該方法的一個擊穿:

首先,地圖被加載:

NSString* mapPath = [[NSBundle mainBundle] pathForResource:mapFileName ofType:mapFileType]; 
NSURL* mapURL = [NSURL fileURLWithPath: mapPath]; 
currentMap_ = [[Map alloc] initWithContentsOfURL: mapURL]; 
worldArray = [[NSMutableArray alloc] init]; 
itemArray = [[NSMutableArray alloc] init]; 
[self populateMap]; 
return; 

然後,在populateMap功能,它通過所加載的地圖的每個小區,使用NSPoints和一個循環,並根據WorldArray中地圖的數據創建對象。對於物品,普通地板放在物品所在的位置,然後在itemArray中製作物品。這兩個陣列都是30x30,由地圖的高度決定。

這裏是populateMap代碼:

- (void)populateMap 
{ 
NSPoint location; 

for (location.y = 0; location.y < [currentMap_ height]; location.y++) 
{ 
    for (location.x = 0; location.x < [currentMap_ width]; location.x++) 
    { 
     char mapData = [currentMap_ dataAtLocation: location]; 
     for (GameObject *thisObject in worldDictionary) 
     { 
      //NSLog(@"char: <%c>", [thisObject single]); 
      if (mapData == [thisObject single]) 
      { 
       NSString* world = [thisObject className]; 
       //NSLog(@"(%@) object created",thisObject); 
       [self spawnObject:world atLocation:location]; 
      } 
     } 
     for (Item *thisObject in itemDictionary) 
     { 
      //NSLog(@"char: <%c>", [thisObject single]); 
      if (mapData == [thisObject single]) 
      { 
       NSString* item = [thisObject className]; 
       NSString* floor = [NormalFloor className]; 
       //NSLog(@"(%@) object created",thisObject); 
       [self spawnItem:item atLocation:location]; 
       [self spawnObject:floor atLocation:location]; 
      } 
     } 
     if (mapData == '1' 
      && [player_ stepsTaken] <= 0) 
     { 
      //NSLog(@"player spawned at (%f, %f)",location.x,location.y); 
      player_ = [[Player alloc] initAtLocation: location]; 
     } 
     if (mapData == '1') 
     { 
      //NSLog(@"floor created at (%f, %f)",location.x,location.y); 
      [worldArray addObject:[[NormalFloor alloc] initAtLocation: location]]; 
     } 
    } 
} 
[self setNeedsDisplay:YES]; 
} 

這是當事情產生了什麼叫做:

- (void)spawnObject: (NSString*) object atLocation: (NSPoint) location 
{ 
    //NSLog(@"(%@) object created",thisObject); 
    [worldArray addObject:[[NSClassFromString(object) alloc] initAtLocation: location]]; 
} 
- (void)spawnItem: (NSString*) item atLocation: (NSPoint) location 
{ 
    //NSLog(@"(%@) object created",thisObject); 
    [itemArray addObject:[[NSClassFromString(item) alloc] initAtLocation: location]]; 
} 

worldArray和itemArray是什麼遊戲,從那個時刻工作在起,包括畫畫。玩家也在worldArray內。我正在考慮將玩家分成另一個characterArray陣列,以便在不久的將來添加諸如怪物之類的東西時更容易。

現在,當我加載一個新的級別時,我首先考慮了將它們保存到數據並稍後加載它們的方法,或者某種類型的豐富功能。然後我又意識到我需要能夠得到在同一時間一切,因爲事情都還在外面發生玩家的當前範圍,包括怪物的多個樓層,並隨機傳送點被追逐。所以基本上,我需要找出一個好方法來存儲worldArray和itemArray,以便能夠從0開始並繼續前進。我確實需要一個功能豐富的功能,但是在完成之前沒有任何意義,因爲你實際上不應該被允許將你的遊戲保存在roguelikes中。

所以要重申,我需要在每個關卡中有一組這樣的數組,我需要以易於使用的方式存儲這些數組。從0向上的數字系統很好,但如果我可以使用像地圖名稱那樣更具描述性的內容,那麼從長遠來看,這樣做會好得多。

我已經想通了我的問題,我使用每個NSMutableDictionary並將它們與每個級別對應的鍵一起存儲。奇蹟般有效。現在其他地方的問題更大。

回答

1

我想通了,我使用NSMutableDictionaries,每個數組(對象,項目,最終字符)一個。它們使用關卡的名稱進行存儲。奇蹟般有效。

+1

http://sneakyness.com/GTFO/01.75.app.zip如果您想嘗試什麼,我有這麼遠。僅限OS X。 – Sneakyness 2009-07-23 14:03:57

相關問題