Untitled
plain_text
a month ago
18 kB
1
Indexable
Never
using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.WSA; using UnityEngine.XR.Interaction.Toolkit; public class InflatingBalloons : ActivityManager { // unique activiy type public static ActivityType activityType = ActivityType.InflatingBalloons; public ParticleSystem disappearPS; public GameObject[] balloons = new GameObject[] { }; public GameObject inflatingBalloonsPrefab; public GameObject fox; public GameObject rabbit; public GameObject deer; public GameObject rayInteractor; private RaycastHit hit; private int rewardCount; private int balloonsHit = 0; private Balloon balloonComponent; public float scaleFactor = 0.04f; public float interval = 0.01f; private Coroutine growBalloonCoroutine; private Coroutine shrinkBalloonCoroutine; private Camera camera; private bool lookedOnBalloon = false; private GameObject balloon; private bool evaluatingBalloon = false; // Start is called before the first frame update void Start() { ControllerManager.instance.SetPlayerMovement(true); } // Update is called once per frame private void Update() { var camera = Camera.main; rayInteractor.transform.position = camera.transform.position - new Vector3(0, 0.15f, 0); rayInteractor.transform.rotation = camera.transform.rotation; if (Physics.Raycast(camera.transform.position - new Vector3(0, 0.15f, 0), camera.transform.forward, out hit)) { //Debug.DrawRay(camera.transform.position - new Vector3(0, 0.15f, 0), camera.transform.forward * 100f, Color.green); //Debug.Log("EyeRay hit " + hit.transform.gameObject.name); if (hit.transform.gameObject.name.Contains("balloon")) { StopCoroutine(BalloonResult()); lookedOnBalloon = true; balloon = hit.transform.gameObject; balloonComponent = hit.transform.gameObject.GetComponent<Balloon>(); balloonComponent.balloonOutline.active = true; balloon.transform.localScale += new Vector3(1, 1, 1) * 0.03f; Debug.Log("looking at: " + balloon.gameObject.name); if (balloon.transform.localScale.x > 4) { EvaluatePopBalloon(); } } else // if not looking on the balloon { if(balloonComponent != null) { balloonComponent.balloonOutline.active = false; } if (lookedOnBalloon) { //balloon.GetComponent<Balloon>().balloonOutline.active = false; //StartCoroutine(BalloonResult()); if (balloon.transform.localScale.x <= 4) { EvaluateFlyAwayBalloon(); lookedOnBalloon = false; } } } } } private void EvaluatePopBalloon() { if (!balloonComponent.destroyed) { switch (balloonsHit) { case int n when (n >= 0 && n < 3): balloonComponent.StartCoroutine(balloonComponent.DeactivateBalloonLater(balloon, VegetableType.APPLE, true)); break; case int n when (n >= 3 && n < 6): balloonComponent.StartCoroutine(balloonComponent.DeactivateBalloonLater(balloon, VegetableType.BROCCOLI, true)); break; case int n when (n >= 6 && n < 9): balloonComponent.StartCoroutine(balloonComponent.DeactivateBalloonLater(balloon, VegetableType.CARROT, true)); break; default: break; } balloonsHit += 1; Debug.Log("balloons hit : " + balloonsHit); switch (balloonsHit) { case int n when (n >= 3 && n < 4): FinishFirstLevel(); LoadBalloonsLvl2(); break; case int n when (n >= 6 && n < 7): FinishSecondLevel(); LoadBalloonsLvl3(); break; case int n when (n >= 9 && n < 10): FinishThirdLevel(); Finish(); break; default: break; } } } private void EvaluateFlyAwayBalloon() { Debug.Log("evaluating fly away balloon, pos of balloon :" + balloon.transform.position); Vector3 targetPosition = balloon.transform.position + Vector3.up * 500f; balloon.GetComponent<Balloon>().move = false; balloon.transform.position = Vector3.MoveTowards( balloon.transform.position, targetPosition, 2 * Time.deltaTime); if (Vector3.Distance(balloon.transform.position, targetPosition) < 0.01f) { // Balloon has reached the target, handle any necessary actions // For example, you might want to deactivate the balloon balloon.SetActive(false); } Debug.Log("pos of balloon: " + balloon.transform.position); switch (balloon.transform.localScale.x) { case float scale when scale < 2f: rewardCount = 1; break; case float scale when scale < 3.2f && scale >= 2f: rewardCount = 2; break; case float scale when scale < 3.7f && scale >= 3.2f: rewardCount = 3; break; case float scale when scale <= 4f && scale >= 3.7f: rewardCount = 4; break; default: break; } if (!balloonComponent.destroyed) { switch (balloonsHit) { case int n when (n >= 0 && n < 3): balloonComponent.StartCoroutine(balloonComponent.DeactivateBalloonLater(balloon, VegetableType.APPLE, false)); break; case int n when (n >= 3 && n < 6): balloonComponent.StartCoroutine(balloonComponent.DeactivateBalloonLater(balloon, VegetableType.BROCCOLI, false)); break; case int n when (n >= 6 && n < 9): balloonComponent.StartCoroutine(balloonComponent.DeactivateBalloonLater(balloon, VegetableType.CARROT, false)); break; default: break; } balloonComponent.rewardCount = rewardCount; balloonsHit += 1; Debug.Log(balloonsHit + "balloons hit count"); switch (balloonsHit) { case int n when (n >= 3 && n < 4): FinishFirstLevel(); LoadBalloonsLvl2(); break; case int n when (n >= 6 && n < 7): FinishSecondLevel(); LoadBalloonsLvl3(); break; case int n when (n >= 9 && n < 10): FinishThirdLevel(); Finish(); break; default: break; } } } IEnumerator BalloonResult() { //yield return new WaitForSeconds(1.5f); if (lookedOnBalloon) { yield return null; } else { if (balloon != null) { if (balloon.transform.localScale.x <= 4) { EvaluateFlyAwayBalloon(); } else { //EvaluatePopBalloon(); } } else { Debug.Log("evaluating fly away1 balloon null" + balloon); evaluatingBalloon = false; } } } public void InflateBalloon(HoverEnterEventArgs args) { //if (shrinkBalloonCoroutine != null) //{ // StopCoroutine(shrinkBalloonCoroutine); //} //Debug.Log("inflating "+ args.interactableObject); //var balloon = args.interactableObject.transform.gameObject; //if (balloon.name.Contains("balloon")) //{ // //StartCoroutine(ScaleBalloon(hit.transform.gameObject)); // //balloonComponent = balloon.GetComponent<Balloon>(); // //var rand = Random.Range(3f, 6f); // //Debug.Log(rand); // if (balloon.transform.localScale.x <= 6) // { // balloon.transform.localScale += new Vector3(1, 1, 1) * 2f; // growBalloonCoroutine = StartCoroutine(GrowBalloon(balloon)); // Debug.Log("looking at: " + balloon.gameObject.name); // switch (balloon.transform.localScale.x) // { // case float scale when scale < 4f: // rewardCount = 1; // break; // case float scale when scale < 5f && scale >= 4.5f: // rewardCount = 2; // break; // case float scale when scale < 5.5f && scale >= 5f: // rewardCount = 3; // break; // case float scale when scale <= 6f && scale >= 5.5f: // rewardCount = 4; // break; // default: // break; // } // } // else if (!balloonComponent.destroyed) // { // balloonComponent.StartCoroutine(balloonComponent.DeactivateBalloonLater(balloon, VegetableType.BROCCOLI)); // balloonComponent.rewardCount = rewardCount; // Debug.Log("reward count" + rewardCount); // balloonsHit += 1; // Debug.Log("balloons hit : " + balloonsHit); // //if (balloonsHit >= 2) // //{ // // Debug.Log("1st level finishedd "); // // FinishFirstLevel(); // // LoadBalloons(); // //} // switch (balloonsHit) // { // case int n when (n >= 3 && n < 4): // FinishFirstLevel(); // //ReloadBalloons(); // break; // case int n when (n >= 6 && n < 7): // FinishSecondLevel(); // //ReloadBalloons(); // break; // case int n when (n >= 9 && n < 10): // FinishThirdLevel(); // Finish(); // break; // default: // break; // } // } //} } public void DeflateBalloon(HoverExitEventArgs args) { //if (growBalloonCoroutine != null) //{ // StopCoroutine(growBalloonCoroutine); //} //Debug.Log("deflating " + args.interactableObject); //var balloon = args.interactableObject.transform.gameObject; //shrinkBalloonCoroutine = StartCoroutine(ShrinkBalloon(balloon)); } private IEnumerator GrowBalloon(GameObject balloon) { //while (false) //{ // balloonComponent = balloon.GetComponent<Balloon>(); // Debug.Log("balloonComponent destroyed : " + balloonComponent.destroyed); // if (balloon.transform.localScale.x <= 6) // { // // Increase the scale of the object // balloon.transform.localScale += new Vector3(scaleFactor, scaleFactor, scaleFactor); // switch (balloon.transform.localScale.x) // { // case float scale when scale < 4f: // rewardCount = 1; // break; // case float scale when scale < 5f && scale >= 4.5f: // rewardCount = 2; // break; // case float scale when scale < 5.5f && scale >= 5f: // rewardCount = 3; // break; // case float scale when scale <= 6f && scale >= 5.5f: // rewardCount = 4; // break; // default: // break; // } // } // else if (!balloonComponent.destroyed) // { // switch (balloonsHit) // { // case int n when ( n <= 3): // balloonComponent.StartCoroutine(balloonComponent.DeactivateBalloonLater(balloon, VegetableType.APPLE)); // break; // case int n when (n>3 && n <= 6 ): // balloonComponent.StartCoroutine(balloonComponent.DeactivateBalloonLater(balloon, VegetableType.BROCCOLI)); // break; // case int n when (n>6 && n <= 9): // balloonComponent.StartCoroutine(balloonComponent.DeactivateBalloonLater(balloon, VegetableType.CARROT)); // break; // default: // break; // } // balloonsHit += 1; // balloonComponent.rewardCount = rewardCount; // switch (balloonsHit) // { // case int n when (n >= 3 && n < 4): // FinishFirstLevel(); // LoadBalloonsLvl2(); // break; // case int n when (n >= 6 && n < 7): // FinishSecondLevel(); // LoadBalloonsLvl3(); // break; // case int n when (n >= 9 && n < 10): // FinishThirdLevel(); // Finish(); // break; // default: // break; // } // } yield return new WaitForSeconds(interval); //} } private IEnumerator ShrinkBalloon(GameObject balloon) { //while (true) //{ // // Increase the scale of the object // if (balloon.transform.localScale.magnitude > new Vector3(2, 2, 2).magnitude) // { // balloon.transform.localScale -= new Vector3(scaleFactor, scaleFactor, scaleFactor); // } yield return new WaitForSeconds(interval); //} } public override ActivityType GetActivityType() { return activityType; } public override void StartDistraction(PlayerType playerType, Spot spot) { base.StartDistraction(playerType, spot); inflatingBalloonsPrefab.SetActive(true); Debug.Log("run called"); LoadBalloons(); } public void LoadBalloons() { for (int i = 0; i < 3; i++) { balloons[i].SetActive(true); //balloons[i].GetComponent<Balloon>().balloonOutline.SetActive(true); } } public void LoadBalloonsLvl2() { for (int i = 3; i < 6; i++) { balloons[i].SetActive(true); balloons[i].GetComponent<Balloon>().destroyed = false; balloons[i].GetComponent<Balloon>().activated = false; } } public void LoadBalloonsLvl3() { for (int i = 6; i < 9; i++) { balloons[i].SetActive(true); balloons[i].GetComponent<Balloon>().destroyed = false; balloons[i].GetComponent<Balloon>().activated = false; } } //public void ReloadBalloons() //{ // for (int i = 0; i < balloons.Length; i++) // { // balloons[i].SetActive(true); // balloons[i].transform.localScale = new Vector3(2, 2, 2); // //balloons[i].GetComponent<Balloon>().destroyed = false; // } //} public void FinishFirstLevel() { Debug.Log("1st level finished "); fox.SetActive(true); } public void FinishSecondLevel() { Debug.Log("2nd level finished "); fox.SetActive(false); rabbit.SetActive(true); } public void FinishThirdLevel() { Debug.Log("3rd level finished "); rabbit.SetActive(false); deer.SetActive(true); } public override void Reload() { } }