rabidus-test/Assets/BNG Framework/Scripts/Extras/SceneLoader.cs

115 lines
2.9 KiB
C#
Raw Normal View History

2023-08-22 15:41:12 +03:00
using QFSW.QC;
using System.Collections;
2023-07-24 16:38:13 +03:00
using System.Collections.Generic;
2023-10-02 19:12:35 +03:00
using System.Linq;
2023-07-24 16:38:13 +03:00
using UnityEngine;
using UnityEngine.SceneManagement;
namespace BNG {
public class SceneLoader : MonoBehaviour {
2023-10-02 19:12:35 +03:00
public List<string> AllMapsLevelNames = new List<string>();
2023-10-11 11:58:06 +03:00
public string ResultSceneName = "ResultScene";
2023-09-11 15:44:17 +03:00
public static SceneLoader Instance;
2023-10-11 15:44:05 +03:00
public int TournamentSceneCount = 3;
2023-10-11 11:58:06 +03:00
private List<string> _randomMapsList = new List<string>();
2023-09-11 15:44:17 +03:00
private void Awake()
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(gameObject);
}
2023-09-13 13:24:05 +03:00
}
2023-09-11 15:44:17 +03:00
2023-10-11 11:58:06 +03:00
2023-09-13 13:24:05 +03:00
private string _sceneName;
2023-10-11 11:58:06 +03:00
private void Start()
{
InitStartValues();
}
[ContextMenu("Debug test")]
private void InitStartValues()
{
_randomMapsList.Clear();
_randomMapsList = new List<string>();
2023-10-11 15:44:05 +03:00
_randomMapsList.AddRange(AllMapsLevelNames.OrderBy(x => Random.Range(0, 100)).Take(TournamentSceneCount));
2023-10-11 11:58:06 +03:00
_randomMapsList.Add(ResultSceneName);
GlobalSettings.Instance.AllMaps = false;
}
2023-10-02 19:12:35 +03:00
public void ChangeLevel()
2023-09-13 13:24:05 +03:00
{
2023-10-11 11:58:06 +03:00
var firstPersonAfterJack = _randomMapsList.SkipWhile(p => p != SceneManager.GetActiveScene().name).ElementAt(1);
2023-10-10 17:25:59 +03:00
LoadScene(firstPersonAfterJack);
2023-10-02 19:12:35 +03:00
}
2023-09-13 13:24:05 +03:00
2023-10-11 11:58:06 +03:00
public void LoadTournament()
{
LoadScene(_randomMapsList.First());
}
2023-10-10 17:25:59 +03:00
public void LoadScene(string sceneName)
2023-10-02 19:12:35 +03:00
{
_sceneName = sceneName;
2023-09-13 13:24:05 +03:00
StartCoroutine(LoadAsuncScene());
}
2023-07-24 16:38:13 +03:00
2023-10-11 11:58:06 +03:00
public void LoadStartScene()
{
InitStartValues();
LoadScene("StartScene");
}
2023-09-13 13:24:05 +03:00
private IEnumerator LoadAsuncScene()
{
var scene = SceneManager.LoadSceneAsync(_sceneName, LoadSceneMode.Single);
scene.allowSceneActivation = false;
while (!scene.isDone)
{
scene.allowSceneActivation = true;
yield return null;
2023-07-24 16:38:13 +03:00
}
}
2023-09-13 13:24:05 +03:00
private int _sceneIndex = 0;
2023-09-11 15:44:17 +03:00
private void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
SceneManager.LoadScene(0);
}
2023-09-13 13:24:05 +03:00
if (Input.GetKeyDown(KeyCode.Equals))
2023-09-11 15:44:17 +03:00
{
2023-09-13 13:24:05 +03:00
_sceneIndex++;
if (_sceneIndex >= SceneManager.sceneCountInBuildSettings)
_sceneIndex = 0;
2023-09-11 15:44:17 +03:00
2023-09-13 13:24:05 +03:00
SceneManager.LoadScene(_sceneIndex);
2023-07-24 16:38:13 +03:00
}
2023-09-13 13:24:05 +03:00
else if (Input.GetKeyDown(KeyCode.Minus))
{
_sceneIndex--;
if (_sceneIndex < 0)
_sceneIndex = SceneManager.sceneCountInBuildSettings - 1;
2023-07-24 16:38:13 +03:00
2023-09-13 13:24:05 +03:00
SceneManager.LoadScene(_sceneIndex);
2023-07-24 16:38:13 +03:00
}
}
}
}