init: version 1
This commit is contained in:
63
Assets/Prefabs/Managers/Scripts/GameManager.cs
Normal file
63
Assets/Prefabs/Managers/Scripts/GameManager.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// GameManager is a singleton that manages the game state of
|
||||
/// a single scene.
|
||||
/// </summary>
|
||||
public class GameManager : MonoBehaviour
|
||||
{
|
||||
public static GameManager Instance => _instance;
|
||||
private static GameManager _instance;
|
||||
|
||||
public MapSwitchEvent onMapSwitch;
|
||||
public RespawnEvent onRespawn;
|
||||
|
||||
public int currentMapIdx;
|
||||
public List<GameObject> maps;
|
||||
public List<CubeController> cubes;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (_instance != null && _instance != this)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
_instance = this;
|
||||
}
|
||||
|
||||
public void SwitchMap(int mapIdx)
|
||||
{
|
||||
Vector3 offset = maps[mapIdx].transform.position - maps[currentMapIdx].transform.position;
|
||||
currentMapIdx = mapIdx;
|
||||
onMapSwitch?.Invoke(currentMapIdx, offset);
|
||||
}
|
||||
|
||||
public void ResetCubes()
|
||||
{
|
||||
foreach (var cube in cubes.Where(x=> x.currentMap == currentMapIdx && x.originMap != x.currentMap))
|
||||
{
|
||||
cube.Respawn();
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!Input.GetKeyDown(KeyCode.J)) return;
|
||||
SwitchMap((currentMapIdx + 1) % maps.Count);
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class MapSwitchEvent : UnityEvent<int, Vector3>
|
||||
{
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class RespawnEvent : UnityEvent
|
||||
{
|
||||
}
|
||||
11
Assets/Prefabs/Managers/Scripts/GameManager.cs.meta
Normal file
11
Assets/Prefabs/Managers/Scripts/GameManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e76b69a7b31c4556ab9063967e140e54
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: -100
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
12
Assets/Prefabs/Managers/Scripts/MenuManager.cs
Normal file
12
Assets/Prefabs/Managers/Scripts/MenuManager.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// MenuManager manages the transitions from the menu screen to the game levels.
|
||||
/// </summary>
|
||||
public class MenuManager : MonoBehaviour
|
||||
{
|
||||
public void ToLevel(int level)
|
||||
{
|
||||
TransitionManager.Instance.ToLevel(level);
|
||||
}
|
||||
}
|
||||
3
Assets/Prefabs/Managers/Scripts/MenuManager.cs.meta
Normal file
3
Assets/Prefabs/Managers/Scripts/MenuManager.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: df5754d43d0942b489107176ee01993e
|
||||
timeCreated: 1752740870
|
||||
54
Assets/Prefabs/Managers/Scripts/TransitionManager.cs
Normal file
54
Assets/Prefabs/Managers/Scripts/TransitionManager.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// TransitionManager is a singleton that manages the transitions
|
||||
/// between levels in the game.
|
||||
/// </summary>
|
||||
public class TransitionManager : MonoBehaviour
|
||||
{
|
||||
public static TransitionManager Instance => _instance;
|
||||
private static TransitionManager _instance;
|
||||
|
||||
public int currentLevel;
|
||||
|
||||
private Animator _animator;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (_instance != null && _instance != this)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
_instance = this;
|
||||
DontDestroyOnLoad(gameObject);
|
||||
|
||||
_animator = GetComponentInChildren<Animator>();
|
||||
}
|
||||
|
||||
public void NextLevel()
|
||||
{
|
||||
ToLevel(currentLevel + 1);
|
||||
}
|
||||
|
||||
public void ToLevel(int level)
|
||||
{
|
||||
currentLevel = level;
|
||||
var scene = $"Level {level}";
|
||||
if (Application.CanStreamedLevelBeLoaded(scene))
|
||||
StartCoroutine(LoadLevel(scene));
|
||||
else
|
||||
{
|
||||
currentLevel = 0;
|
||||
StartCoroutine(LoadLevel("Menu"));
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator LoadLevel(string levelName)
|
||||
{
|
||||
_animator.SetTrigger("Start");
|
||||
yield return new WaitForSeconds(.5f);
|
||||
UnityEngine.SceneManagement.SceneManager.LoadScene(levelName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 89e423dc0c9ad138ebf86acee5de9581
|
||||
Reference in New Issue
Block a user