Ejemplo n.º 1
0
        /// <summary>Initializes a rows * cols sized raster.</summary>
        protected virtual void Initialize(int cols, int rows)
        {
            var neighbors = new SimpleList <Vector>(8);

            if (GridType.HasFlag(GridType.Horizontal))
            {
                neighbors.Add(Vector.W);
                neighbors.Add(Vector.E);
            }
            if (GridType.HasFlag(GridType.Veritical))
            {
                neighbors.Add(Vector.N);
                neighbors.Add(Vector.S);
            }
            if (GridType.HasFlag(GridType.Diagonal))
            {
                neighbors.Add(Vector.NE);
                neighbors.Add(Vector.NW);
                neighbors.Add(Vector.SE);
                neighbors.Add(Vector.SW);
            }

            foreach (var point in Points.Grid(cols, rows))
            {
                var n = GridType.IsSpheric()
                    ? neighbors.Count
                    : neighbors.Select(v => point + v).Count(p => OnGrid(p));

                var index = Index(point.X, point.Y);
                Tiles[index] = Create(index, point.X, point.Y, n);
            }

            foreach (var tile in Tiles)
            {
                foreach (var position in neighbors.Select(v => tile.Position + v))
                {
                    var neighbor = this[position];
                    if (neighbor is ITile)
                    {
                        tile.Neighbors.Add(neighbor);
                    }
                }
            }
        }