/// <summary> /// The neighbours of a block /// </summary> /// <param name="currentBlock"></param> /// <returns></returns> public BlockNeighbours GetNeighbours(Block currentBlock) { BlockNeighbours neighbours = new BlockNeighbours(); if (currentBlock == null) { return(neighbours); } if (currentBlock.Y != 0) { neighbours.Up = Blocks[currentBlock.X, currentBlock.Y - 1]; } if (currentBlock.X != Width - 1) { neighbours.Right = Blocks[currentBlock.X + 1, currentBlock.Y]; } if (currentBlock.Y != Height - 1) { neighbours.Down = Blocks[currentBlock.X, currentBlock.Y + 1]; } if (currentBlock.X != 0) { neighbours.Left = Blocks[currentBlock.X - 1, currentBlock.Y]; } return(neighbours); }
private void FreshDistance() { Distances = new int[map.Width, map.Height]; for (int i = 0; i < map.Width; i++) { for (int j = 0; j < map.Height; j++) { Distances[i, j] = int.MaxValue; } } Distances[X, Y] = 0; BlockNeighbours neighbourhood = map.GetNeighbours(myBlock); var allneighbours = neighbourhood.Neighbours; int currentDistance = 0; Block[] validNeighbours = (from b in allneighbours where b.Passable && Distances[b.X, b.Y] > (currentDistance + 1) select b).ToArray(); while (validNeighbours.Length > 0) { List <Block> newNeighbours = new List <Block>(); foreach (Block neighbour in validNeighbours) { Distances[neighbour.X, neighbour.Y] = currentDistance + 1; //newNeighbours.AddRange(); foreach (Block newNeighbour in map.GetNeighbours(neighbour).Neighbours) { if (!newNeighbours.Contains(newNeighbour)) { newNeighbours.Add(newNeighbour); } } } validNeighbours = newNeighbours.Where(b => Distances[b.X, b.Y] > currentDistance + 1 && b.Passable).ToArray(); currentDistance++; } }