1

我有一个问题,我不确定如何解决。我需要为我的 XNA 应用程序创建一个 2D 地图编辑器,其中包含一定数量的图块。假设地图将是 50x100 的图块。

我不确定地图、图块使用什么数据结构以及如何将其存储在硬盘驱动器上以供以后加载。

我现在想的是这个。我会将地图存储在一个文本文件中,如下所示:

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

其中 0 = 草,1 = 河流等地面地形,0 = 无,1 = 物体类型的墙。

然后我将有一个游戏组件映射类,它可以读取该文件或从头开始创建一个新文件:

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

      }

我的 Tile 类可能不是游戏组件。我仍然不太确定如何处理碰撞检测,例如来自玩家的子弹与地图对象之间的碰撞检测。这应该由 Map 类还是某种超级 Manager 类来处理?

欢迎任何提示。谢谢!

4

2 回答 2

1

为什么不将其存储为:

Height 
Width
GroundType * (Height * Width)

给予类似的东西

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

它既简单又紧凑。:) 至于游戏内存储,2D 阵列非常适合,除非您有其他特定需求。一种典型的碰撞检测技术是由物理子系统完成一个广泛的阶段,例如使用边界球或轴对齐的边界框,然后计算可能发生碰撞的对象对是否确实存在碰撞。

您的 tile 类可能不应该是一个组件,除非您有非常令人信服的理由。

编辑:忘记了那里的对象类型,但也很容易集成它。

于 2009-04-20T00:27:58.433 回答
0

谢谢,但我决定使用 (ground_type, object_type) 对。所以像这样的输入文件

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

使用如下映射:GROUND = {0:grass, 1:road} OBJECTS = {0:none, 1:crate} 并且 map 的 width=2 和 height=2 将产生以下结果:

grass+crate  |   grass 
grass        |   road+crate

当然,我必须知道地图的宽度/高度才能理解文件。我也可以把它放在输入文件中。

于 2009-04-20T19:08:33.840 回答