Exemple #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);
                    }
                }
            }
        }
    }
Exemple #2
0
    int GetDistance(NavGrid.Node nodeA, NavGrid.Node nodeB)
    {
        int dstX = (int)Mathf.Abs(nodeA.grid.x - nodeB.grid.x);
        int dstY = (int)Mathf.Abs(nodeA.grid.y - nodeB.grid.y);

        if (dstX > dstY)
        {
            return(14 * dstY + 10 * (dstX - dstY));
        }
        return(14 * dstX + 10 * (dstY - dstX));
    }
        public static Bitmap Render(NavGrid navgrid, Gradient gradient, int upscale = 1)
        {
            Bitmap result      = new Bitmap(navgrid.Width, navgrid.Height);
            float  maxPathCost = navgrid.MaxPathCost;

            Render(result, (x, y) =>
            {
                NavGrid.Node node = navgrid[x, y];
                return((node.PathCost <= maxPathCost) ? gradient(node.PathCost, maxPathCost) : Color.Black);
            });
            return(Upscale(result, upscale));
        }
Exemple #4
0
    void RetracePath(NavGrid.Node startNode, NavGrid.Node endNode)
    {
        List <NavGrid.Node> path = new List <NavGrid.Node>();

        NavGrid.Node currentNode = endNode;

        while (currentNode != startNode)
        {
            path.Add(currentNode);
            currentNode = currentNode.parent;
        }
        path.Reverse();

        navmesh.path = path;
        this.path    = path;
    }