views:

171

answers:

2

I'm working on a basic iPhone game that requires a single-screen tilemap. Nothing difficult there. I come from a C background, so my current solution looks a bit like this:

typedef struct _Tile {
    NSString *type;
} Tile;

@interface Map {
    Tile mapData[MAP_TILE_MAX_X][MAP_TILE_MAX_Y];
}

This works fine, but I'm wondering if there's a slightly more 'correct' way to handle things via Objective-C. Here's how I see the situation going if I were to adopt an Objective-C approach: I'd create a base Tile class to hold basic tile properties, which I could then subclass for specific tile types (@interface Water : Tile {}, for example). This would allow me to also have Tile-specific logic. For instance: the Tile class could have a 'think' method which would execute any necessary logic. In my Water subclass, this may involve creating a ripple effect if the player were submerged.

My questions then:

  1. Is it acceptable to use C structs in this situation? If not, am I on the right track with regards to my Obj-C approach?
  2. If I were to create a base Tile class and use subclasses for specific Tile types, how would I dynamically instantiate each Tile subclass (given that I have an NSString containing the type 'water', I would need to instantiate the Water class).
A: 

Using structs is what I would do here.., I love using them as it makes my code look awesome and its easy to use.

Apple uses it too, what about NSRect NSPoint and NSSize.

Also I would store types like:

typedef enum{
  TileTypeDefault = 0,
  TileTypeSomething,
  TileTypeLOL
} TileType;

switch((TileType)type) { case TileTypeDefault: [[TileDefault alloc] initWithSize:size]; break; }

and so on.. if you don't really get what iam trying to say comment me :P

Antwan van Houdt
I'm aware of enums. But can you justify the use of a struct here in lieu of a class? I'm not particularly interested in making my code "awesome", I would just like to make it proper.
ndg
+1  A: 

The second part of your question is easier to address, so I will approach that first.

Dynamically instantiating an object of an arbitrary class at runtime can be done using NSClassFromString()

Assuming your WaterTile is a subclass of UIView (which seems to make sense since it is likely to to be drawn on the screen)

UIView *newTile = [[NSClassFromString([NSString stringWithFormat:@"%@TileView", [@"water" capitalizedString]]) alloc] init];

Now for the first part.

Given that the tile is something that you want to draw on screen you will benefit from all of the OO goodness by inheriting from UIView which will respond to touch events and have methods needed for positioning and drawing. This is the major advantage over using a struct for your tiles.

Chances are, the abstract Tile class that you are thinking of won't really be needed as most of the properties and methods provided by UIView which leads me to think that you may want to define a Tile @protocol instead.

@protocol TileViewDrawing 
- (void)drawThinking;
@end

@interface WaterTileView : UIView <TileViewDrawing>
@end

@implementation WaterTileView
-(void)drawThinking
{
    // Code to show rippling effect
}
@end

To create a 2D arrays in your Map define an NSArray (columns) of NSArrays (row)

falconcreek