XNA如何存储和绘制2d地图?

时间:2021-03-10 13:34:40

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.

我有一个问题,我不知道如何处理解决方案。我需要为我的XNA应用程序创建一个2D地图编辑器,并使用一定数量的图块。说地图将是50x100瓷砖。

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.

其中0 = Grass,1 =地面地形的河流等,0 = Nothing,1 =对象类型的墙。

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

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

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?

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

Any hints are welcome. Thanks!

任何提示都是受欢迎的。谢谢!

2 个解决方案

#1


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.

它既简单又紧凑。 :)至于游戏内存储,2D阵列是完美的,除非你有其他特殊需求。典型的碰撞检测技术是由物理子系统完成宽相,例如具有边界球或轴对齐的边界框,然后具有可能碰撞的对的对,以计算实际上是否存在碰撞。

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

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

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

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

#2


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

谢谢,但我决定(ground_type,object_type)对。所以输入文件就像

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:

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

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.

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

#1


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.

它既简单又紧凑。 :)至于游戏内存储,2D阵列是完美的,除非你有其他特殊需求。典型的碰撞检测技术是由物理子系统完成宽相,例如具有边界球或轴对齐的边界框,然后具有可能碰撞的对的对,以计算实际上是否存在碰撞。

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

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

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

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

#2


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

谢谢,但我决定(ground_type,object_type)对。所以输入文件就像

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:

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

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.

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