【问题标题】:XNA how to store and draw a 2d map?XNA如何存储和绘制二维地图?
【发布时间】:2009-04-20 00:23:31
【问题描述】:

我有一个问题,我不确定如何解决。 我需要为我的 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 类来处理?

欢迎任何提示。 谢谢!

【问题讨论】:

    标签: c# xna


    【解决方案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 类可能不应该是一个组件。

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

    【讨论】:

      【解决方案2】:

      谢谢,但我决定使用 (ground_type, object_type) 对。 所以像

      这样的输入文件
      0, 1
      0, 0,
      0, 0,
      1, 1
      

      使用如下映射: 地面 = {0:草,1:道路} 对象 = {0:none, 1:crate} map 的 width=2 和 height=2 将产生以下结果:

      grass+crate  |   grass 
      grass        |   road+crate
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-05
        • 2016-07-20
        • 2016-02-14
        • 1970-01-01
        • 2016-03-26
        • 1970-01-01
        相关资源
        最近更新 更多