简体   繁体   中英

Loading a 2D map - C# Monogame

Okay everyone, I have a problem.

I am making a Map Editor for my 2D game.

Basically I have a "Slot" object, and it contains a "Tile" object (which belongs to slot and has a Texture). I have 2 Types of slots:

  1. Type is Pen Slots, when you press them and form pops up which you use to load an image for a tile anywhere on the computer. Last clicked tile is an active tile.

  2. Type is Map tile. They are 32x32 slots that are stacked next to each other an make it look like a map. When you have a pen and you clicked on it so its active, you can draw a map then, it copies pen slot texture to map texture.

Now I managed to save my map to a text file (for each tile -> Path.GetFileNameWithoutExtenson -> save map.txt). And I can load a text file to get saved names, BUT I DONT KNOW WHAT TO DO WITH TEXTURES, should I:

1. I would be using this Editor for my game, so I could put all my textures from the game inside some folder in Map Editor project, same textures same names that could work.

2. ????

Could anyone reccomend me if they can think of another way to load textures into Map slots ?

EDIT:

Lets say I have a 3 x 3 map and 3 textures as tiles:

sky sky sky sky grass sky dirt grass dirt

also texture names are also sky, grass and dirt, so its easy to store texture names in variables/array whatever when loading a map. But what about textures? What do I do with the images.

Lets say I use my map editor for 10 different games, I save a map as a map.txt file with texture names. I restart map editor and I want to load the map, it can load text given data fine, but what about images, how do I pass tiles from map editor to game

When saving information it's generally best to segregate data as much as possible. This allows for easier maintenance in the future, and overall cleaner design.

In your case I would store each unique tile in an array, and simply have the map keep track of which tiles go where (through indexes into the tile array). This is the most common way tile maps are implemented in games .

How you actually keep track of the data on disc is entirely up to you, although I would recommend JSON as there are many libraries that make parsing it very easy. Although you could use serializable objects, or even write your own file format. For more information on reading / writing JSON files in C#, see this post .

An example of a test map stored to disc as JSON would look something like this:

{  
   "tiles": [  
      {  
         "id": 1,
         "hasCollision": false,
         "texture": "DirtPath.png"
      },
      {  
         "id": 2,
         "hasCollision": true,
         "texture": "DeadTreeStump.png"
      }
   ],
   "maps": [  
      {  
         "id": 1,
         "name": "Test Map",
         "tiles": [  
            0, 0, 0, 0, 0,
            0, 1, 1, 1, 0,
            0, 1, 2, 1, 0,
            0, 1, 1, 1, 0,
            0, 0, 0, 0, 0 ]
      }
   ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM