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.

190 lines
6.9 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
  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. private void Start()
  26. {
  27. Texture _displayGraphic = parentBases[Random.Range(0, parentBases.Length)];
  28. 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);
  29. _displayGraphic = parentFaceNeutral;
  30. 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);
  31. }
  32. [ContextMenu("Get Random Child")]
  33. public void GetRandomChild()
  34. {
  35. bool _validChild = false;
  36. while (!_validChild)
  37. {
  38. int _childIndex = Random.Range(0, babySpawner.transform.childCount);
  39. child = babySpawner.transform.GetChild(_childIndex).gameObject;
  40. if (!ReferenceEquals(child, otherParent.child) || otherParent.child == null)
  41. {
  42. //this prevents both parents asking for the same child
  43. _validChild = true;
  44. }
  45. }
  46. Randomizer _childRandomizer = child.GetComponent<Randomizer>();
  47. baseVis = _childRandomizer.baseObj.GetComponent<MeshRenderer>().material.mainTexture;
  48. hatVis = _childRandomizer.hatObj.GetComponent<MeshRenderer>().material.mainTexture;
  49. eyeVis = _childRandomizer.eyeObj.GetComponent<MeshRenderer>().material.mainTexture;
  50. topVis = _childRandomizer.topObj.GetComponent<MeshRenderer>().material.mainTexture;
  51. bottomVis = _childRandomizer.bottomObj.GetComponent<MeshRenderer>().material.mainTexture;
  52. order.Insert(Random.Range(0, order.Count), "base");
  53. order.Insert(Random.Range(0, order.Count), "hat");
  54. order.Insert(Random.Range(0, order.Count), "eye");
  55. order.Insert(Random.Range(0, order.Count), "top");
  56. order.Insert(Random.Range(0, order.Count), "bottom");
  57. GiveFirstDetail();
  58. }
  59. public bool CheckChild(GameObject _child)
  60. {
  61. if (_child == child)
  62. {
  63. return true;
  64. }
  65. else
  66. {
  67. return false;
  68. }
  69. }
  70. public Texture GiveDetails(GameObject _child)
  71. {
  72. for (int i = 0; i < order.Count; i++)
  73. {
  74. switch (order[i]){
  75. case "base":
  76. if (_child.GetComponent<Randomizer>().baseObj.GetComponent<MeshRenderer>().material.mainTexture.name != baseVis.name)
  77. {
  78. print("returning baseVis");
  79. return baseVis;
  80. }
  81. break;
  82. case "hat":
  83. if (_child.GetComponent<Randomizer>().hatObj.GetComponent<MeshRenderer>().material.mainTexture.name != hatVis.name)
  84. {
  85. print("returning hatVis");
  86. return hatVis;
  87. }
  88. break;
  89. case "eye":
  90. if (_child.GetComponent<Randomizer>().eyeObj.GetComponent<MeshRenderer>().material.mainTexture.name != eyeVis.name)
  91. {
  92. print("returning eyeVis");
  93. return eyeVis;
  94. }
  95. break;
  96. case "top":
  97. if (_child.GetComponent<Randomizer>().topObj.GetComponent<MeshRenderer>().material.mainTexture.name != topVis.name)
  98. {
  99. print("returning topVis");
  100. return topVis;
  101. }
  102. break;
  103. case "bottom":
  104. if (_child.GetComponent<Randomizer>().bottomObj.GetComponent<MeshRenderer>().material.mainTexture.name != bottomVis.name)
  105. {
  106. print("returning bottomVis");
  107. return bottomVis;
  108. }
  109. break;
  110. default:
  111. break;
  112. }
  113. }
  114. return null;
  115. }
  116. public void GiveFirstDetail()
  117. {
  118. Texture _displayGraphic;
  119. switch (order[0])
  120. {
  121. case "base":
  122. _displayGraphic = baseVis;
  123. break;
  124. case "hat":
  125. _displayGraphic = hatVis;
  126. break;
  127. case "eye":
  128. _displayGraphic = eyeVis;
  129. break;
  130. case "top":
  131. _displayGraphic = topVis;
  132. break;
  133. case "bottom":
  134. _displayGraphic = bottomVis;
  135. break;
  136. default:
  137. _displayGraphic = null;
  138. break;
  139. }
  140. 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);
  141. }
  142. private void OnCollisionEnter(Collision collision)
  143. {
  144. if (child != null)
  145. {
  146. if (collision.collider.gameObject.tag == "Child")
  147. {
  148. if (CheckChild(collision.collider.gameObject))
  149. {
  150. print("correct child");
  151. //this is the correct child
  152. }
  153. else
  154. {
  155. Texture _displayGraphic = GiveDetails(collision.collider.gameObject);
  156. //display the display graphic
  157. 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);
  158. //yeet child back to centre of room
  159. Debug.Log("ParentBehaviour.OnCollissionEnter: Yeet Back Child");
  160. GameObject lastHeldPlayer = collision.collider.gameObject.GetComponent<YeetHandle>().lastHeld;
  161. float yeetBackForce = 15f;
  162. Vector3 yeetBackVelocity = (-1 * lastHeldPlayer.transform.forward * yeetBackForce) + lastHeldPlayer.transform.up * yeetBackForce;
  163. collision.collider.gameObject.GetComponent<Rigidbody>().velocity = yeetBackVelocity;
  164. }
  165. }
  166. }
  167. }
  168. }