Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Objective-C roguelike中实现多个级别的最佳方法?_Objective C_Cocoa_Data Structures - Fatal编程技术网

在Objective-C roguelike中实现多个级别的最佳方法?

在Objective-C roguelike中实现多个级别的最佳方法?,objective-c,cocoa,data-structures,Objective C,Cocoa,Data Structures,我正在使用Objective-C/Cocoa开发一个类似roguelike的程序,以了解更多信息。我已经完成了大部分基本功能,但我仍然有一个问题一直在努力解决 以下是流程的分解: 首先,加载地图: NSString* mapPath = [[NSBundle mainBundle] pathForResource:mapFileName ofType:mapFileType]; NSURL* mapURL = [NSURL fileURLWithPath: mapPath]; currentMa

我正在使用Objective-C/Cocoa开发一个类似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];
}
worldArray和itemArray是游戏从那一刻开始的工作,包括绘图。玩家也在worldArray内。我正在考虑将玩家拆分为另一个角色数组,以便在不远的将来添加怪物之类的东西时更加容易

现在,当我加载一个新的级别时,我首先考虑了一些方法,比如将它们保存到数据中,然后再加载,或者某种形式的savestate函数。然后我意识到我需要能够同时获得所有东西,因为事情仍然可能发生在玩家当前范围之外,包括被怪物追逐多个楼层,以及随机传送。所以,基本上,我需要找到一种很好的方法来存储worldArray和itemArray,这样我就可以从0开始,一直到下一个级别。我确实需要一个savestate函数,但在我完成这项工作之前,没有必要去碰它,因为你实际上不应该被允许用roguelikes保存你的游戏

所以重申一下,我需要在每个级别上有一组这样的阵列,并且我需要以一种易于使用的方式存储它们。从0到以上的数字系统是可以的,但如果我能使用更具描述性的名称,如地图名称,从长远来看会更好


我已经解决了我的问题,我正在为每个级别使用NSMutableDictionary,并使用对应于每个级别的键存储它们。工作起来很有魅力。现在其他地方出现了更大的问题。

我发现了这个问题,我正在使用NSMutableDictionary,每个数组(对象、项目,最终是字符)对应一个。它们使用级别的名称存储。工作起来很有魅力

我找到了答案,我使用了nsmutabledictionary,每个数组(对象、项,最终是字符)对应一个。它们使用级别的名称存储。工作起来很有魅力

如果你想试试我目前所做的。仅限OSX。如果你想试试我目前所做的。仅限OSX。
- (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]];
}