Esempio n. 1
0
    // Update is called once per frame
    void Update()
    {
        if (talkTime > 0)
        {
            talkTime -= Time.deltaTime;
        }

        if (Talk.activeSelf && talkTime <= 0)
        {
            talkTime = 0;
            Talk.SetActive(false);
        }

        //set to correct look direction when moving towards selected
        if (Selected != null && state == State.Move)
        {
            float scale = gameObject.transform.localScale.x;

            if (scale < 0 && Selected.transform.position.x < gameObject.transform.position.x)
            {
                gameObject.transform.localScale = new Vector3(scale * -1, 60, 1);
            }
            else if (scale > 0 && Selected.transform.position.x >= gameObject.transform.position.x)
            {
                gameObject.transform.localScale = new Vector3(scale * -1, 60, 1);
            }
        }

        //set text bubble to correct direction
        if (gameObject.transform.localScale.x > 0 && Talk.transform.localScale.x < 0)
        {
            Talk.transform.localScale = new Vector3(-Talk.transform.localScale.x, Talk.transform.localScale.y, Talk.transform.localScale.z);

            if (Talk.transform.localPosition == originalTalkPos)
            {
                Talk.transform.localPosition = new Vector3(-originalTalkPos.x, Talk.transform.localPosition.y, Talk.transform.localPosition.z);
            }
        }
        else if (gameObject.transform.localScale.x < 0 && Talk.transform.localScale.x > 0)
        {
            Talk.transform.localScale = new Vector3(-Talk.transform.localScale.x, Talk.transform.localScale.y, Talk.transform.localScale.z);

            if (Talk.transform.localPosition != originalTalkPos)
            {
                Talk.transform.localPosition = originalTalkPos;
            }
        }

        switch (state)
        {
        case State.Idle:
            Sounds.CatStop();
            OriginalTexture();
            WalkLines.SetActive(false);
            gameObject.transform.localScale = new Vector3(-60, 60, 1);
            break;

        case State.Move:
            if (!Sounds.CatSource.isPlaying)
            {
                Sounds.CatWalking();
            }

            WalkLines.SetActive(true);

            gameObject.GetComponent <Animator>().SetBool("Move", true);

            gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, Selected.transform.position, speed * Time.deltaTime);

            if (gameObject.GetComponent <Collider>().bounds.Contains(Selected.transform.position) && Selected.tag == "Food")
            {
                Selected.GetComponent <Food>().held = true;
                GrabFood();
            }

            break;

        case State.Return:
            float magnitude = (originalPos - gameObject.transform.position).magnitude;

            if (!Sounds.CatSource.isPlaying)
            {
                Sounds.CatWalking();
            }

            //set correct look direction when returning to original position
            if (originalPos.x < gameObject.transform.position.x)
            {
                gameObject.transform.localScale = new Vector3(60, 60, 1);
            }
            else
            {
                gameObject.transform.localScale = new Vector3(-60, 60, 1);
            }

            WalkLines.SetActive(true);

            if (magnitude > 0.5)
            {
                gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, originalPos, speed * Time.deltaTime);
            }
            else
            {
                gameObject.transform.position = originalPos;
                gameObject.GetComponent <Animator>().SetBool("Move", false);
                Selected = null;
                state    = State.Idle;
            }
            break;

        case State.Twitch:
            Sounds.CatStop();
            OriginalTexture();
            WalkLines.SetActive(false);
            gameObject.GetComponent <Animator>().SetTrigger("Twitch");
            gameObject.GetComponent <Animator>().SetBool("Move", false);
            if (gameObject.transform.position != originalPos)
            {
                if (gameObject.GetComponent <Animator>().GetCurrentAnimatorStateInfo(0).IsName("Twitch"))
                {
                    wait = true;
                }
                else
                {
                    if (wait && gameObject.GetComponent <Animator>().GetCurrentAnimatorStateInfo(0).IsName("Idle"))
                    {
                        wait  = false;
                        state = State.Return;
                    }
                }
            }
            else
            {
                state = State.Idle;
            }
            break;

        case State.Reach:
            Sounds.CatStop();
            WalkLines.SetActive(false);
            gameObject.GetComponent <Animator>().SetTrigger("Reach");
            if (gameObject.GetComponent <Animator>().GetCurrentAnimatorStateInfo(0).IsName("Reach"))
            {
                reach = true;
            }
            else if (reach && gameObject.GetComponent <Animator>().GetCurrentAnimatorStateInfo(0).IsName("Walk"))
            {
                Destroy(Held);
                reach = false;
                state = State.Return;
            }
            break;
        }
    }