/Assets/Scripts/Eating.cs

https://bitbucket.org/kyle-romano/piyo-world · C# · 147 lines · 120 code · 20 blank · 7 comment · 26 complexity · 49d860fe48289e784176fbba65a80225 MD5 · raw file

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Spine;
  5. using Spine.Unity;
  6. public class Eating : MonoBehaviour
  7. {
  8. IEnumerator eatingRoutine;
  9. GameObject heldFood;
  10. public bool foodHeldOverPiyo;
  11. private bool biting;
  12. [SerializeField] float timeBetweenBites;
  13. [SerializeField] float amountToReduceSizeBy;
  14. [SerializeField] float minSize;
  15. [SerializeField] ParticleSystem constantParticles;
  16. [SerializeField] ParticleSystem burstParticles;
  17. [SerializeField] Pathfinding.AIPath AipathScript;
  18. [SerializeField] string foodPrefernce;
  19. [SerializeField] string heldFoodType;
  20. [SerializeField] int postionOfChild;
  21. public SkeletonAnimation spineAnim;
  22. Spine.Event bite;
  23. public bool isHappy;
  24. Spine.AnimationState state;
  25. public bool piyoEating;
  26. TrackEntry trackEntry;
  27. [SpineEvent] public string biteEventName;
  28. // Use this for initialization
  29. void Awake()
  30. {
  31. biteEventName = "bite";
  32. eatingRoutine = EatingAnimation();
  33. spineAnim = GetComponentInChildren<SkeletonAnimation>();
  34. }
  35. private void Start()
  36. {
  37. spineAnim.state.Event += HandleEvent;
  38. spineAnim.state.Start += delegate (Spine.TrackEntry entry) { };
  39. spineAnim.state.End += delegate { };
  40. }
  41. void HandleEvent(Spine.TrackEntry entry, Spine.Event e)
  42. {
  43. if (e.data.name == biteEventName && piyoEating == false)
  44. {
  45. StartCoroutine("EatingAnimation");
  46. }
  47. }
  48. IEnumerator EatingAnimation()
  49. {
  50. while (foodHeldOverPiyo == true)
  51. {
  52. piyoEating = true;
  53. heldFood.transform.localScale = new Vector3(heldFood.transform.localScale.x - amountToReduceSizeBy, heldFood.transform.localScale.y - amountToReduceSizeBy, 1);
  54. burstParticles.Play();
  55. if (heldFood.transform.localScale.x <= minSize)
  56. {
  57. AipathScript.canMove = true;
  58. DragCamera.waitingForMouseRelease = true;
  59. burstParticles.Play();
  60. checkFoodPrefernce();
  61. Destroy(heldFood.transform.parent.gameObject);
  62. DragCamera.pickedUpObject = false;
  63. foodHeldOverPiyo = false;
  64. piyoEating = false;
  65. }
  66. yield return new WaitForSeconds(timeBetweenBites);
  67. }
  68. }
  69. void checkFoodPrefernce()
  70. {
  71. if(heldFood != null)
  72. {
  73. heldFoodType = heldFood.GetComponent<SpriteRenderer>().sprite.name;
  74. }
  75. if (foodPrefernce == heldFoodType)
  76. {
  77. isHappy = true;
  78. //spineAnim.AnimationName = ("happy");
  79. //Debug.Log(1);
  80. StartCoroutine (HappyTime());
  81. }
  82. }
  83. IEnumerator HappyTime()
  84. {
  85. trackEntry = spineAnim.AnimationState.SetAnimation(1, "happy", false);
  86. yield return new WaitForSpineAnimationComplete(trackEntry);
  87. isHappy = false;
  88. if (trackEntry.IsComplete)
  89. {
  90. spineAnim.AnimationState.ClearTrack(1);
  91. //spineAnim.AnimationState.SetAnimation(0,"idle",true);
  92. yield return null;
  93. }
  94. //spineAnim.AnimationState.SetEmptyAnimation(1,0);
  95. // trackEntry = spineAnim.AnimationState.SetAnimation(0, "idle", true);
  96. }
  97. private void OnTriggerEnter2D(Collider2D collision)
  98. {
  99. if (collision.gameObject.tag == "Item" && foodHeldOverPiyo == false && DragCamera.pickedUpObject == true)
  100. {
  101. AipathScript.canMove = false;
  102. heldFood = collision.transform.GetChild(0).gameObject;
  103. burstParticles = collision.transform.GetChild(postionOfChild).GetComponent<ParticleSystem>();
  104. foodHeldOverPiyo = true;
  105. spineAnim.AnimationName = "eating";
  106. ZoomCamera.startShrinking = true;
  107. StartCoroutine("EatingAnimation");
  108. }
  109. }
  110. private void OnTriggerExit2D(Collider2D collision)
  111. {
  112. if (collision.gameObject.tag == "Item" && DragCamera.pickedUpObject == true)
  113. {
  114. AipathScript.canMove = true;
  115. if (heldFood != null && burstParticles != null)
  116. {
  117. heldFood.transform.localScale = new Vector3(0.15f, 0.15f, 0.15f);
  118. //constantParticles.Stop();
  119. burstParticles.Stop();
  120. }
  121. foodHeldOverPiyo = false;
  122. StopCoroutine("EatingAnimation");
  123. ZoomCamera.startShrinking = false;
  124. spineAnim.AnimationName = "idle";
  125. }
  126. }
  127. }