-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameManager.cs
More file actions
103 lines (94 loc) · 2.72 KB
/
GameManager.cs
File metadata and controls
103 lines (94 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public LevelsBuilder m_LevelsBuilder;
public GameObject m_ResetButton;
private bool m_ReadyForInput;
private Player m_Player;
public int brLevela;
private Timer theTimer;
void Start()
{
theTimer = FindObjectOfType<Timer>();
m_ResetButton.SetActive(true);
ResetScene();
}
void Update()
{
Vector2 moveInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
moveInput.Normalize();
if (moveInput.sqrMagnitude > 0.5)
{
if (m_ReadyForInput)
{
m_ReadyForInput = false;
m_Player.Move(moveInput);
EndGame();
}
}
else
{
m_ReadyForInput = true;
}
}
public void NextLevel()
{
m_LevelsBuilder.NextLevel();
StartCoroutine(ResetSceneASync());
brLevela++;
}
public void ResetScene()
{
StartCoroutine(ResetSceneASync());
}
public void EndGame()
{
if (brLevela == 4 && IsLevelComplete())
{
m_ResetButton.SetActive(false);
theTimer.scoreIncreasing = false;
theTimer.GameEnd();
}
else if (IsLevelComplete())
{
NextLevel();
}
}
bool IsLevelComplete()
{
Box[] boxes = FindObjectsOfType<Box>();
foreach (var box in boxes)
{
if (!box.m_OnCross) return false;
}
return true;
}
IEnumerator ResetSceneASync()
{
if (SceneManager.sceneCount > 1)
{
AsyncOperation asyncUnload = SceneManager.UnloadSceneAsync("LevelScene");
while (!asyncUnload.isDone)
{
yield return null;
Debug.Log("UnLoading...");
}
Debug.Log("Unload Done");
Resources.UnloadUnusedAssets();
}
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync("LevelScene", LoadSceneMode.Additive);
while (!asyncLoad.isDone)
{
yield return null;
Debug.Log("Loading...");
}
SceneManager.SetActiveScene(SceneManager.GetSceneByName("LevelScene"));
m_LevelsBuilder.Build();
m_Player = FindObjectOfType<Player>();
Debug.Log("Level loaded " + brLevela);
}
}