Newer
Older
Dreamsturbia-Project-IADE-Unity3D / Assets / Scripts / Player / CharacterAnimationController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterAnimationController : MonoBehaviour
{
    private Animator animator;
    public float dumpTime = 0.1f;

    private CharacterMovement characterMovement;
    private PlayerController playerController;
    private Health healthScript;

    private float horizontal, vertical;

    // Start is called before the first frame update
    void Start()
    {
        characterMovement = GetComponent<CharacterMovement>();
        playerController = GetComponent<PlayerController>();
        healthScript = GetComponent<Health>();
        animator = GetComponentInChildren<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        horizontal = (Input.GetAxis("Horizontal"));
        vertical = (Input.GetAxis("Vertical"));
        
        //Movement
        Idle();
        Run();
        Jump();
        Injured();

        //Abilities
        PushAndPull();
        Dash();

        //Combat
        StartCoroutine(Attack());
        StartCoroutine(Parry());

        LooseHealth();
        
        //Debug.Log(characterMovement.OnGround());
        SecretDance();
    }


    private void Idle()
    {
        if (horizontal < 0f || vertical < 0f)
        {
            animator.SetFloat("Speed", 0f);
        }
    }


    private void Run()
    {

        if(animator.GetFloat("Speed") != Mathf.Abs(horizontal) + Mathf.Abs(vertical))
        animator.SetFloat("Speed", Mathf.Abs(horizontal) + Mathf.Abs(vertical), dumpTime, Time.deltaTime);
        
    }

    private void Jump()
    {
        if (Input.GetKeyDown(KeyCode.Space) && characterMovement.numberOfJumps < 1)
        {
            animator.SetTrigger("Jump");
        }
    }

  /*  private void DoubleJump()
    {
        if (Input.GetKeyDown(KeyCode.Space) && characterMovement.numberOfJumps > 1)
        {
            animator.SetTrigger("DoubleJump");
        }

    }*/

    private void Dash()
    {
        if (Input.GetKeyDown(playerController.abilities[2].code))
        {
            //Dash
            //animator.SetBool("Dash", true);
            animator.SetTrigger("Dash");
        }
    }


    private void PushAndPull()
    {
        if (!playerController.intention)
        {
            if (Input.GetKey(playerController.abilities[3].code) || Input.GetKey(playerController.abilities[4].code))
            {
                animator.SetBool("PushAndPull", true);
                animator.SetLayerWeight(animator.GetLayerIndex("PushAndPull Layer"), 1);
            }
            else
            animator.SetBool("PushAndPull", false);
        }
    }

    private void Injured()
    {
        if (healthScript.hp <= healthScript.totalHealth / 2)
        {
            animator.SetLayerWeight(animator.GetLayerIndex("Injured Layer"), 1);

        }
        else
            animator.SetLayerWeight(animator.GetLayerIndex("Injured Layer"), 0);
    }

    private IEnumerator Parry()
    {
        if (playerController.intention)
        {
            if (Input.GetKeyDown(playerController.abilities[5].code))
            {
                //Attack
                animator.SetLayerWeight(animator.GetLayerIndex("Parry Layer"), 1);
                animator.SetTrigger("Parry");

                yield return new WaitForSeconds(0.9f);
            }
        }

    }

    private void LooseHealth()
    {
        if (Input.GetKeyDown("l"))
        {
            healthScript.hp -= 10f;
        }

        if (Input.GetKey("k"))
        {
            healthScript.hp = healthScript.totalHealth;
        }
    }

    private IEnumerator Attack()
    {
        if (playerController.intention)
        {
            if (Input.GetKeyDown(playerController.abilities[0].code))
            {
                //Attack
                animator.SetLayerWeight(animator.GetLayerIndex("Attack Layer"), 1);
                animator.SetTrigger("Attack");

                yield return new WaitForSeconds(0.9f);
            }
        }

    }


    private void SecretDance()
    {
        if (Input.GetKeyDown("t"))
        {
            animator.SetTrigger("Dance");
        }
    }

    private void GettingHit()
    {

    }

}