309 lines
10 KiB
C#
309 lines
10 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using PixelCrushers;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Beyond
|
|
{
|
|
public class MainMenu : MonoBehaviour
|
|
{
|
|
private enum State
|
|
{ MAIN_MENU, NEW_GAME, LOAD_GAME, LOADING_SCENE, DIFFICULTY };
|
|
|
|
private State m_state = State.MAIN_MENU;
|
|
public GameObject m_mainButtons;
|
|
public GameObject m_newGamePage;
|
|
public GameObject m_slotSelector;
|
|
public GameObject m_loadingScreen;
|
|
public GameObject m_loadGameButton;
|
|
public GameObject m_continueGameButton;
|
|
public GameObject m_difficultyPage;
|
|
|
|
public Animator m_doorAnimator;
|
|
public Animator m_cameraAnimator;
|
|
|
|
public Button[] m_difficultyButtons;
|
|
|
|
public float m_cameraDoorTime = 3f;
|
|
|
|
// Start is called before the first frame update
|
|
private void Start()
|
|
{
|
|
SetState(State.MAIN_MENU);
|
|
GameStateManager.Instance.SetDifficulty((GameStateManager.Difficulty)SaveUtility.Instance.GetCurrentDifficulty());
|
|
}
|
|
|
|
public void OnNewGameButton()
|
|
{
|
|
SetState(State.NEW_GAME);
|
|
}
|
|
|
|
public void OnLoadGameButton()
|
|
{
|
|
SetState(State.LOAD_GAME);
|
|
}
|
|
|
|
private void SetState(State state)
|
|
{
|
|
switch (state)
|
|
{
|
|
case State.MAIN_MENU:
|
|
OnMainMenu();
|
|
break;
|
|
|
|
case State.NEW_GAME:
|
|
OnNewGame();
|
|
break;
|
|
|
|
case State.LOAD_GAME:
|
|
OnLoadGame();
|
|
break;
|
|
|
|
case State.LOADING_SCENE:
|
|
m_mainButtons.SetActive(false);
|
|
m_newGamePage.SetActive(false);
|
|
m_loadingScreen.SetActive(true);
|
|
break;
|
|
|
|
case State.DIFFICULTY:
|
|
OnDifficulty(SaveUtility.Instance.GetCurrentDifficulty());
|
|
m_difficultyPage.SetActive(true);
|
|
m_mainButtons.SetActive(false);
|
|
break;
|
|
}
|
|
}
|
|
|
|
private IEnumerator LoadSceneCooroutine(int sceneNum)
|
|
{
|
|
// The Application loads the Scene in the background as the current Scene runs.
|
|
// This is particularly good for creating loading screens.
|
|
// You could also load the Scene by using sceneBuildIndex. In this case Scene2 has
|
|
// a sceneBuildIndex of 1 as shown in Build Settings.
|
|
//yield return new WaitForSeconds(1f);
|
|
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneNum);
|
|
asyncLoad.allowSceneActivation = false;
|
|
// Wait until the asynchronous scene fully loads
|
|
while (!asyncLoad.isDone)
|
|
{
|
|
//m_bar.transform.localScale = new Vector3(asyncLoad.progress, 0.9f, 1);
|
|
if (asyncLoad.progress >= 0.9f)
|
|
{
|
|
//m_bar.transform.localScale = new Vector3(0.9f, 0.9f, 1);
|
|
asyncLoad.allowSceneActivation = true;
|
|
}
|
|
yield return null;
|
|
}
|
|
//m_bar.transform.localScale = new Vector3(1.0f, 0.9f, 1);
|
|
yield return null;
|
|
}
|
|
|
|
private void OnMainMenu()
|
|
{
|
|
m_mainButtons.SetActive(true);
|
|
m_newGamePage.SetActive(false);
|
|
m_loadingScreen.SetActive(false);
|
|
m_difficultyPage.SetActive(false);
|
|
bool hasSaves = false;
|
|
for (int i = 0; i < m_slotSelector.transform.childCount; i++)
|
|
{
|
|
if (SaveSystem.HasSavedGameInSlot(i))
|
|
{
|
|
hasSaves = true;
|
|
break;
|
|
}
|
|
}
|
|
m_loadGameButton.SetActive(hasSaves);
|
|
m_continueGameButton.SetActive(hasSaves && SaveUtility.Instance.GetCurrentSlot() >= 0);
|
|
}
|
|
|
|
private void NewGameInSlot(int slot)
|
|
{
|
|
SaveUtility.Instance.SetCurrentSlot(slot);
|
|
GameStateManager.Instance.SetDifficulty((GameStateManager.Difficulty)SaveUtility.Instance.GetCurrentDifficulty());
|
|
//SceneManager.LoadScene(1);
|
|
ProxySceneLoader.LoadScene($"index:{1}");
|
|
}
|
|
|
|
private void OnNewGameSlotSelected(int num)
|
|
{
|
|
Debug.Log("OnNewGameSlot "+num);
|
|
if (SaveSystem.HasSavedGameInSlot(num))
|
|
{
|
|
ModalPopup.Instance.Show(yes =>
|
|
{
|
|
if (yes)
|
|
{
|
|
SaveSystem.DeleteSavedGameInSlot(num);
|
|
//NewGameInSlot(num);
|
|
StartCoroutine(StartScene(NewGameInSlot, num));
|
|
}
|
|
});
|
|
}
|
|
else
|
|
{
|
|
//NewGameInSlot(num);
|
|
StartCoroutine(StartScene(NewGameInSlot, num));
|
|
}
|
|
}
|
|
|
|
private delegate void startSceneCallback(int num);
|
|
|
|
private IEnumerator StartScene(startSceneCallback callback, int num)
|
|
{
|
|
if (m_cameraAnimator != null)
|
|
{
|
|
m_cameraAnimator.SetTrigger("Door");
|
|
}
|
|
|
|
yield return new WaitForSeconds(m_cameraDoorTime);
|
|
callback(num);
|
|
yield return null;
|
|
}
|
|
|
|
private void OnLoadGameSlotSelected(int num)
|
|
{
|
|
Debug.Log("OnLoadGameSlot " + num);
|
|
if (SaveSystem.HasSavedGameInSlot(num))
|
|
{
|
|
SaveUtility.Instance.SetCurrentSlot(num);
|
|
//SaveSystem.LoadFromSlot(num);
|
|
StartCoroutine(StartScene(SaveSystem.LoadFromSlot, num));
|
|
}
|
|
else
|
|
{
|
|
//NewGameInSlot(num);
|
|
}
|
|
}
|
|
|
|
private void OnDifficulty(int difficulty)
|
|
{
|
|
for (int i = 0; i < (int)GameStateManager.Difficulty.COUNT; i++)
|
|
{
|
|
var txt = m_difficultyButtons[i].GetComponentInChildren<TextMeshProUGUI>();
|
|
txt.color = i == difficulty ? Color.white : Color.gray;
|
|
/*
|
|
var colors = m_difficultyButtons[i].colors;
|
|
colors.normalColor = i == difficulty ? Color.white : Color.gray;
|
|
m_difficultyButtons[i].colors = colors;
|
|
*/
|
|
}
|
|
}
|
|
|
|
public void OnDifficultySelected(int difficulty)
|
|
{
|
|
OnMainMenu();
|
|
OnDifficulty(difficulty);
|
|
SaveUtility.Instance.SetCurrentDifficulty(difficulty);
|
|
GameStateManager.Instance.SetDifficulty((GameStateManager.Difficulty)difficulty);
|
|
}
|
|
|
|
public void OnDifficulty()
|
|
{
|
|
SetState(State.DIFFICULTY);
|
|
}
|
|
|
|
public void OnContinue()
|
|
{
|
|
int id = SaveUtility.Instance.GetCurrentSlot();
|
|
if (id < 0)
|
|
{
|
|
Debug.LogError("SaveGame error: slot id < 0");
|
|
return;
|
|
}
|
|
//SaveSystem.LoadFromSlot(id);
|
|
if (m_doorAnimator)
|
|
{
|
|
m_doorAnimator.Play("Open");
|
|
}
|
|
StartCoroutine(StartScene(SaveSystem.LoadFromSlot, id));
|
|
}
|
|
|
|
public void OnPlayTrailer()
|
|
{
|
|
VideoCutsceneController.Instance.PlayVideo();
|
|
}
|
|
|
|
public void OnBackButton()
|
|
{
|
|
if (m_doorAnimator)
|
|
{
|
|
m_doorAnimator.Play("Close");
|
|
}
|
|
SetState(State.MAIN_MENU);
|
|
}
|
|
|
|
private void OnNewGame()
|
|
{
|
|
if (m_doorAnimator)
|
|
{
|
|
m_doorAnimator.Play("Open");
|
|
}
|
|
|
|
m_mainButtons.SetActive(false);
|
|
m_newGamePage.SetActive(true);
|
|
for (int i = 0; i < m_slotSelector.transform.childCount; i++)
|
|
{
|
|
var slot = m_slotSelector.transform.GetChild(i).gameObject;
|
|
//Destroy(m_slotSelector.transform.GetChild(i).gameObject);
|
|
var slotTxt = slot.GetComponentInChildren<TextMeshProUGUI>();
|
|
var slotButton = slot.GetComponentInChildren<Button>();
|
|
if (SaveSystem.HasSavedGameInSlot(i))
|
|
{
|
|
string s = SaveUtility.Instance.GetSlotDesc(i);
|
|
if (String.IsNullOrEmpty(s))
|
|
s = "Unknown";
|
|
slotTxt.text = s;
|
|
}
|
|
int id = i;
|
|
slotButton.onClick.AddListener(() =>
|
|
{
|
|
OnNewGameSlotSelected(id);
|
|
});
|
|
}
|
|
}
|
|
|
|
private void OnLoadGame()
|
|
{
|
|
if (m_doorAnimator)
|
|
{
|
|
m_doorAnimator.Play("Open");
|
|
}
|
|
m_mainButtons.SetActive(false);
|
|
m_newGamePage.SetActive(true);
|
|
for (int i = 0; i < m_slotSelector.transform.childCount; i++)
|
|
{
|
|
var slot = m_slotSelector.transform.GetChild(i).gameObject;
|
|
//Destroy(m_slotSelector.transform.GetChild(i).gameObject);
|
|
var slotTxt = slot.GetComponentInChildren<TextMeshProUGUI>();
|
|
var slotButton = slot.GetComponentInChildren<Button>();
|
|
if (SaveSystem.HasSavedGameInSlot(i))
|
|
{
|
|
string s = SaveUtility.Instance.GetSlotDesc(i);
|
|
if (String.IsNullOrEmpty(s))
|
|
s = "Unknown";
|
|
slotTxt.text = s;
|
|
slotButton.interactable = true;
|
|
}
|
|
else
|
|
{
|
|
slotButton.interactable = false;
|
|
}
|
|
int id = i;
|
|
slotButton.onClick.AddListener(() =>
|
|
{
|
|
OnLoadGameSlotSelected(id);
|
|
});
|
|
}
|
|
}
|
|
|
|
// Update is called once per frame
|
|
private void Update()
|
|
{
|
|
}
|
|
}
|
|
} |