Global Game Jam 2021
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

242 lines
8.6 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class ParentBehaviour : MonoBehaviour
  6. {
  7. public spawner babySpawner;
  8. public GameObject child;
  9. public Texture baseVis;
  10. public Texture hatVis;
  11. public Texture eyeVis;
  12. public Texture topVis;
  13. public Texture bottomVis;
  14. public List<string> order = new List<string>();
  15. public ParentBehaviour otherParent;
  16. public Texture2D[] parentBases;
  17. public Texture2D parentFaceNeutral;
  18. public Texture2D parentFaceHappy1;
  19. public Texture2D parentFaceHappy2;
  20. public Texture2D parentFaceAngry;
  21. public Texture2D parentFaceTalk;
  22. public GameObject parentBodyObj;
  23. public GameObject parentFaceObj;
  24. public GameObject parentDialougeObj;
  25. public GameObject parentDialogueOutline;
  26. public Transform parentRoot;
  27. public Vector3 slideDistance = Vector3.right;
  28. private void Start()
  29. {
  30. GetRandomChild();
  31. GenerateVisuals();
  32. }
  33. [ContextMenu("Get Random Child")]
  34. public void GetRandomChild()
  35. {
  36. bool _validChild = false;
  37. while (!_validChild)
  38. {
  39. int _childIndex = Random.Range(0, babySpawner.transform.childCount);
  40. child = babySpawner.transform.GetChild(_childIndex).gameObject;
  41. if (!ReferenceEquals(child, otherParent.child) || otherParent.child == null)
  42. {
  43. //this prevents both parents asking for the same child
  44. _validChild = true;
  45. }
  46. }
  47. Randomizer _childRandomizer = child.GetComponent<Randomizer>();
  48. baseVis = _childRandomizer.baseObj.GetComponent<MeshRenderer>().material.mainTexture;
  49. hatVis = _childRandomizer.hatObj.GetComponent<MeshRenderer>().material.mainTexture;
  50. eyeVis = _childRandomizer.eyeObj.GetComponent<MeshRenderer>().material.mainTexture;
  51. topVis = _childRandomizer.topObj.GetComponent<MeshRenderer>().material.mainTexture;
  52. bottomVis = _childRandomizer.bottomObj.GetComponent<MeshRenderer>().material.mainTexture;
  53. order.Insert(Random.Range(0, order.Count), "base");
  54. order.Insert(Random.Range(0, order.Count), "hat");
  55. order.Insert(Random.Range(0, order.Count), "eye");
  56. order.Insert(Random.Range(0, order.Count), "top");
  57. order.Insert(Random.Range(0, order.Count), "bottom");
  58. GiveFirstDetail();
  59. }
  60. public bool CheckChild(GameObject _child)
  61. {
  62. if (ReferenceEquals(_child, child))
  63. {
  64. return true;
  65. }
  66. else
  67. {
  68. return false;
  69. }
  70. }
  71. public IEnumerator SlideInOut(Vector3 endPos, float slideTime)
  72. {
  73. parentDialougeObj.SetActive(false);
  74. parentDialogueOutline.SetActive(false);
  75. Vector3 startPos = parentRoot.position;
  76. float timeElapsed = 0;
  77. while(timeElapsed < slideTime)
  78. {
  79. parentRoot.position = Vector3.Lerp(startPos, endPos, timeElapsed / slideTime);
  80. yield return new WaitForEndOfFrame();
  81. timeElapsed += Time.deltaTime;
  82. }
  83. parentRoot.position = endPos;
  84. GetRandomChild();
  85. GenerateVisuals();
  86. while (timeElapsed < slideTime)
  87. {
  88. parentRoot.position = Vector3.Lerp(endPos, startPos, timeElapsed / slideTime);
  89. yield return new WaitForEndOfFrame();
  90. timeElapsed += Time.deltaTime;
  91. }
  92. parentRoot.position = startPos;
  93. parentDialougeObj.SetActive(true);
  94. parentDialogueOutline.SetActive(true);
  95. }
  96. public void GenerateVisuals()
  97. {
  98. Texture _displayGraphic = parentBases[Random.Range(0, parentBases.Length)];
  99. parentBodyObj.GetComponent<Image>().sprite = Sprite.Create((Texture2D)_displayGraphic, new Rect(0.0f, 0.0f, _displayGraphic.width, _displayGraphic.height), new Vector2(0.5f, 0.5f), 100.0f);
  100. _displayGraphic = parentFaceNeutral;
  101. parentFaceObj.GetComponent<Image>().sprite = Sprite.Create((Texture2D)_displayGraphic, new Rect(0.0f, 0.0f, _displayGraphic.width, _displayGraphic.height), new Vector2(0.5f, 0.5f), 100.0f);
  102. }
  103. public Texture GiveDetails(GameObject _child)
  104. {
  105. for (int i = 0; i < order.Count; i++)
  106. {
  107. switch (order[i]){
  108. case "base":
  109. if (_child.GetComponent<Randomizer>().baseObj.GetComponent<MeshRenderer>().material.mainTexture.name != baseVis.name)
  110. {
  111. print("returning baseVis");
  112. return baseVis;
  113. }
  114. break;
  115. case "hat":
  116. if (_child.GetComponent<Randomizer>().hatObj.GetComponent<MeshRenderer>().material.mainTexture.name != hatVis.name)
  117. {
  118. print("returning hatVis");
  119. return hatVis;
  120. }
  121. break;
  122. case "eye":
  123. if (_child.GetComponent<Randomizer>().eyeObj.GetComponent<MeshRenderer>().material.mainTexture.name != eyeVis.name)
  124. {
  125. print("returning eyeVis");
  126. return eyeVis;
  127. }
  128. break;
  129. case "top":
  130. if (_child.GetComponent<Randomizer>().topObj.GetComponent<MeshRenderer>().material.mainTexture.name != topVis.name)
  131. {
  132. print("returning topVis");
  133. return topVis;
  134. }
  135. break;
  136. case "bottom":
  137. if (_child.GetComponent<Randomizer>().bottomObj.GetComponent<MeshRenderer>().material.mainTexture.name != bottomVis.name)
  138. {
  139. print("returning bottomVis");
  140. return bottomVis;
  141. }
  142. break;
  143. default:
  144. break;
  145. }
  146. }
  147. return null;
  148. }
  149. public void GiveFirstDetail()
  150. {
  151. Texture _displayGraphic;
  152. switch (order[0])
  153. {
  154. case "base":
  155. _displayGraphic = baseVis;
  156. break;
  157. case "hat":
  158. _displayGraphic = hatVis;
  159. break;
  160. case "eye":
  161. _displayGraphic = eyeVis;
  162. break;
  163. case "top":
  164. _displayGraphic = topVis;
  165. break;
  166. case "bottom":
  167. _displayGraphic = bottomVis;
  168. break;
  169. default:
  170. _displayGraphic = null;
  171. break;
  172. }
  173. parentDialougeObj.GetComponent<Image>().sprite = Sprite.Create((Texture2D)_displayGraphic, new Rect(0.0f, 0.0f, _displayGraphic.width, _displayGraphic.height), new Vector2(0.5f, 0.5f), 100.0f);
  174. }
  175. private void OnCollisionEnter(Collision collision)
  176. {
  177. if (child != null)
  178. {
  179. if (collision.collider.gameObject.tag == "Child")
  180. {
  181. if (CheckChild(collision.collider.gameObject))
  182. {
  183. print("correct child");
  184. //this is the correct child
  185. GameObject player = collision.collider.gameObject.GetComponent<YeetHandle>().lastHeld;
  186. player.GetComponent<PlayerDataHolder>().AddScore(1);
  187. //destroy baby, spawn 2 more
  188. Destroy(collision.collider.gameObject);
  189. babySpawner.SpawnBaby();
  190. babySpawner.SpawnBaby();
  191. //Slide, cha cha real smooth
  192. StartCoroutine(SlideInOut(parentRoot.position + slideDistance, 2));
  193. }
  194. else
  195. {
  196. Texture _displayGraphic = GiveDetails(collision.collider.gameObject);
  197. //display the display graphic
  198. parentDialougeObj.GetComponent<Image>().sprite = Sprite.Create((Texture2D)_displayGraphic, new Rect(0.0f, 0.0f, _displayGraphic.width, _displayGraphic.height), new Vector2(0.5f, 0.5f), 100.0f);
  199. //yeet child back to centre of room
  200. Debug.Log("ParentBehaviour.OnCollissionEnter: Yeet Back Child");
  201. GameObject lastHeldPlayer = collision.collider.gameObject.GetComponent<YeetHandle>().lastHeld;
  202. float yeetBackForce = 15f;
  203. Vector3 yeetBackVelocity = (-1 * lastHeldPlayer.transform.forward * yeetBackForce) + lastHeldPlayer.transform.up * yeetBackForce;
  204. collision.collider.gameObject.GetComponent<Rigidbody>().velocity = yeetBackVelocity;
  205. }
  206. }
  207. }
  208. }
  209. }