Help implementing a C # project: multiple list arrays or a better way?

I am creating a 2D tile RPG in XNA and in the initial design phase. I thought about how I want my winding engine to work, and came up with an approximate sketch. Basically I want a grid of tiles, but in each place of the tile I want to be able to add a few tiles and have an offset. I wish I could do something like adding individual trees to a world map to give more talent. Or place bottles on a bar in a city without drawing a bunch of different tiles with different bottles.

But maybe my reach is higher than mine. I went to implement the idea and had something similar in my Map object:

List<Tile>[,] Grid; 

But then I thought about it. Let's say I had a 200x200 world map that would actually be pretty small if the RPGs went. That would be 40,000 lists. In my opinion, I think there should be a better way. Now this is a preliminary mature optimization. I don’t know if I can manage this way to create my own maps and game, but it seems uselessly ineffective and something that can sneak up if my game becomes more complicated.

One of my ideas is to make the offset and some fragments optional, so that I pay for them only when necessary. But I do not know how to do this. Multiple arrays of objects?

 object[,] Grid; 

So my criteria are:

  • Two-dimensional grid of tile locations
  • There is a minimum of 1 tile in each tile location, but may be larger if necessary
  • Each additional tile may additionally have an offset of x and y for point placement.

Can someone help with some ideas for implementing such a design (shouldn't this be done for me, just ideas), while maintaining a minimum of memory usage?

If you need more background, roughly what my Map and Tile objects make up:

 public struct Map { public Texture2D Texture; public List<Rectangle> Sources; //Source Rectangles for where in Texture to get the sprite public List<Tile>[,] Grid; } public struct Tile { public int Index; //Where in Sources to find the source Rectangle public int X, Y; //Optional offsets } 
+4
source share
3 answers

What you can do is just have a Tile array:

 class Grid { Tile[,] grid; } 

... and this Tile class has a List<Sprite> in it:

 class Tile { List<Sprite> sprites; } 

... and that the Sprite class will have your texture and offset:

 class Sprite { Vector2 offset; Texture2D texture; } 

Complete all this using drawing methods:

 class Grid { Tile[,] grid; void Draw(GraphicsDevice graphics) { // call your tiles Draw() } } class Tile { List<Sprite> sprites; void Draw(GraphicsDevice graphics, int x, int y) { // call your sprites Draw() } } class Sprite { Vector2 offset; Texture2D texture; // or texture and rectangle, or whatever void Draw(GraphicsDevice graphics, int x, int y) { // draw the sprite to graphics using x, y, offset and texture } } 

Of course, it gets a lot harder, but you should get this idea.

Separating all your problems into different classes, you easily add a new fonctionnality function that will not conflict with existing code. Trying to crush all your data in one object, such as List <Tile> [,], is bad form and will eventually bite you when you try to expand.

+4
source

Your approach seems to confuse presentation with behavior. If the game’s behavior is tile-based, then create it functionally, and then create a presentation as a result of the state of the board.

+1
source

You essentially want the sparse matrix to represent the “decorations” on each tile. A rare matrix is ​​a matrix structure in which not all elements need to matter. There are C # libraries that represent them.

A simple approach would be to use a regular dictionary in which the key is Tile # (a unique number for each tile), which can, for example, be calculated using the same type of formula that is used to address video memory: Y * MAXIMUM_X + X. For this tile just check if there is a unique tile entry for it #. The dictionary should probably contain a list of decorations for this particular tile:

 Dictionary<int, List<Sprites>> spritesPerTile; // ... if (spritesPerTile.ContainsKey(tileNumber)) { List<Sprites> decorationsThisTile = spritesPerTile[tileNumber]; // Proceed to render sprites on this tile. } 
+1
source

Source: https://habr.com/ru/post/1304492/


All Articles