Newer
Older
Dreamsturbia-Project-IADE-Unity3D / Assets / Scripts / Player / CharacterAnimationController.cs
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class CharacterAnimationController : MonoBehaviour
  5. {
  6. private Animator animator;
  7. public float dumpTime = 0.1f;
  8. private CharacterMovement characterMovement;
  9. private PlayerController playerController;
  10. private Health healthScript;
  11. private float horizontal, vertical;
  12. // Start is called before the first frame update
  13. void Start()
  14. {
  15. characterMovement = GetComponent<CharacterMovement>();
  16. playerController = GetComponent<PlayerController>();
  17. healthScript = GetComponent<Health>();
  18. animator = GetComponentInChildren<Animator>();
  19. }
  20. // Update is called once per frame
  21. void Update()
  22. {
  23. horizontal = (Input.GetAxis("Horizontal"));
  24. vertical = (Input.GetAxis("Vertical"));
  25. //Movement
  26. Idle();
  27. Run();
  28. Jump();
  29. Injured();
  30. //Abilities
  31. PushAndPull();
  32. Dash();
  33. //Combat
  34. StartCoroutine(Attack());
  35. StartCoroutine(Parry());
  36. LooseHealth();
  37. //Debug.Log(characterMovement.OnGround());
  38. SecretDance();
  39. }
  40. private void Idle()
  41. {
  42. if (horizontal < 0f || vertical < 0f)
  43. {
  44. animator.SetFloat("Speed", 0f);
  45. }
  46. }
  47. private void Run()
  48. {
  49. if(animator.GetFloat("Speed") != Mathf.Abs(horizontal) + Mathf.Abs(vertical))
  50. animator.SetFloat("Speed", Mathf.Abs(horizontal) + Mathf.Abs(vertical), dumpTime, Time.deltaTime);
  51. }
  52. private void Jump()
  53. {
  54. if (Input.GetKeyDown(KeyCode.Space) && characterMovement.numberOfJumps < 1)
  55. {
  56. animator.SetTrigger("Jump");
  57. }
  58. }
  59. /* private void DoubleJump()
  60. {
  61. if (Input.GetKeyDown(KeyCode.Space) && characterMovement.numberOfJumps > 1)
  62. {
  63. animator.SetTrigger("DoubleJump");
  64. }
  65. }*/
  66. private void Dash()
  67. {
  68. if (Input.GetKeyDown(playerController.abilities[2].code))
  69. {
  70. //Dash
  71. //animator.SetBool("Dash", true);
  72. animator.SetTrigger("Dash");
  73. }
  74. }
  75. private void PushAndPull()
  76. {
  77. if (!playerController.intention)
  78. {
  79. if (Input.GetKey(playerController.abilities[3].code) || Input.GetKey(playerController.abilities[4].code))
  80. {
  81. animator.SetBool("PushAndPull", true);
  82. animator.SetLayerWeight(animator.GetLayerIndex("PushAndPull Layer"), 1);
  83. }
  84. else
  85. animator.SetBool("PushAndPull", false);
  86. }
  87. }
  88. private void Injured()
  89. {
  90. if (healthScript.hp <= healthScript.totalHealth / 2)
  91. {
  92. animator.SetLayerWeight(animator.GetLayerIndex("Injured Layer"), 1);
  93. }
  94. else
  95. animator.SetLayerWeight(animator.GetLayerIndex("Injured Layer"), 0);
  96. }
  97. private IEnumerator Parry()
  98. {
  99. if (playerController.intention)
  100. {
  101. if (Input.GetKeyDown(playerController.abilities[5].code))
  102. {
  103. //Attack
  104. animator.SetLayerWeight(animator.GetLayerIndex("Parry Layer"), 1);
  105. animator.SetTrigger("Parry");
  106. yield return new WaitForSeconds(0.9f);
  107. }
  108. }
  109. }
  110. private void LooseHealth()
  111. {
  112. if (Input.GetKeyDown("l"))
  113. {
  114. healthScript.hp -= 10f;
  115. }
  116. if (Input.GetKey("k"))
  117. {
  118. healthScript.hp = healthScript.totalHealth;
  119. }
  120. }
  121. private IEnumerator Attack()
  122. {
  123. if (playerController.intention)
  124. {
  125. if (Input.GetKeyDown(playerController.abilities[0].code))
  126. {
  127. //Attack
  128. animator.SetLayerWeight(animator.GetLayerIndex("Attack Layer"), 1);
  129. animator.SetTrigger("Attack");
  130. yield return new WaitForSeconds(0.9f);
  131. }
  132. }
  133. }
  134. private void SecretDance()
  135. {
  136. if (Input.GetKeyDown("t"))
  137. {
  138. animator.SetTrigger("Dance");
  139. }
  140. }
  141. private void GettingHit()
  142. {
  143. }
  144. }