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:
- 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?
- 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).