Best way to implement multiple levels within Objective-C roguelike?

I'm working on a roguelike using Objective-C/Cocoa to learn more. I've gotten most of the basic functionality out of the way, but I still have one problem I've been trying to figure out.

Here's a breakdown of the process:

First, the map is loaded:

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;

Then, in the populateMap function, it goes through each cell of the loaded map, using NSPoints and a loop, and creates objects based on the data from the map in WorldArray. For items, normal floor is put in where the item is, and an item is then made in itemArray. Both arrays are 30x30, as determined by the height of the map.

Here is the populateMap code:

- (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]; }

This is what is called when things are spawned:

- (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 and itemArray are what the game works on from that moment onwards, including the drawing. The player is inside of worldArray as well. I'm considering splitting the player into another array of characterArray, to make it easier when I add things like monsters in the not so distant future.

Now, when I load a new level, I had first considered methods like saving them to data and loading them later, or some sort of savestate function. Then I came to the realization that I would need to be able to get to everything at the same time, because things can still happen outside of the player's current scope, including being chased by monsters for multiple floors, and random teleports. So basically, I need to figure out a good way to store worldArray and itemArray in a way that I will be able to have levels of them, starting from 0 and going onward. I do need a savestate function, but there's no point touching that until I have this done, as you shouldn't actually be allowed to save your game in roguelikes.

So to reiterate, I need to have one set of these arrays per level, and I need to store them in a way that is easy for me to use. A system of numbers going from 0-upward are fine, but if I could use something more descriptive like a map name, that would be much better in the long run.

I've figured out my problem, I'm using an NSMutableDictionary for each and storing them with the keys that correspond to each level. Works like a charm. Bigger problems elsewhere now.

-------------Problems Reply------------

I figured it out, I'm using NSMutableDictionaries, one for each array (objects, items, eventually characters). They're stored using the name of the level. Works like a charm.

Category:objective c Views:0 Time:2009-07-22

Related post

  • How to implement multiple levels of sorting on a CollectionViewSource 2011-11-12

    I originally posted this as a LINQ query - got it working and then realised I had a problem. You can't use LINQ queries to select/filter and order the items in a CollectionViewSource (why oh why didn't I check this first, why oh why isn't this possib

  • How to implement multiple-level filters in an extensible manner? 2009-11-17

    Should make it extensible. When I say make it extensible,I mean that the detail of filter options can be added later on with ease. By design,it's better that parsing and rendering are independant.(?) During parsing,need to show the next level filter

  • How to manage multiple levels of Objects using Casbah and Subset? 2012-02-01

    I have three objects case class Metric(val name: String, val tags: Map[String, String]) case class Threshold(val metric: Metric, val critical: Long, val warning: Long) class Profile(val name: String, val thresholds: List[Threshold]) I plan to store o

  • Implementing Row-Level Security – (SPs vs LINQ to Objects) 2009-10-15

    I believe this is more a question about best practices and design than anything else. I tried searching for similar queries regarding this but couldn’t find any. I actually found the Row Level Security with Entity Framework but I believe the context

  • multiple levels of associated db objects to YAML 2011-06-01

    I need to create a 'List' object from the following db tables. I've already done this in a rails/datamapper application, but now I have a need to get specific lists into and out of a db through YAML. List Categories Items Item choices e.g. given a li

  • How to handle Dtos for objects which implement multiple interfaces? 2012-03-07

    We are using Dtos in our WCF service interface, but have started to come across issues when the Business Object that the Dto represents implements more than a single interface and we want to return the Dtos in those different contexts and to also be

  • Iterate and search values in a object-literal having multiple levels 2012-12-24

    Here is what I want to do: I have a tree (javascript object-literal structure) with multiple levels. I have a value of a particular key of this object. I want to search for this exact key-value pair in the structure and return the value of another ke

  • How to implement multiple threads in Java 2009-03-17

    How do I implement multiple threads in Java? --------------Solutions------------- Start here: Java Sun Tutorial, Lesson: Concurrency And perhaps focus on this part: High Level Concurrency Objects You could do it this way: Write a class that implement

  • How do I implement multiple inheritence in Java 2010-10-12

    I'm working with a certain API library in Java. It has a base class A, as well as B and C which both extend A. B & C provide similar but distinct functionality, all three classes are in the library. public abstract class A { virtual foo(); } publ

  • implementing multi-level "iterator" in PHP 2011-07-04

    I'm trying to create a iterator like this one, for a list of comments: // the iterator class, pretty much the same as the one from the php docs... abstract class MyIterator implements Iterator{ public $position = 0, $list; public function __construct

  • Customizing Windows Right-Click menus with multiple levels 2009-03-17

    I understand the process needed to customize a right click menu going through the regedit etc. However I need to the ability to go multiple levels such as in applications like WinZip. Here's a picture for clarification of what I need --------------So

  • Uses for multiple levels of pointer dereferences? 2009-04-17

    When does using pointers in any language require someone to use more than one, let's say a triple pointer. When does it make sense to use a triple pointer instead of just using a regular pointer? For example: char * * *ptr; instead of char *ptr; ----

  • NHibernate Eager Fetching Over Multiple Levels with StatelessSession 2010-02-23

    I've referenced the following thread which works with a Session but not a StatelessSession. The problem I am having is similar to the above thread. I'm getting b

  • Multiple levels of 'collection.defaultdict' in Python 2010-04-08

    Thanks to some great folks on SO, I discovered the possibilities offered by collections.defaultdict, notably in readability and speed. I have put them to use with success. Now I would like to implement three levels of dictionaries, the two top ones b

  • Is there a way to declare a variable that implements multiple interfaces in .Net? 2010-05-03

    Similar to this Java question. I would like to specify that a variable implements multiple interfaces. For instance private {IFirstInterface, ISecondInterface} _foo; public void SetFoo({IFirstInterface, ISecondInterface} value) { _foo = value; } Requ

  • How does implementing multiple COM interfaces work in C++? 2010-06-13

    I am trying to understand this example code regarding Browser Helper Objects. Inside, the author implements a single class which exposes multiple interfaces (IObjectWithSite, IDispatch). His QueryInterface function performs the following: if(riid ==

  • how to dump multiple levels LINQPAD 2010-10-09

    when i do a object.dump() i can get only 1 level. How do i do multiple levels. is ther any examples. --------------Solutions------------- Looking at that screenshot, the simple answer is that LINQPad's default Dump depth of 5 is not enough for that o

  • how to create multiple level subdomains with apache2 and/or PHP 2010-12-03

    I am looking for a way to have multiple-level subdomains on a server running Apache2.2 and PHP5. Ideally the solution will be in Apache and not PHP. For instance: OR I've seen this on commercial sites before

  • DataSet with multiple levels of related tables 2011-01-09

    I am trying to use DataSet and DataAdapter to "filter" and "navigate" DataRows in DataTables. THE SITUATION: I have multiple logical objects, e.g. Car, Door and Hinge. I am loading a Form which will display complete Car information including each Doo

Copyright (C), All Rights Reserved.

processed in 0.170 (s). 11 q(s)