level editor design issues

Member
Posts: 34
Joined: 2004.07
Post: #1
Hello all,

as some of you might be aware (irc://irc.freenode.net/#iDevGames people)
I'm working on a game called CocoaRunner (name change is in order i know)
which is a "clone" of the ever popular Lode Runner game.

Right now, I have a design issue

First I have a Tile class, which is the super class of all my tiles in the game, the levelClass has an array of tiles which tells each tile to draw

what my problem is, is this:
when i have a selected tile, i have a pointer to a generic tile
I can't just set the tile clicked at clickpoint.x and clickpoint.y to be equal to the selected tile, i have to create a new instance, otherwise when i change the selected tile, all the tiles in the edited level get changed to because those are just pointers to the same tile instance, my main problem is i don't know how to figure out what class to make the new tile instances.

I'm using Objective-C classes, OpenGL and the Cocoa Event system, it's all working great so far, but this problem has got me stumped

any and all help is appreciated Smile

ss2 cire

P.S.
Please tell me if you need more info
Quote this message in a reply
Member
Posts: 41
Joined: 2005.02
Post: #2
Well, I'm not totally clear on what you are doing, but let's see if this helps you...

Alloc a blank, basic, or empty title to each location in your level.

When you change a title, release the blank title object and alloc a new one of the proper type to that location in the array. Make sure you changing the clickpoint coordinates to the proper location in your array.

To save some memory initially, you can have them all point to the same empty title, just retain it for each location and then release when you put a 'real' tile in it's place.

~ Travis
Quote this message in a reply
Member
Posts: 34
Joined: 2004.07
Post: #3
well, i figured i'd do that but the problem is, how do i figure out which tile to alloc? i could do a big giant switch statement to select the correct class, but I know there must be a better way...
Quote this message in a reply
Member
Posts: 304
Joined: 2002.04
Post: #4
Im a fan of OOP - but in this instance you may want to consider creating a variable that encapsulates all the different tile types into one piece of data. You could use a C "union" variable to hold the specific tile data - and then allocate all your tiles once and be done with it.
Quote this message in a reply
Member
Posts: 304
Joined: 2002.04
Post: #5
something like this (typed in Firefox - so may not compile as-is)

Code:
struct tileStruct {

// bunch of variables that are
// common to all tile types

  union tileTypeUnion {
    struct treeStruct {
      // tree specific variables
    } tree;
    struct rockStruct {
      // rock specific variables
    } rock;
  } tileType
} tiles[100][100];
Quote this message in a reply
Member
Posts: 34
Joined: 2004.07
Post: #6
thanks for the help but... i don't think that is the solution for this problem..

i've already got all the tiles setup to be classes (sub class of tile, which provides all generic functions needed for a tile and the sub classes add specifics)
Quote this message in a reply
Member
Posts: 89
Joined: 2002.04
Post: #7
Couldn't you do a simple 2 dimensional array of tile objects? For example, if you have a screen size that fits 128 x 128 tiles:
Tile tilesArray[128][128];

You can use the power of polymorphism to handle the drawing of the tile by overriding the appropriate methods. So when you want to draw you could do something like the following:

tilesArray[x][y].draw();

Since each subclass of Tile overrides the draw() method the proper image will be drawn.

If you are reading a file for your levels you will still need to define some kind of mapping between the file and the tile types. For example, "0" in the file may map to an EmptyTile object while a "2" may map to a LadderTile object. I'm not sure there is an easy way around this unless you serialize your objects into a file...

Not sure if that helps you or not.
Good luck!
Quote this message in a reply
Member
Posts: 34
Joined: 2004.07
Post: #8
Thanks for all the help

i got it to work Smile
thanks to object introspection i can alloc the correct class by doing

Tile* newTile = [[[SelectedTile class] alloc] init];

and it creates the new class Smile of the type that i want

Regards,
Eric
Quote this message in a reply
Post Reply