Example #1
0
 void Start()
 {
     _aiController                = GetComponent <BaseAIController>();
     _sight                       = GetComponent <SightBehaviour>();
     _sight.PlayerSeen           += OnPlayerSeen;
     _aiController.IsDoneChasing += OnDoneChasing;
     _aiController.Deadened      += OnDeath;
 }
Example #2
0
        public void ResolvePath(Vector3 realWorldPosition, Vector3 realWorldTarget,
                                BaseAIController requestingController)
        {
            var open   = new List <NavNode>();
            var closed = new List <NavNode>();

            Vector2Int?pseudoGridPosition = GetPseudoPosition(realWorldPosition);
            Vector2Int?pseudoTarget       = GetPseudoPosition(realWorldTarget);

            if (!pseudoGridPosition.HasValue)
            {
                throw new Exception($"Controller {requestingController.gameObject.name} requested destination to tile position {pseudoTarget} but current position of agent is {requestingController.transform.position} which is invalid!");
            }
            if (!pseudoTarget.HasValue)
            {
                throw new Exception($"Controller {requestingController.gameObject.name} requested destination to real world position {realWorldTarget} but that is out of range of the grid!");
            }

            var node = new NavNode(pseudoGridPosition.Value, realWorldPosition, pseudoTarget.Value);

            open.Add(node);
            var routeFound = false;

            while (open.Count > 0)
            {
                node = open.OrderBy(x => x.FinalScore).First();
                closed.Add(node);
                open.Remove(node);

                if (closed.Any(x => x.PseudoPosition == pseudoTarget.Value))
                {
                    routeFound = true;
                    break;
                }

                List <Vector2Int> adjacentNodePoints = GetAdjacentNodePoints(node.PseudoPosition);

                foreach (var point in adjacentNodePoints)
                {
                    if (closed.Any(x => x.PseudoPosition == point))
                    {
                        continue;
                    }
                    if (PseudoGrid[point.x, point.y].HasValue && open.All(x => x.PseudoPosition != point))
                    {
                        open.Add(new NavNode(point, PseudoGrid[point.x, point.y].Value, pseudoTarget.Value, node));
                    }
                    else if (PseudoGrid[point.x, point.y].HasValue)
                    {
                        var testNode = new NavNode(point, PseudoGrid[point.x, point.y].Value, pseudoTarget.Value, node);
                        if (testNode.FinalScore < open.First(x => x.PseudoPosition == point).FinalScore)
                        {
                            open.First(x => x.PseudoPosition == point).Parent = node;
                        }
                    }
                }
                //yield return null;
            }

            if (!routeFound)
            {
                throw new Exception(
                          $"Somehow, {requestingController.gameObject.name} made a malformed request that escaped the algorithm.");
            }

            var realWorldPositions = new List <Vector3>();

            var destinationNode = closed.First(x => x.PseudoPosition == pseudoTarget);

            ResolveParentChain(destinationNode, realWorldPositions);
            realWorldPositions.Reverse();
            requestingController.Route = realWorldPositions;
//            Debug.Log("WORLD ROUTE:");
//            foreach (var worldPosition in realWorldPositions)
//            {
//                Debug.Log(worldPosition);
//            }
        }