Help with C# program design implementation: multiple array of lists or a better way?

Posted by Bob on Stack Overflow See other posts from Stack Overflow or by Bob
Published on 2010-03-18T19:11:10Z Indexed on 2010/03/18 19:21 UTC
Read the original article Hit count: 245

Filed under:
|
|

I'm creating a 2D tile-based RPG in XNA and am in the initial design phase. I was thinking of how I want my tile engine to work and came up with a rough sketch. Basically I want a grid of tiles, but at each tile location I want to be able to add more than one tile and have an offset. I'd like this so that I could do something like add individual trees on the world map to give more flair. Or set bottles on a bar in some town without having to draw a bunch of different bar tiles with varying bottles.

But maybe my reach is greater than my grasp. I went to implement the idea and had something like this in my Map object:

List<Tile>[,] Grid;

But then I thought about it. Let's say I had a world map of 200x200, which would actually be pretty small as far as RPGs go. That would amount to 40,000 Lists. To my mind I think there has to be a better way. Now this IS pre-mature optimization. I don't know if the way I happen to design my maps and game will be able to handle this, but it seems needlessly inefficient and something that could creep up if my game gets more complex.

One idea I have is to make the offset and the multiple tiles optional so that I'm only paying for them when needed. But I'm not sure how I'd do this. A multiple array of objects?

object[,] Grid;

So here's my criteria:

  • A 2D grid of tile locations
  • Each tile location has a minimum of 1 tile, but can optionally have more
  • Each extra tile can optionally have an x and y offset for pinpoint placement

Can anyone help with some ideas for implementing such a design (don't need it done for me, just ideas) while keeping memory usage to a minimum?

If you need more background here's roughly what my Map and Tile objects amount to:

public struct Map
{
    public Texture2D Texture;
    public List<Rectangle> Sources; //Source Rectangles for where in Texture to get the sprite
    public List<Tile>[,] Grid;
}
public struct Tile
{
    public int Index; //Where in Sources to find the source Rectangle
    public int X, Y; //Optional offsets
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about XNA