using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ParentBehaviour : MonoBehaviour { public spawner babySpawner; public GameObject child; public Texture baseVis; public Texture hatVis; public Texture eyeVis; public Texture topVis; public Texture bottomVis; public List order = new List(); public ParentBehaviour otherParent; public Texture2D[] parentBases; public Texture2D parentFaceNeutral; public Texture2D parentFaceHappy1; public Texture2D parentFaceHappy2; public Texture2D parentFaceAngry; public Texture2D parentFaceTalk; public GameObject parentBodyObj; public GameObject parentFaceObj; public GameObject parentDialougeObj; public GameObject parentDialogueOutline; public Transform parentRoot; public Vector3 slideDistance = Vector3.right; private void Start() { GetRandomChild(); GenerateVisuals(); } [ContextMenu("Get Random Child")] public void GetRandomChild() { bool _validChild = false; while (!_validChild) { int _childIndex = Random.Range(0, babySpawner.transform.childCount); child = babySpawner.transform.GetChild(_childIndex).gameObject; if (!ReferenceEquals(child, otherParent.child) || otherParent.child == null) { //this prevents both parents asking for the same child _validChild = true; } } Randomizer _childRandomizer = child.GetComponent(); baseVis = _childRandomizer.baseObj.GetComponent().material.mainTexture; hatVis = _childRandomizer.hatObj.GetComponent().material.mainTexture; eyeVis = _childRandomizer.eyeObj.GetComponent().material.mainTexture; topVis = _childRandomizer.topObj.GetComponent().material.mainTexture; bottomVis = _childRandomizer.bottomObj.GetComponent().material.mainTexture; order.Insert(Random.Range(0, order.Count), "base"); order.Insert(Random.Range(0, order.Count), "hat"); order.Insert(Random.Range(0, order.Count), "eye"); order.Insert(Random.Range(0, order.Count), "top"); order.Insert(Random.Range(0, order.Count), "bottom"); GiveFirstDetail(); } public bool CheckChild(GameObject _child) { if (ReferenceEquals(_child, child)) { return true; } else { return false; } } public IEnumerator SlideInOut(Vector3 endPos, float slideTime) { parentDialougeObj.SetActive(false); parentDialogueOutline.SetActive(false); Vector3 startPos = parentRoot.position; float timeElapsed = 0; while(timeElapsed < slideTime) { parentRoot.position = Vector3.Lerp(startPos, endPos, timeElapsed / slideTime); yield return new WaitForEndOfFrame(); timeElapsed += Time.deltaTime; } parentRoot.position = endPos; GetRandomChild(); GenerateVisuals(); while (timeElapsed < slideTime) { parentRoot.position = Vector3.Lerp(endPos, startPos, timeElapsed / slideTime); yield return new WaitForEndOfFrame(); timeElapsed += Time.deltaTime; } parentRoot.position = startPos; parentDialougeObj.SetActive(true); parentDialogueOutline.SetActive(true); } public void GenerateVisuals() { Texture _displayGraphic = parentBases[Random.Range(0, parentBases.Length)]; parentBodyObj.GetComponent().sprite = Sprite.Create((Texture2D)_displayGraphic, new Rect(0.0f, 0.0f, _displayGraphic.width, _displayGraphic.height), new Vector2(0.5f, 0.5f), 100.0f); _displayGraphic = parentFaceNeutral; parentFaceObj.GetComponent().sprite = Sprite.Create((Texture2D)_displayGraphic, new Rect(0.0f, 0.0f, _displayGraphic.width, _displayGraphic.height), new Vector2(0.5f, 0.5f), 100.0f); } public Texture GiveDetails(GameObject _child) { for (int i = 0; i < order.Count; i++) { switch (order[i]){ case "base": if (_child.GetComponent().baseObj.GetComponent().material.mainTexture.name != baseVis.name) { print("returning baseVis"); return baseVis; } break; case "hat": if (_child.GetComponent().hatObj.GetComponent().material.mainTexture.name != hatVis.name) { print("returning hatVis"); return hatVis; } break; case "eye": if (_child.GetComponent().eyeObj.GetComponent().material.mainTexture.name != eyeVis.name) { print("returning eyeVis"); return eyeVis; } break; case "top": if (_child.GetComponent().topObj.GetComponent().material.mainTexture.name != topVis.name) { print("returning topVis"); return topVis; } break; case "bottom": if (_child.GetComponent().bottomObj.GetComponent().material.mainTexture.name != bottomVis.name) { print("returning bottomVis"); return bottomVis; } break; default: break; } } return null; } public void GiveFirstDetail() { Texture _displayGraphic; switch (order[0]) { case "base": _displayGraphic = baseVis; break; case "hat": _displayGraphic = hatVis; break; case "eye": _displayGraphic = eyeVis; break; case "top": _displayGraphic = topVis; break; case "bottom": _displayGraphic = bottomVis; break; default: _displayGraphic = null; break; } parentDialougeObj.GetComponent().sprite = Sprite.Create((Texture2D)_displayGraphic, new Rect(0.0f, 0.0f, _displayGraphic.width, _displayGraphic.height), new Vector2(0.5f, 0.5f), 100.0f); } private void OnCollisionEnter(Collision collision) { if (child != null) { if (collision.collider.gameObject.tag == "Child") { if (CheckChild(collision.collider.gameObject)) { print("correct child"); //this is the correct child GameObject player = collision.collider.gameObject.GetComponent().lastHeld; player.GetComponent().AddScore(1); //destroy baby, spawn 2 more Destroy(collision.collider.gameObject); babySpawner.SpawnBaby(); babySpawner.SpawnBaby(); //Slide, cha cha real smooth StartCoroutine(SlideInOut(parentRoot.position + slideDistance, 2)); } else { Texture _displayGraphic = GiveDetails(collision.collider.gameObject); //display the display graphic parentDialougeObj.GetComponent().sprite = Sprite.Create((Texture2D)_displayGraphic, new Rect(0.0f, 0.0f, _displayGraphic.width, _displayGraphic.height), new Vector2(0.5f, 0.5f), 100.0f); //yeet child back to centre of room Debug.Log("ParentBehaviour.OnCollissionEnter: Yeet Back Child"); GameObject lastHeldPlayer = collision.collider.gameObject.GetComponent().lastHeld; float yeetBackForce = 15f; Vector3 yeetBackVelocity = (-1 * lastHeldPlayer.transform.forward * yeetBackForce) + lastHeldPlayer.transform.up * yeetBackForce; collision.collider.gameObject.GetComponent().velocity = yeetBackVelocity; } } } } }