Beispiel #1
0
        // Checks if Diagonal move is possible/not blocked
        public bool IsDiagonalPathBlocked(Coords originCoords, Coords destinationCoords, LocationModel unitLocationModel)
        {
            CoordsCalculator coordsCalculator = new CoordsCalculator((FloatCoords)originCoords);

            if (!coordsCalculator.GetNeighbours().Contains((FloatCoords)destinationCoords))
            {
                throw new ArgumentException("destinationCoords must be neighbouring originCoords");
            }

            //checks if coords are next to each other
            if (coordsCalculator.GetNonDiagonalNeighbours().Contains((FloatCoords)destinationCoords))
            {
                return(false);
            }

            Coords commonNeighbour1 = new Coords
            {
                x = originCoords.x,
                y = destinationCoords.y
            };

            Coords commonNeighbour2 = new Coords
            {
                x = destinationCoords.x,
                y = originCoords.y
            };

            WorldCellController cell1 = worldController.GetCellFromCoords(commonNeighbour1);
            WorldCellController cell2 = worldController.GetCellFromCoords(commonNeighbour2);

            //Checks if both coords are blocked by an obstacle
            return(cell1 != null && IsCellImpassable(cell1.worldCellModel, unitLocationModel) ||
                   cell2 != null && IsCellImpassable(cell2.worldCellModel, unitLocationModel));
        }
Beispiel #2
0
        // sets the weight-values of all the neighbours of a cell
        private Dictionary <Coords, CellWeightValues> CalculateNeighboursWeights(Coords currentCell, Coords targetCoords, LocationModel unit)
        {
            CoordsCalculator coordsCalculator = new CoordsCalculator((FloatCoords)currentCell);

            FloatCoords[] neighbours = new FloatCoords[8];

            coordsCalculator.GetNeighbours().CopyTo(neighbours, 0);

            return((from neighbour in neighbours
                    where worldController.GetCellFromCoords((Coords)neighbour) != null &&
                    !IsCellImpassable(worldController.GetCellFromCoords((Coords)neighbour).worldCellModel, unit)
                    let cellWeights = CalculateCellWeights((Coords)neighbour, unit.Coords, targetCoords)
                                      select new KeyValuePair <Coords, CellWeightValues>((Coords)neighbour, cellWeights)).ToDictionary(pair => pair.Key, pair => pair.Value));
        }