views:

1572

answers:

2

Hello, I have a problem and I am not sure how to approach the solution. I need to create a 2D map editor for my XNA app., with a certain number of tiles. Say a map will be 50x100 tiles.

I am not sure what data structure to use for the map, tiles and how to store it on the hard-drive for later loading.

What I am thinking now is this. I will store the map in a text file like so:

//x, y, ground_type, object_type
0, 0, 1, 0
0, 1, 2, 1

where 0=Grass, 1=River etcc for ground terrain, and 0=Nothing, 1=Wall for object types.

Then I will have a Game Component Map class that can read that file or create a new one from scratch:

class Map : DrawableGameComponent {
      //These are things like grass, whater, sand...
      Tile ground_tiles[,];
      //These are things like walls that can be destroyed
      Tile object_tiles[,];

      public Map(Game game, String filepath){
          for line in open(filepath){
               //Set the x,y tile to a new tile
               ground_tiles[line[0], line[1]] = new Tile(line[3])
               object_tiles[line[0], line[1]] = new Tile(line[4])
          }
      }

      public Map(Game game, int width, int heigth){
        //constructor
        init_map()
      }
      private void init_map(){
          //initialize all the ground_tiles
          //to "grass"
          for i,j width, heigth{
              ground_tiles[i,j] = new Tile(TILE.GRASS)
          }


      public override Draw(game_time){
            for tile in tiles: 
                sprite_batch.draw(tile.texture, tile.x, tile.y etc..)

      }

My Tile class will probably NOT be a game component. I am still not quite sure how to handle collision detection for example between a bullet originating from the player with the map object. Should that be handled by the Map class or some kind of super Manager class?

Any hints are welcome. Thanks!

+1  A: 

Why not just store it as:

Height 
Width
GroundType * (Height * Width)

Giving something like

4 
4
0 0 0 0 
0 0 0 0
0 0 0 0
0 0 0 0

It's both easier and more compact. :) As for in-game storage, a 2D array is perfect for this, unless you have other specific needs. A typical collision detection technique is to have a broad phase done by a the physics subsystem, with bounding spheres or axis-aligned bounding boxes for example, and then have the pairs of possibly colliding objects to compute if there was in fact a collision.

Your tile class should probably not be a component, unless you have a very compelling reason.

Edit: Forgot the object type up there, but it'd be easy to integrate it too.

jfclavette
A: 

Thanks but I decided on (ground_type, object_type) pairs. So an input file like

0, 1
0, 0,
0, 0,
1, 1

with mappings like: GROUND = {0:grass, 1:road} OBJECTS = {0:none, 1:crate} and the width=2 and height=2 of map will product the following:

grass+crate  |   grass 
grass        |   road+crate

Granted, I have to know the width/height of the map in order to make sense of the file. I might put that in the input file as well.

drozzy