예제 #1
0
    /*
     * void updateAI(){
     *      switch (state) {
     *      case AIState.Idle:
     *              // Continually move towards target location while not at target.
     *              // Idle for 1.5-3 seconds, then seek out another target.
     *              if (state == AIState.Idle && transform.position == targetPos) {
     *                      state = AIState.Seek;
     *              }
     *              break;
     *      case AIState.Seek:
     *              // By default: search for nearby person. If low mood: search for nearby happy person.
     *              // Else: go randomly.
     *
     *              // Case 1: Mood is not low. Search for a nearby person.
     *              // Case 2: Mood is low. Search for nearby happy person.
     *              // Case 3: Nobody is nearby. Move within random location.
     *
     *              //RaycastHit2D[] nearbyPeople = Physics2D.CircleCastAll (transform.position, 3.0f, Vector2.zero, 0.0f, LayerMask.NameToLayer ("NPC"));
     *              Collider2D[] nearbyPeople = Physics2D.OverlapCircleAll(transform.position, 3f, LayerMask.NameToLayer("NPC"));
     *
     *
     *              if (nearbyPeople.Length != 0) {
     *                      targetPos = nearbyPeople[0].transform.position;
     *                      Debug.Log ("Nearby person found!!");
     *              } else {
     *                      targetPos.x = (transform.position.x + Random.Range (-1.5f, 1.5f));
     *                      targetPos.y = (transform.position.z + Random.Range (-1.5f, 1.5f));
     *                      Debug.Log ("Random position!");
     *              }
     *              state = AIState.Idle;
     *              time = Random.Range (2.5f, 3.25f);
     *
     *              break;
     *      case AIState.Talk:
     *              // Placeholder for now.
     *              break;
     *      case AIState.Enforce:
     *              // Essentially an uber-seek. Stay ultra close to a target.
     *              Character[] charListArray = FindObjectsOfType<Character> ();
     *              List<Character> charList = new List<Character> (charListArray);
     *
     *              foreach (Character target in charList) {
     *                      if (target.type == Character.CharacterClass.normie && target.mood > -6) {
     *                              targetPos.x = target.transform.position.x + Random.Range(-0.5f, 0.5f);
     *                              targetPos.y = target.transform.position.y + Random.Range(-0.5f, 0.5f);
     *                              time = Random.Range (0.5f, 1.5f);
     *                      }
     *              }
     *              break;
     *      default:
     *              break;
     *      }
     *
     * }*/

    void FindPath(Vector3 startPos, Vector3 targetPos)
    {
        NavGrid.Node startNode  = navmesh.NodeFromWorldPoint(startPos);
        NavGrid.Node targetNode = navmesh.NodeFromWorldPoint(targetPos);

        List <NavGrid.Node>    openSet   = new List <NavGrid.Node>();
        HashSet <NavGrid.Node> closedSet = new HashSet <NavGrid.Node>();

        openSet.Add(startNode);

        while (openSet.Count > 0)
        {
            NavGrid.Node node = openSet[0];
            for (int i = 1; i < openSet.Count; i++)
            {
                if (openSet[i].f < node.f || openSet[i].f == node.f)
                {
                    if (openSet[i].h < node.h)
                    {
                        node = openSet[i];
                    }
                }
            }

            openSet.Remove(node);
            closedSet.Add(node);

            if (node == targetNode)
            {
                RetracePath(startNode, targetNode);
                return;
            }

            foreach (NavGrid.Node neighbour in navmesh.GetNeighbors(node))
            {
                if (!neighbour.walkable || closedSet.Contains(neighbour))
                {
                    continue;
                }

                int newCostToNeighbour = node.g + GetDistance(node, neighbour);
                if (newCostToNeighbour < neighbour.g || !openSet.Contains(neighbour))
                {
                    neighbour.g      = newCostToNeighbour;
                    neighbour.h      = GetDistance(neighbour, targetNode);
                    neighbour.parent = node;

                    if (!openSet.Contains(neighbour))
                    {
                        openSet.Add(neighbour);
                    }
                }
            }
        }
    }
예제 #2
0
    IEnumerator CreatePath(Vector3 startPos, Vector3 targetPos)
    {
        NavNode startNode  = grid.GetNodeFromWorld(startPos);
        NavNode targetNode = grid.GetNodeFromWorld(targetPos);

        Vector3[] waypoints   = new Vector3[0];
        bool      pathSuccess = false;

        Debug.Log("Creating Path");
        if (startNode.walkable && targetNode.walkable)
        {
            Debug.Log("Creating Path inside first check");
            Heap <NavNode>    openSet   = new Heap <NavNode>(grid.MaxSize);
            HashSet <NavNode> closedSet = new HashSet <NavNode>();
            openSet.Add(startNode);

            while (openSet.Count > 0)
            {
                NavNode currentNode = openSet.RemoveFirst();
                closedSet.Add(currentNode);

                if (currentNode == targetNode)
                {
                    Debug.Log("Path Found!");
                    pathSuccess = true;
                    break;
                }

                foreach (NavNode neighbor in grid.GetNeighbors(currentNode))
                {
                    if (!neighbor.walkable || closedSet.Contains(neighbor))
                    {
                        continue;
                    }

                    int newMovementCostToNeighbor = currentNode.gCost + GetDistance(currentNode, neighbor);
                    if (newMovementCostToNeighbor < neighbor.gCost || !openSet.Contains(neighbor))
                    {
                        neighbor.gCost  = newMovementCostToNeighbor;
                        neighbor.hCost  = GetDistance(neighbor, targetNode);
                        neighbor.parent = currentNode;

                        if (!openSet.Contains(neighbor))
                        {
                            openSet.Add(neighbor);
                        }
                        else
                        {
                            openSet.UpdateItem(neighbor);
                        }
                    }
                }
            }
        }
        yield return(null);

        if (pathSuccess)
        {
            waypoints = RetracePath(startNode, targetNode);
        }
        PathRequestManager.Instance.ProcessingComplete(waypoints, pathSuccess);
    }