Grid creation - what is an effective way to do it

I am currently working on a small text game after watching the new Throne movie the other day. (how you do it when you play with a lot of time on your hands).

I have created a grid in which objects can be placed, and currently I find that creating my grid takes a lot of time.

I wonder how you would apply this and any design patterns, ideas, concepts, etc. that would be useful for this type of thing.

I currently have 4 main โ€œpartsโ€ that make up the grid.

Firstly, I have the grid itself, which contains an array of "rows"

public interface IGrid { GridRow[] Rows { get; set; } } 

A GridRow , in turn, contains an array of GridCell 's, each of which contains an array of IEntity (each object that can be placed in a grid cell must implement IEntity.

 public class GridRow { public int Index { get; set; } public GridCell[] Cells { get; set; } } public class GridCell { public int Index { get; set; } public IEntity[] Entities { get; set; } } 

When it comes to meshing, it takes quite a while. Currently 200 * 200 'grid takes ~ 17 seconds to compose.

I am sure there should be a much more efficient way to save this data or create a grid using the current concept.

We invite everyone and all tips.

UPDATE

This is how I am drawing up the grid now:

 public IGrid Create(int rows, int cols, int concurentEntitiesPerCell) { var grid = new Grid(); Rows = new GridRow[rows]; grid.Rows = Rows; var masterCells = new GridCell[cols]; var masterRow = new GridRow(); var masterCell = new GridCell(); for (var i = 0; i < masterCells.Count(); i++) { masterCells[i] = new GridCell(); masterCells[i].Index = i; masterCells[i].Entities = new IEntity[concurentEntitiesPerCell]; } masterRow.Cells = masterCells; for (var j = 0; j < rows; j++) { grid.Rows[j] = new GridRow(); } return grid; } 
+4
source share
1 answer

It seems you are creating a lot of objects for no reason? Why not just use a List<List<IEntity>> or a multidimensional array of Entities [,]?

In any case, the code you showed does not indicate the reason for the slowdown. Can you post the code in which you actually place the objects?

+3
source

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


All Articles