private void Walk()
    {
        Vector3 dir = path[0].transform.position + path[0].offset - transform.position;

        dir.Normalize();

        GetComponent <SpriteRenderer>().flipX = dir.x < 0f;

        transform.position += dir / 6f;
        if (Vector3.Distance(transform.position, path[0].transform.position + path[0].offset) < 1f)
        {
            if (path[0] == target)
            {
                if (target.type == NodeType.standard)
                {
                    action = CatAction.idle;
                }
                else
                {
                    action    = CatAction.busy;
                    busyTimer = 1f;
                }
            }
            current = path[0];
            path.RemoveAt(0);
        }
    }
    void moveToNewSpot()
    {
        currentMood = mood.moving;
        CatNode toMoveTo = null;

        reshuffleNodes();
        for (int i = 0; i < catNodes.Length; i++)
        {
            CatNode tempNode = catNodes[i];
            if (tempNode.canSit())
            {
                toMoveTo = tempNode;
                break;
            }
        }
        if (toMoveTo != null)
        {
            if (currentNode != null)
            {
                currentNode.getLeft();
            }
            currentNode = toMoveTo;
            StopAllCoroutines();
            StartCoroutine(moveToSpot(currentNode));
            currentNode.getSatOn();
            resetTimer();
        }
    }
    private void Decide()
    {
        switch (type)
        {
        case NodeType.pee:
            target = FindTarget(NodeType.pee);
            break;

        case NodeType.food:
            target = FindTarget(NodeType.food);
            break;

        case NodeType.sleep:
            target = FindTarget(NodeType.sleep);
            break;

        default:
            target = FindTarget(NodeType.standard);
            break;
        }
        if (current != target)
        {
            FindPath(current, target);
            action = CatAction.walking;
        }
    }
示例#4
0
        public object Visit(CatNode node, object value)
        {
            foreach (var child in node.Children)
            {
                child.Accept(this, null);
            }

            return(this);
        }
 void reshuffleNodes()
 {
     // Knuth shuffle algorithm :: courtesy of Wikipedia :)
     for (int t = 0; t < catNodes.Length; t++)
     {
         CatNode tmp = catNodes[t];
         int     r   = Random.Range(t, catNodes.Length);
         catNodes[t] = catNodes[r];
         catNodes[r] = tmp;
     }
 }
    public void FindPath(CatNode start, CatNode target)
    {
        List <CatNode>    openSet   = new List <CatNode>();
        HashSet <CatNode> closedSet = new HashSet <CatNode>();

        openSet.Add(start);

        while (openSet.Count > 0)
        {
            CatNode currentNode = openSet[0];
            for (int i = 1; i < openSet.Count; i++)
            {
                if (openSet[i].fCost < currentNode.fCost || openSet[i].fCost == currentNode.fCost && openSet[i].hCost < currentNode.hCost)
                {
                    currentNode = openSet[i];
                }
            }

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

            if (currentNode == target)
            {
                RetracePath(start, target);
                return;
            }

            foreach (CatNode neighbor in currentNode.neighbors)
            {
                if (closedSet.Contains(neighbor))
                {
                    continue;
                }

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

                    if (!openSet.Contains(neighbor))
                    {
                        openSet.Add(neighbor);
                    }
                }
            }
        }
    }
    private void RetracePath(CatNode start, CatNode end)
    {
        List <CatNode> path        = new List <CatNode>();
        CatNode        currentNode = end;

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

        path.Reverse();

        this.path = path;
    }
    IEnumerator moveToSpot(CatNode node)
    {
        animator.ResetTrigger("chill");
        animator.SetTrigger("run");
        float scale = Mathf.Abs(transform.localScale.x);

        while (transform.position != node.floorPoint.transform.position)
        {
            transform.position = Vector3.MoveTowards(transform.position, node.floorPoint.transform.position, 0.1f);
            Vector2 dist = transform.position - node.floorPoint.transform.position;
            scale = Mathf.MoveTowards(scale, 1, 0.01f);
            flipSprite(dist, scale);
            if (dist.magnitude <= 0.1f)
            {
                transform.position = node.floorPoint.transform.position;
                animator.ResetTrigger("chill");
                animator.SetTrigger("jump");
            }
            yield return(new WaitForEndOfFrame());
        }
        scale = Mathf.Abs(transform.localScale.x);
        while (transform.position != node.gameObject.transform.position)
        {
            transform.position = Vector3.MoveTowards(transform.position, node.gameObject.transform.position, 0.1f);
            scale = Mathf.MoveTowards(scale, node.scaleFactor, 0.01f);
            Vector2 dist = transform.position - node.gameObject.transform.position;
            flipSprite(dist, scale);
            if (dist.magnitude <= 0.1f)
            {
                transform.position = node.gameObject.transform.position;
                animator.SetTrigger("chill");
            }
            yield return(new WaitForEndOfFrame());
        }
        currentMood = mood.sitting;
    }
 private float GetDistance(CatNode a, CatNode b)
 {
     return(Vector3.Distance(a.transform.position, a.transform.position));
 }
示例#10
0
 public int Visit(CatNode node)
 {
     return(node.Children.Sum(child => child.Accept(this)));
 }
示例#11
0
 public object Visit(CatNode node, int posOffset)
 {
     FillFollowPos(posOffset, positions, node.Children);
     return(this);
 }
示例#12
0
 public IntSet Visit(CatNode node, int posOffset)
 {
     return(LastPos(posOffset, node.Children));
 }
示例#13
0
 public bool Visit(CatNode node)
 {
     return(node.Children.All(child => child.Accept(this)));
 }