Ejemplo n.º 1
0
        public void SetEnd(int endX, int endY, HallwayType[,] map)
        {
            float dX = endX - x;
            float dY = endY - y;

            this.endX = endX;
            this.endY = endY;

            baseCost = 1;
            if (x > 0 && (map[x - 1, y] != HallwayType.None && map[x - 1, y] != HallwayType.Invalid))
            {
                baseCost = 100;
            }
            else if (y > 0 && (map[x, y - 1] != HallwayType.None && map[x, y - 1] != HallwayType.Invalid))
            {
                baseCost = 100;
            }
            else if (x + 1 < map.GetLength(0) && (map[x + 1, y] != HallwayType.None && map[x + 1, y] != HallwayType.Invalid))
            {
                baseCost = 100;
            }
            else if (y + 1 < map.GetLength(1) && (map[x, y + 1] != HallwayType.None && map[x, y + 1] != HallwayType.Invalid))
            {
                baseCost = 100;
            }

            cost = baseCost;

            minCostToStart         = null;
            manhattenDistanceToEnd = Mathf.Abs(dX + dY);
            nearestToStart         = null;
        }
Ejemplo n.º 2
0
 private void BuildShortestPath(List <HallwayPoint> list, HallwayPoint node)
 {
     if (node.nearestToStart == null)
     {
         return;
     }
     list.Add(node.nearestToStart);
     BuildShortestPath(list, node.nearestToStart);
 }
Ejemplo n.º 3
0
        public void CalculateCost(HallwayPoint from)
        {
            var extraCost = 0;

            if (from.nearestToStart != null)
            {
                if (DirectionFrom(from) != from.DirectionFrom(from.nearestToStart))
                {
                    extraCost = 100;
                }
            }
            cost = baseCost + extraCost;
        }
Ejemplo n.º 4
0
    public HallwayGenerator(HallwayType[,] map)
    {
        this.map = map;
        width    = map.GetLength(0);
        height   = map.GetLength(1);

        points = new HallwayPoint[width, height];
        for (int x = 0; x < map.GetLength(0); x++)
        {
            for (int y = 0; y < map.GetLength(1); y++)
            {
                points[x, y] = new HallwayPoint(x, y);
            }
        }
    }
Ejemplo n.º 5
0
 private int DirectionFrom(HallwayPoint from)
 {
     if (x > from.x)
     {
         return(0);
     }
     else if (x < from.x)
     {
         return(1);
     }
     else if (y < from.y)
     {
         return(2);
     }
     else
     {
         return(3);
     }
 }
Ejemplo n.º 6
0
    private void AStarSearch(HallwayPoint start, HallwayPoint end)
    {
        bool[,] visited = new bool[width, height];

        start.minCostToStart = 0;
        var priorityQueue = new List <HallwayPoint> {
            start
        };

        do
        {
            priorityQueue = priorityQueue.OrderBy(x => x.minCostToStart + x.manhattenDistanceToEnd).ToList();
            var hallwayPoint = priorityQueue.First();
            priorityQueue.Remove(hallwayPoint);
            hallwayPoint.ConnectedHallwayPoints(map, points).ForEach(p => p.CalculateCost(hallwayPoint));

            foreach (var connectedHallwayPoint in hallwayPoint.ConnectedHallwayPoints(map, points).OrderBy(x => x.cost).ToList())
            {
                if (visited[connectedHallwayPoint.x, connectedHallwayPoint.y])
                {
                    continue;
                }
                if (connectedHallwayPoint.minCostToStart == null ||
                    hallwayPoint.minCostToStart + connectedHallwayPoint.cost < connectedHallwayPoint.minCostToStart)
                {
                    connectedHallwayPoint.minCostToStart = hallwayPoint.minCostToStart + connectedHallwayPoint.cost;
                    connectedHallwayPoint.nearestToStart = hallwayPoint;
                    if (!priorityQueue.Contains(connectedHallwayPoint))
                    {
                        priorityQueue.Add(connectedHallwayPoint);
                    }
                }
            }
            visited[hallwayPoint.x, hallwayPoint.y] = true;
            if (hallwayPoint.x == end.x && hallwayPoint.y == end.y)
            {
                return;
            }
        } while (priorityQueue.Any());
    }