private void findDistances(int sourceX, int sourceY, int sourceOrientation, Cell[,] resultMatrix) { for (int i = 0; i < columnsGrid; i++) { for (int j = 0; j < columnsGrid; j++) { resultMatrix[i, j].distances[0] = resultMatrix[i, j].distances[1] = resultMatrix[i, j].distances[2] = resultMatrix[i, j].distances[3] = resultMatrix[i, j].min = int.MaxValue; } } Queue<int[]> q = new Queue<int[]>(); q.Enqueue(new int[] { sourceX, sourceY }); resultMatrix[sourceY, sourceX].setDistance(0, sourceOrientation == 0 ? 0 : 1, -1); resultMatrix[sourceY, sourceX].setDistance(1, sourceOrientation == 1 ? 0 : 1, -1); resultMatrix[sourceY, sourceX].setDistance(2, sourceOrientation == 2 ? 0 : 1, -1); resultMatrix[sourceY, sourceX].setDistance(3, sourceOrientation == 3 ? 0 : 1, -1); int[] tmp; int parentDistance; int tmpMov; while (q.Count > 0) { tmp = q.Dequeue(); parentDistance = resultMatrix[tmp[1], tmp[0]].getDistance(0); if (tmp[1] > 0 && !barriers.Contains(arena[tmp[1] - 1, tmp[0]])) { if (resultMatrix[tmp[1] - 1, tmp[0]].getDistance(0) > parentDistance + 2) { if (resultMatrix[tmp[1], tmp[0]].moveTo == -1) { tmpMov = 0; } else { tmpMov = resultMatrix[tmp[1], tmp[0]].move[0]; } resultMatrix[tmp[1] - 1, tmp[0]].setDistance(0, parentDistance + 1, tmpMov); resultMatrix[tmp[1] - 1, tmp[0]].setDistance(1, parentDistance + 2, tmpMov); resultMatrix[tmp[1] - 1, tmp[0]].setDistance(2, parentDistance + 2, tmpMov); resultMatrix[tmp[1] - 1, tmp[0]].setDistance(3, parentDistance + 2, tmpMov); q.Enqueue(new int[] { tmp[0], tmp[1] - 1 }); } else if (resultMatrix[tmp[1] - 1, tmp[0]].getDistance(0) > parentDistance + 1) { resultMatrix[tmp[1] - 1, tmp[0]].setDistance(0, parentDistance + 1, resultMatrix[tmp[1], tmp[0]].move[0]); q.Enqueue(new int[] { tmp[0], tmp[1] - 1 }); } } parentDistance = resultMatrix[tmp[1], tmp[0]].getDistance(1); if (tmp[0] < columnsGrid - 1 && !barriers.Contains(arena[tmp[1], tmp[0] + 1])) { if (resultMatrix[tmp[1], tmp[0] + 1].getDistance(1) > parentDistance + 2) { if (resultMatrix[tmp[1], tmp[0]].moveTo == -1) { tmpMov = 1; } else { tmpMov = resultMatrix[tmp[1], tmp[0]].move[1]; } resultMatrix[tmp[1], tmp[0] + 1].setDistance(0, parentDistance + 2, tmpMov); resultMatrix[tmp[1], tmp[0] + 1].setDistance(1, parentDistance + 1, tmpMov); resultMatrix[tmp[1], tmp[0] + 1].setDistance(2, parentDistance + 2, tmpMov); resultMatrix[tmp[1], tmp[0] + 1].setDistance(3, parentDistance + 2, tmpMov); q.Enqueue(new int[] { tmp[0] + 1, tmp[1] }); } else if (resultMatrix[tmp[1], tmp[0] + 1].getDistance(1) > parentDistance + 1) { resultMatrix[tmp[1], tmp[0] + 1].setDistance(1, parentDistance + 1, resultMatrix[tmp[1], tmp[0]].move[1]); q.Enqueue(new int[] { tmp[0] + 1, tmp[1] }); } } parentDistance = resultMatrix[tmp[1], tmp[0]].getDistance(2); if (tmp[1] < columnsGrid - 1 && !barriers.Contains(arena[tmp[1] + 1, tmp[0]])) { if (resultMatrix[tmp[1] + 1, tmp[0]].getDistance(2) > parentDistance + 2) { if (resultMatrix[tmp[1], tmp[0]].moveTo == -1) { tmpMov = 2; } else { tmpMov = resultMatrix[tmp[1], tmp[0]].move[2]; } resultMatrix[tmp[1] + 1, tmp[0]].setDistance(0, parentDistance + 2, tmpMov); resultMatrix[tmp[1] + 1, tmp[0]].setDistance(1, parentDistance + 2, tmpMov); resultMatrix[tmp[1] + 1, tmp[0]].setDistance(2, parentDistance + 1, tmpMov); resultMatrix[tmp[1] + 1, tmp[0]].setDistance(3, parentDistance + 2, tmpMov); q.Enqueue(new int[] { tmp[0], tmp[1] + 1 }); } else if (resultMatrix[tmp[1] + 1, tmp[0]].getDistance(2) > parentDistance + 1) { resultMatrix[tmp[1] + 1, tmp[0]].setDistance(2, parentDistance + 1, resultMatrix[tmp[1], tmp[0]].move[2]); q.Enqueue(new int[] { tmp[0], tmp[1] + 1 }); } } parentDistance = resultMatrix[tmp[1], tmp[0]].getDistance(3); if (tmp[0] > 0 && !barriers.Contains(arena[tmp[1], tmp[0] - 1])) { if (resultMatrix[tmp[1], tmp[0] - 1].getDistance(3) > parentDistance + 2) { if (resultMatrix[tmp[1], tmp[0]].moveTo == -1) { tmpMov = 3; } else { tmpMov = resultMatrix[tmp[1], tmp[0]].move[3]; } resultMatrix[tmp[1], tmp[0] - 1].setDistance(0, parentDistance + 2, tmpMov); resultMatrix[tmp[1], tmp[0] - 1].setDistance(1, parentDistance + 2, tmpMov); resultMatrix[tmp[1], tmp[0] - 1].setDistance(2, parentDistance + 2, tmpMov); resultMatrix[tmp[1], tmp[0] - 1].setDistance(3, parentDistance + 1, tmpMov); q.Enqueue(new int[] { tmp[0] - 1, tmp[1] }); } else if (resultMatrix[tmp[1], tmp[0] - 1].getDistance(3) > parentDistance + 1) { resultMatrix[tmp[1], tmp[0] - 1].setDistance(3, parentDistance + 1, resultMatrix[tmp[1], tmp[0]].move[3]); q.Enqueue(new int[] { tmp[0] - 1, tmp[1] }); } } } }
public PlayerInfo(int x, int y) { position = new Vector2(x, y); for (int i = 0; i < Game1.columnsGrid; i++) { for (int j = 0; j < Game1.columnsGrid; j++) { distanceMatrix[i, j] = new Cell(); } } }