示例#1
0
    //occurs every frame
    //src: http://michaelcummings.net/mathoms/creating-2d-animated-sprites-using-unity-4.3
    void Update()
    {
        //dev console input check
        if (Input.GetKeyDown(KeyCode.BackQuote))
        {
            devConsoleEnabled = !devConsoleEnabled;

            Time.timeScale = devConsoleEnabled ? 0f : 1f;
            devConsole.SetActive(devConsoleEnabled);
            if (devConsoleEnabled)
            {
                devConsole.GetComponent <UnityEngine.UI.InputField>().ActivateInputField();
            }
            lockAI += (devConsoleEnabled ? 1 : -1);
        }


        if (IsActive())
        {
            //animator contorl based on input
            animator.SetBool("Walking", true);
            if (input.y > 0)            //up
            {
                facing = Vector2.up;
                animator.SetInteger("Direction", 0);
                attackHitboxOffset.x = 0;
                attackHitboxOffset.y = 0.5f;
            }
            else if (input.y < 0)     //down
            {
                facing = Vector2.down;
                animator.SetInteger("Direction", 1);
                attackHitboxOffset.x = 0;
                attackHitboxOffset.y = -0.5f;
            }
            else if (input.x > 0)     //right
            {
                facing = Vector2.right;
                animator.SetInteger("Direction", 3);
                attackHitboxOffset.x = 0.5f;
                attackHitboxOffset.y = -0.25f;
            }
            else if (input.x < 0)     //left
            {
                facing = Vector2.left;
                animator.SetInteger("Direction", 2);
                attackHitboxOffset.x = -0.5f;
                attackHitboxOffset.y = -0.25f;
            }
            else
            {
                animator.SetBool("Walking", false);
                //            int dir = animator.GetInteger("Direction"); //commented lines are for old animation controller
                //            if(dir == 0) //idle down
                //                animator.SetInteger("Direction", 4);
                //            else if (dir == 1) //idle right
                //                animator.SetInteger("Direction", 5);
                //            else if (dir == 2) //idle up
                //                animator.SetInteger("Direction", 6);
                //            else if (dir == 3) // idle left
                //                animator.SetInteger("Direction", 7);
            }
            if (Input.GetButtonDown("Attack") && !attacking)
            {
                attacking   = true;
                attackTimer = attackTime;
                attackHitbox.transform.localPosition = attackHitboxOffset;
            }

            //TODO crappy way of doing this; I'm too lazy to make extensive changes to the controller untiol we know for sure what we're doing with these animations
            animator.SetBool("Attacking", attacking || casting);


            // Used for inventory stuff
            if (Input.GetKeyDown(KeyCode.I))             // opens inventory
            {
                Debug.Log("I key pressed");
                inventory.Open();
            }
            // use specific items based on which num is used
            if (Input.GetKeyDown(KeyCode.Keypad1) || Input.GetKeyDown(KeyCode.Alpha1))
            {
                Debug.Log("1 is Pressed");
                inventory.UseItem(0);
            }
            if (Input.GetKeyDown(KeyCode.Keypad2) || Input.GetKeyDown(KeyCode.Alpha2))
            {
                Debug.Log("2 is Pressed");
                inventory.UseItem(1);
            }
            if (Input.GetKeyDown(KeyCode.Keypad3) || Input.GetKeyDown(KeyCode.Alpha3))
            {
                Debug.Log("3 is Pressed");
                inventory.UseItem(2);
            }
            if (Input.GetKeyDown(KeyCode.Keypad4) || Input.GetKeyDown(KeyCode.Alpha4))
            {
                Debug.Log("4 is Pressed");
                inventory.UseItem(3);
            }
            if (Input.GetKeyDown(KeyCode.Keypad5) || Input.GetKeyDown(KeyCode.Alpha5))
            {
                Debug.Log("5 is Pressed");
                inventory.UseItem(4);
            }
            if (Input.GetKeyDown(KeyCode.Keypad6) || Input.GetKeyDown(KeyCode.Alpha6))
            {
                Debug.Log("6 is Pressed");
                inventory.UseItem(5);
            }
            if (Input.GetKeyDown(KeyCode.Keypad7) || Input.GetKeyDown(KeyCode.Alpha7))
            {
                Debug.Log("7 is Pressed");
                inventory.UseItem(6);
            }
            if (Input.GetKeyDown(KeyCode.Keypad8) || Input.GetKeyDown(KeyCode.Alpha8))
            {
                Debug.Log("8 is Pressed");
                inventory.UseItem(7);
            }
            if (Input.GetKeyDown(KeyCode.Keypad9) || Input.GetKeyDown(KeyCode.Alpha9))
            {
                Debug.Log("9 is Pressed");
                inventory.UseItem(8);
            }
            if (Input.GetKeyDown(KeyCode.Keypad0) || Input.GetKeyDown(KeyCode.Alpha0))
            {
                Debug.Log("0 is Pressed");
                inventory.UseItem(9);
            }
            // end hot bar keys
            // chest and interact key
            if (Input.GetKeyDown(KeyCode.E))
            {
                if (chest != null)
                {
                    chest.Open();
                }
                if (shop != null)
                {
                    shop.Open();
                }
            }
        }


        //timers continue regardless of whether the player is active or not
        if (attacking)
        {
            if (attackTimer > 0)
            {
                attackTimer -= Time.deltaTime;
            }
            else
            {
                attacking = false;
            }
        }

        if (casting)
        {
            if (castTimer > 0)
            {
                castTimer -= Time.deltaTime;
            }
            else
            {
                animator.speed = 1f;
                animator.SetBool("Casting", false);
                casting = false;
            }
        }

        //attack hitbox should not stay on if frozen; status updated outside IsActive block
        attackHitbox.isActive = attacking && this.IsActive();

        if (bombTimer >= 0)
        {
            bombTimer -= Time.deltaTime;
        }

        if (iceTimer >= 0)
        {
            iceTimer -= Time.deltaTime;
        }

        if (pushTimer >= 0)
        {
            pushTimer -= Time.deltaTime;
        }
    }