开发者

Creating a Grid - What is an efficient way to do this

I'm currently working on a little text-based game after watching the new Tron movie the other day. (as you do when you're a geek with to much time on your hands).

I've created a Grid in which objects can be placed and am currently finding that the creation of my grid is taking a long time.

I'm interested in how you'd impliment this and any design patterns, ideas, concepts etc that are useful for this type of thing.

Currently I have 4 main 'parts' that compose the grid.

First off I have the grid itself which contains an array of 'rows'

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

A GridRow in turn holds an array of GridCell's, each of which contains an IEntity array (every object that can be placed in a grid cell must impliment 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 creating the grid it takes quite a long time. Currently a 200*200 'grid' takes ~17 seconds to compose.

I'm quite sure there must be a much more efficient way to either store this data or create the grid using the current concept.

Any and all advice welcome.

UPDATE

Here's how I currently compose the grid:

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++)
            {
                masterC开发者_高级运维ells[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;
        }


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

In any case I the code you have shown do not suggest the reason for the slow down. Can you post the code where you are actually allocating the objects?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜