-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameManager.cs
252 lines (214 loc) · 8.14 KB
/
GameManager.cs
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class GameManager : MonoBehaviour
{
public static GameManager instance; //Singleton
[SerializeField] private bool setupGameOnStart;
[SerializeField] private bool loadAllEnemiesOnStart;
[SerializeField] private GameObject targetDisplay;
[Tooltip("Set the total turn points that the characters will take in a turn")]
[SerializeField] private int totalTurnPoints = 2; //Total points for the characters to play
[Tooltip("This is to track the total remaining turn points")]
public int turnPointsRemaining; //Tracking the remaining pointss
public int currentActionCost = 1;
private CharacterControl activePlayer;
private int currentChar; //Current Character
private List<CharacterControl> playerTeam = new List<CharacterControl>(); //Controls player team
private List<CharacterControl> enemyTeam = new List<CharacterControl>(); //Controls enemy team
private List<CharacterControl> activeEnemies = new List<CharacterControl>(); //Controls enemy team
public IEnumerable<CharacterControl> AllCharacters => playerTeam.Concat(activeEnemies);
public List<CharacterControl> PlayerTeam => playerTeam;
public List<CharacterControl> ActiveEnemies => activeEnemies;
public GameObject TargetDisplay => targetDisplay;
public CharacterControl ActivePlayer => activePlayer;
private void Awake()
{
instance = this; //Singleton
}
private void Start()
{
LevelGenerator.Instance?.OnLevelGenerationCompleted.AddListener(OnLevelGenerationCompleted);
if (setupGameOnStart)
{
SetupGame();
}
}
private void OnLevelGenerationCompleted()
{
if(!setupGameOnStart)
{
StartCoroutine(SetupGameDelayed());
}
}
private IEnumerator SetupGameDelayed()
{
yield return new WaitForEndOfFrame();
yield return new WaitForEndOfFrame();
yield return new WaitForEndOfFrame();
SetupGame();
}
public void SetupGame()
{
List<CharacterControl> tempList = new List<CharacterControl>();
tempList.AddRange(FindObjectsOfType<CharacterControl>());
//IMPLEMENT COIN TOSS METHOD (50% CHANCE TO DECIDE WHICH TEAM TO START)
foreach (CharacterControl cc in tempList) //Adding teams into the list
{
if (cc.isPlayer)
{
playerTeam.Add(cc);
}
else
{
enemyTeam.Add(cc);
}
}
playerTeam = playerTeam.OrderBy(x => Random.Range(int.MinValue, int.MaxValue)).ToList();
enemyTeam = enemyTeam.OrderBy(x => Random.Range(int.MinValue, int.MaxValue)).ToList();
if(loadAllEnemiesOnStart)
{
activeEnemies = new List<CharacterControl>(enemyTeam);
}
activePlayer = AllCharacters.First();
if (LevelManager.Instance?.PlayerCanControl ?? true)
{
CameraSystem.instance.SetMoveTarget(ActivePlayer.transform.position);
}
currentChar = -1; //Skipping the first character whether its an enemy
EndTurn();
}
public void AddNewEnemies(List<CharacterControl> enemies)
{
List<CharacterControl> tempList = new List<CharacterControl>(enemies);
int iterations = tempList.Count + 50;
while (tempList.Count > 0 && iterations > 0)
{
int randomPick = Random.Range(0, tempList.Count);
enemyTeam.Add(tempList[randomPick]);
activeEnemies.Add(tempList[randomPick]);
tempList.RemoveAt(randomPick);
iterations--;
}
}
public void FinishedMovement()
{
if (ActivePlayer.isPlayer)
{
var roomPos = new Vector2Int(Mathf.RoundToInt(ActivePlayer.transform.position.x / LevelGenerator.TileSize), Mathf.RoundToInt(ActivePlayer.transform.position.z / LevelGenerator.TileSize));
var room = LevelGenerator.Instance?.GetRoomAt(roomPos);
if(room != null)
{
foreach(var enemy in enemyTeam)
{
if (activeEnemies.Contains(enemy)) continue;
var enemyRoomPos = new Vector2Int(
Mathf.RoundToInt(enemy.transform.position.x / LevelGenerator.TileSize),
Mathf.RoundToInt(enemy.transform.position.z / LevelGenerator.TileSize));
if(LevelGenerator.Instance?.GetRoomAt(enemyRoomPos) == room)
{
activeEnemies.Add(enemy);
}
}
}
}
SpendTurnPoints();
}
public void SpendTurnPoints()
{
if (activeEnemies.Count > 0)
{
turnPointsRemaining -= currentActionCost; //Spend the turn points
}
PlayerInputMenu.instance.UpdateTurnPointText(turnPointsRemaining); //Update the turn point
if (turnPointsRemaining <= 0) //If the turn points <= 0...
{
PlayerInputMenu.instance.selectedWalk = false; //Reset the animations
PlayerInputMenu.instance.selectedDash = false; //Reset the animations
EndTurn();
}
else
{
if (ActivePlayer.isPlayer) //If the player is not an enemy...
{
if (LevelManager.Instance?.PlayerCanControl ?? true)
{
PlayerInputMenu.instance.ShowClassPanel(); //Show the class panel
}
}
else //If it's an enemy...
{
PlayerInputMenu.instance.HideClassPanel(); //Hide the class panel
ActivePlayer.aiBrain.ChooseAction();
}
}
}
public void EndTurn()
{
CleanupProcess();
currentChar++;
if (currentChar >= AllCharacters.Count()) //Returning back to the first character if it's above the count limit
{
currentChar = 0;
}
activePlayer = AllCharacters.ToList()[currentChar]; //Switch the current character
if (LevelManager.Instance?.PlayerCanControl ?? true)
{
CameraSystem.instance.SetMoveTarget(ActivePlayer.transform.position);//Move the camera
}
turnPointsRemaining = totalTurnPoints; //Set the turn points
if (ActivePlayer.isPlayer) //If the active player is not an enemy...
{
if (ActivePlayer.isStunned)
{
ActivePlayer.isStunned = false;
ActivePlayer.stunParticle.Stop();
EndTurn();
}
if (LevelManager.Instance?.PlayerCanControl ?? true)
{
PlayerInputMenu.instance.ShowClassPanel(); //Show the class panel
}
}
else //If it's an enemy character...
{
PlayerInputMenu.instance.HideClassPanel(); //Hide class panel
if (ActivePlayer.isStunned)
{
ActivePlayer.isStunned = false;
ActivePlayer.stunParticle.Stop();
EndTurn();
}
else
{
ActivePlayer.aiBrain.ChooseAction();
}
}
currentActionCost = 1;
PlayerInputMenu.instance.UpdateTurnPointText(turnPointsRemaining);
}
#region Cleanup Related
public void CleanupProcess()
{
foreach (CharacterControl cc in AllCharacters.ToList())
{
if (cc.currentHealth <= 0)
{
if (playerTeam.Contains(cc)) //If the player team has it
{
playerTeam.Remove(cc); // Remove from the player team list once the character is dead
}
if (activeEnemies.Contains(cc)) //If enemy team has it
{
activeEnemies.Remove(cc); //Remove the character from the enemy list
}
if (enemyTeam.Contains(cc)) //If enemy team has it
{
enemyTeam.Remove(cc); //Remove the character from the enemy list
}
}
}
}
#endregion
}