/
GameManager.cs
159 lines (132 loc) · 4.04 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
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using System.Collections.Generic;
public class GameManager : MonoBehaviour {
private int players = 1;
private Player currentPlayer;
private bool pinMachineSwiping = false;
private List<Player> playas = new List<Player> ();
private PlayerCountGO pcGO;
private PinSetter pinSetter;
private ScoreDisplay scoreDisplay;
private LevelManager levelManager;
static GameManager instance = null;
void Awake(){
//Allows the game object to transfer between scenes
GameObject.DontDestroyOnLoad (gameObject);
}
// Use this for initialization
void Start () {
pinSetter = GameObject.FindObjectOfType<PinSetter> ();
scoreDisplay = GameObject.FindObjectOfType<ScoreDisplay> ();
pcGO = GameObject.FindObjectOfType<PlayerCountGO> ();
levelManager = GameObject.FindObjectOfType <LevelManager> ();
try{
players = pcGO.GetPlayerCount ();
}catch{
Debug.LogWarning ("WARNING: no PlayerCountGO found. Did you start from menu?");
}
CreatePlayers ();
}
void Update(){
//Deletes current instance of the Game Manager at the _StartMenu scene in order for a new one to be created at game start without conflicts
if (SceneManager.GetActiveScene ().name.Equals ("_StartMenu")){
Destroy(gameObject);
}
}
// Creates players, initalizes them with name and player color.
public void CreatePlayers(){
for (int i = 0; i < players; i++){
playas.Add (new Player ("Player"+(i+1), pcGO.GetPlayerColorsArray()[i]));
}
currentPlayer = playas [0];
}
// Handles the bowls (rolls) made by players
public void Bowl (int pinFall){
List<int> bowls = currentPlayer.GetBowls ();
try{
bowls.Add (pinFall);
ActionMaster.Action nextAction = ActionMaster.NextAction (bowls);
pinSetter.PinMachine (nextAction);
UpdateScore (bowls);
if(nextAction == ActionMaster.Action.Tidy){
StartCoroutine(IsSwiping (5.5f, true));
}
if(nextAction == ActionMaster.Action.EndTurn){
StartCoroutine(IsSwiping (5.5f, false));
}else if(nextAction == ActionMaster.Action.EndGame){
currentPlayer.PlayerFinishedGame();
StartCoroutine(IsSwiping (5.5f, false));
}
}catch{
Debug.LogError ("Something went wrong in Bowl()");
}
}
//Allows the swiper to clean up the lane before next players turn
IEnumerator IsSwiping(float waitTime, bool tidy){
pinMachineSwiping = true;
yield return new WaitForSeconds (5.5f);
Time.timeScale = 1;
pinMachineSwiping = false;
if (!tidy) {
NextPlayer ();
}
}
/* Controls switching between players
* If a player is finished, the turn is passed on until all players are finished
*/
public void NextPlayer(){
if (AllPlayersFinished ()) {
GameOver ();
}
if (playas.Count - 1 > playas.IndexOf (currentPlayer)) {
currentPlayer = playas [playas.IndexOf (currentPlayer) + 1];
currentPlayer.GetBall().Reset ();
if (currentPlayer.GetPlayerFinished()) {
NextPlayer ();
}
} else {
currentPlayer = playas [0];
currentPlayer.GetBall().Reset ();
}
currentPlayer.UpdateBallMaterial ();
scoreDisplay.ClearScoreCard ();
UpdateScore (currentPlayer.GetBowls ());
}
void UpdateScore(List<int> bowls){
try{
scoreDisplay.FillRollCard (bowls);
scoreDisplay.FillFrames (ScoreMaster.ScoreCumulative(bowls));
}catch{
Debug.LogWarning ("FillRollCard() error");
}
}
//Returns true if all players are finished, false if not.
bool AllPlayersFinished(){
int finishedPlayerCount = 0;
foreach (Player player in playas){
if (player.GetPlayerFinished()) {
finishedPlayerCount++;
if (finishedPlayerCount == players) {
return true;
}
}
}
return false;
}
//Fills pcGO's player array with the players. Loads the scoring scene.
void GameOver(){
pcGO.FillAllPlayersArray (playas); //Copy Player references to persistent GO for scoring purposes
levelManager.LoadNextLevel ();
}
public bool IsPinMachineBusy(){
return pinMachineSwiping;
}
public void SetPlayerCount(int playerCount){
players = playerCount;
}
public List<Player> GetPlayers(){
return playas;
}
}