using System.Collections; using System.Collections.Generic; using UnityEngine; using DistantLands.Cozy; using Unity.XR.CoreUtils; public class gameStateManager : MonoBehaviour { public GameObject player; public GameObject loadPlayer; public GameObject playerXrObject; public GameObject options; public GameObject playerCamera; public GameObject loadCamera; public GameObject pauseView; public Transform playerTarget; public Transform loadTarget; [SerializeField] CozyWeather cozyWeather; public GameObject weatherSound; public GameObject audioObject; public Transform levels; private int playerScore; public string[] levelOrder = new string[] { "level3", "level1", "level2", "level1" }; public int level; public bool playIntro = true; private bool gameStarted = false; private GameObject introObject; private GameObject outroObject; private GameObject loadingObject; private GameObject restartGame; private GameObject levelObject; private AudioSource audioSource; // Start is called before the first frame update void Start() { introObject = pauseView.transform.Find("intro").gameObject; outroObject = pauseView.transform.Find("outro").gameObject; loadingObject = pauseView.transform.Find("loading").gameObject; Debug.Log("start script"); level = 0; StartCoroutine(Intro()); } // Update is called once per frame void Update() { } void Levelsetup() { // this loops starts and ends with the player in a 'waiting room' – that is: // the player car should be placed in a waiting spot such that there is not traffic generated // main camera should be the one by the logo // weather app should be deleted / unactive // make next level into level and get new next level // get game object connected to level levelObject = levels.Find(levelOrder[level]).gameObject; // show the level levelObject.SetActive(true); // move car into possition Debug.Log("car moved!"); Transform startPoint = levelObject.transform.Find("startpoint"); MoveCar(startPoint); playerXrObject.transform.SetParent(player.transform.Find("XR interaction object").transform, false); //toggle cameras pauseView.SetActive(false); loadCamera.SetActive(false); playerCamera.SetActive(true); Recenter(playerTarget); options.SetActive(false); //activate sound weatherSound.SetActive(true); //loadEventSystem.SetActive(false); //gameEventSystem.SetActive(true); // start timer Debug.Log("start timer pre!"); GameObject goalObject = levelObject.transform.Find("goalmanager").gameObject; StartCoroutine(StartGoalScript(goalObject)); Debug.Log("start timer post!"); } IEnumerator LevelCompletion(int lastScore) { levelObject.SetActive(false); //1. activate loading scene //Open loading scene //switch cameras? //hide weather LoadingScene(); //2. collect points and pause game // Level completed – collect the score playerScore += lastScore; //3. Artificial wait yield return new WaitForSeconds(5); while (audioSource.isPlaying) { yield return null; // Wait for the next frame } // if next level is outro close loop if (level == levelOrder.Length - 1) { StartCoroutine(Outro()); } else { level++; Levelsetup(); } } public IEnumerator StartGoalScript(GameObject goalObject) { Debug.Log("StartGoalScript"); goalTimer childtimer = goalObject.GetComponent(); Debug.Log(childtimer); if (childtimer != null) { Debug.Log("Calling child's async function..."); //await childtimer.ChildFunctionAsync(); // Wait for the child's function to complete yield return StartCoroutine(childtimer.StartLevel(OnChildFunctionComplete)); Debug.Log("Back in parent after child function completed"); } else { Debug.LogWarning("Child script not found!"); } } private void OnChildFunctionComplete(int result) { Debug.Log("Parent received result from child: " + result); StartCoroutine(LevelCompletion(result)); } void MoveCar(Transform target) { if (target != null) { // Move this object to the target's position player.transform.position = target.position; // Rotate this object to match the target's rotation player.transform.rotation = target.rotation; } else { Debug.LogWarning("Target is not assigned."); } } IEnumerator Intro() { if (playIntro){ LoadingScene(); GameObject startGame = pauseView.transform.Find("start").gameObject; startGame.SetActive(true); while(!gameStarted){ yield return null; } startGame.SetActive(false); introObject.SetActive(true); loadingObject.SetActive(false); StartIntro startIntro = introObject.GetComponent(); yield return StartCoroutine(startIntro.introEnter(OnIntroComplete)); } else{ OnIntroComplete(); } } public void StartGameClicked(){ gameStarted = true; } private void OnIntroComplete() { introObject.SetActive(false); loadingObject.SetActive(true); Levelsetup(); } IEnumerator Outro() { LoadingScene(); loadingObject.SetActive(false); outroObject.SetActive(true); outroObject.GetComponent().outroEnter(playerScore); yield return new WaitForSeconds(7); restartGame = pauseView.transform.Find("reset").gameObject; restartGame.SetActive(true); } public void NextLoadlingAudio(AudioClip audio){ audioObject.SetActive(true); audioSource = audioObject.GetComponent(); audioSource.clip = audio; if (audioSource != null && audioSource.clip != null) { audioSource.Play(); } } void LoadingScene() { Debug.Log("loading scene script"); // the player car should be placed in a waiting spot such that there is not traffic generated // main camera should be the one by the logo // weather app should be deleted / unactive // activate intro function and game object Transform pausePoint = GameObject.Find("pauseLocation").transform; MoveCar(pausePoint); // silence weather weatherSound.SetActive(false); // switch cameras // Debug.Log("pausing camera"); pauseView.SetActive(true); playerXrObject.transform.SetParent(loadPlayer.transform, false); playerCamera.SetActive(false); loadCamera.SetActive(true); Recenter(loadTarget); // playerXrObject.transform.localPosition = Vector3.zero; // Reset local position // playerXrObject.transform.localRotation = Quaternion.identity; // weather reset: cozyWeather.timeModule.currentTime = new MeridiemTime(Random.Range(7, 19), Random.Range(0, 6) * 10); cozyWeather.timeModule.currentDay = cozyWeather.timeModule.currentDay + 1; Debug.Log("slut på loading"); } void Ending() { // the player car should be placed in a waiting spot such that there is not traffic generated // main camera should be the one by the logo // weather app should be deleted / unactive // activate ending function and game object } public void reset(){ playerScore = 0; level = 0; gameStarted = false; restartGame.SetActive(false); outroObject.SetActive(true); StartCoroutine(Intro()); } public void Recenter(Transform target) { Debug.Log("Recenter"); XROrigin xrOrigin = playerXrObject.GetComponent(); Debug.Log(target.position); Debug.Log(target.localPosition); Debug.Log(xrOrigin); Transform cameraOffset = xrOrigin.transform.Find("Camera Offset"); RecenterOrigin recenterOrigin = playerXrObject.GetComponent(); recenterOrigin.target = target; // Calculate the positional offset Vector3 offsetPosition = target.position - xrOrigin.Camera.transform.position; // Apply the positional offset to the Camera Offset object cameraOffset.position += offsetPosition; // Optionally, align the rotation of the Camera Offset to match the target's rotation cameraOffset.rotation = target.rotation; Debug.Log("target.position: " + target.position); Debug.Log("cameraOffset.position: " + cameraOffset.position); Debug.Log("playerXrObject.position: " + playerXrObject.transform.position); } }