104 lines
3.7 KiB
C#
104 lines
3.7 KiB
C#
using System;
|
|
using System.Collections;
|
|
using Management;
|
|
using Player;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace State.Game
|
|
{
|
|
public class MainStateMachine : Machine
|
|
{
|
|
public GameObject MainMenuPrefab => mainMenuPrefab;
|
|
public GameObject PlayerPrefab => playerPrefab;
|
|
public GameObject FruitsPrefab => fruitsPrefab;
|
|
|
|
public Movement PlayerMovement { get; private set; }
|
|
public AudioClip DeathSound => deathSound;
|
|
|
|
[SerializeField] private GameObject mainMenuPrefab;
|
|
[SerializeField] private GameObject playerPrefab;
|
|
[SerializeField] private GameObject fruitsPrefab;
|
|
[SerializeField] private GameObject deathScreenPrefab;
|
|
[SerializeField] private AudioClip deathSound;
|
|
[SerializeField] private GameObject tutorialPrefab;
|
|
|
|
private GameObject _playerInstance;
|
|
private GameObject _fruitsInstance;
|
|
private GameObject _deathScreenInstance;
|
|
private GameObject _tutorialScreenInstance;
|
|
private bool _tutorial1Done;
|
|
private bool _tutorial2Done;
|
|
|
|
private void Awake()
|
|
{
|
|
_deathScreenInstance = Instantiate(deathScreenPrefab);
|
|
ShowDeathScreen(false);
|
|
_tutorialScreenInstance = Instantiate(tutorialPrefab);
|
|
ShowTutorial(false);
|
|
}
|
|
|
|
public void Respawn()
|
|
{
|
|
if (_fruitsInstance)
|
|
{
|
|
Destroy(_fruitsInstance);
|
|
}
|
|
|
|
if (_playerInstance)
|
|
{
|
|
Destroy(_playerInstance);
|
|
}
|
|
var respawn = GameObject.FindGameObjectWithTag("Respawn").transform;
|
|
_fruitsInstance = Instantiate(fruitsPrefab);
|
|
_playerInstance = Instantiate(playerPrefab, respawn.position, Quaternion.identity);
|
|
PlayerMovement = _playerInstance.GetComponent<Movement>();
|
|
}
|
|
|
|
public void ShowDeathScreen(bool show)
|
|
{
|
|
_deathScreenInstance.GetComponent<UIDocument>().rootVisualElement.Q<VisualElement>().visible = show;
|
|
}
|
|
|
|
public void ShowTutorial(bool show)
|
|
{
|
|
if (show)
|
|
{
|
|
if (!_tutorial1Done)
|
|
{
|
|
StartCoroutine(DelayTutorial());
|
|
return;
|
|
}
|
|
|
|
if (!_tutorial2Done)
|
|
{
|
|
Time.timeScale = 0;
|
|
Services.Instance.InputRouter.OnInteract += HandleClearTutorial;
|
|
_tutorial2Done = true;
|
|
_tutorialScreenInstance.GetComponent<UIDocument>().rootVisualElement.Q<VisualElement>("Tutorial2").style.display = DisplayStyle.Flex;;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Time.timeScale = 1;
|
|
_tutorialScreenInstance.GetComponent<UIDocument>().rootVisualElement.Q<VisualElement>("Tutorial1").style.display = DisplayStyle.None;Time.timeScale = 1;
|
|
_tutorialScreenInstance.GetComponent<UIDocument>().rootVisualElement.Q<VisualElement>("Tutorial2").style.display = DisplayStyle.None;
|
|
}
|
|
}
|
|
|
|
private IEnumerator DelayTutorial()
|
|
{
|
|
yield return new WaitForSeconds(0.25f);
|
|
Time.timeScale = 0;
|
|
Services.Instance.InputRouter.OnInteract += HandleClearTutorial;
|
|
_tutorialScreenInstance.GetComponent<UIDocument>().rootVisualElement.Q<VisualElement>("Tutorial1").style.display = DisplayStyle.Flex;;
|
|
_tutorial1Done = true;
|
|
}
|
|
|
|
private void HandleClearTutorial(bool obj)
|
|
{
|
|
Services.Instance.InputRouter.OnInteract -= HandleClearTutorial;
|
|
ShowTutorial(false);
|
|
}
|
|
}
|
|
} |