Browse Source

Started to clean up GameManager + merged with Angus's changes

Josh_Dev_branch
JoshuaReason 5 years ago
parent
commit
86b613e888
4 changed files with 108 additions and 56 deletions
  1. +3
    -3
      Assets/Scripts/GameMode/ColorGameMode/ColorGameMode.cs
  2. +37
    -9
      Assets/Scripts/GameMode/GameMode.cs
  3. +67
    -43
      Assets/Scripts/Managers/GameManager.cs
  4. +1
    -1
      Assets/Scripts/Managers/GameManagerRacetrack.cs

+ 3
- 3
Assets/Scripts/GameMode/ColorGameMode/ColorGameMode.cs View File

@ -66,7 +66,7 @@ public class ColorGameMode : GameMode
/// <param name="character">Character which moved</param> /// <param name="character">Character which moved</param>
/// <param name="client">Client of the character</param> /// <param name="client">Client of the character</param>
/// <param name="currentBlock">Block moved onto</param> /// <param name="currentBlock">Block moved onto</param>
protected override void OnPlayerFinishedMove(Character character, ClientData client, Block currentBlock)
protected override void OnPlayerMoved(Character character, ClientData client, Block currentBlock)
{ {
handleFalling(character, client, currentBlock, character.justMoved); handleFalling(character, client, currentBlock, character.justMoved);
@ -127,7 +127,7 @@ public class ColorGameMode : GameMode
} }
protected override void OnAllPlayersFinishedMove(PlayerData[] allPlayers)
protected override void OnAllPlayersMoved(PlayerData[] allPlayers)
{ {
foreach (PlayerData player in allPlayers) foreach (PlayerData player in allPlayers)
{ {
@ -207,7 +207,7 @@ public class ColorGameMode : GameMode
RoundCount = 0; RoundCount = 0;
for(int i = 0; i < allPlayers.Length; i++) for(int i = 0; i < allPlayers.Length; i++)
{ {
OnPlayerFinishedMove(allPlayers[i].character, allPlayers[i].client, allPlayers[i].character.CurrentBlock);
OnPlayerMoved(allPlayers[i].character, allPlayers[i].client, allPlayers[i].character.CurrentBlock);
} }
} }
} }

+ 37
- 9
Assets/Scripts/GameMode/GameMode.cs View File

@ -37,13 +37,13 @@ public abstract class GameMode : ScriptableObject
/// <param name="character">Character which moved</param> /// <param name="character">Character which moved</param>
/// <param name="client">Client which controls character</param> /// <param name="client">Client which controls character</param>
/// <param name="currentBlock">Block which the character finished on</param> /// <param name="currentBlock">Block which the character finished on</param>
protected abstract void OnPlayerFinishedMove(Character character, ClientData client, Block currentBlock);
protected abstract void OnPlayerMoved(Character character, ClientData client, Block currentBlock);
/// <summary> /// <summary>
/// Called once after every player has finished one move /// Called once after every player has finished one move
/// </summary> /// </summary>
/// <param name="allCharacters">List of all player Info</param> /// <param name="allCharacters">List of all player Info</param>
protected abstract void OnAllPlayersFinishedMove(PlayerData[] allPlayers);
protected abstract void OnAllPlayersMoved(PlayerData[] allPlayers);
public abstract bool isGameOver(PlayerData[] allPlayers); public abstract bool isGameOver(PlayerData[] allPlayers);
@ -59,6 +59,12 @@ public abstract class GameMode : ScriptableObject
return retVal; return retVal;
} }
public void PreGameStart()
{
}
public void GameStart(PlayerData[] allPlayers) public void GameStart(PlayerData[] allPlayers)
{ {
gameTimes = new List<float>(RoundTimes); gameTimes = new List<float>(RoundTimes);
@ -73,21 +79,31 @@ public abstract class GameMode : ScriptableObject
OnGameStart(allPlayers); OnGameStart(allPlayers);
} }
public void InputStart(PlayerData[] AllPlayers)
{
}
public void InputEnd(PlayerData[] AllPlayers)
{
}
public void RoundStart(PlayerData[] allPlayers) public void RoundStart(PlayerData[] allPlayers)
{ {
OnRoundStart(allPlayers); OnRoundStart(allPlayers);
RoundStartEvent?.Invoke(); RoundStartEvent?.Invoke();
} }
public void FinishedMove(PlayerData[] allPlayers)
public void AllPlayersMoved(PlayerData[] allPlayers)
{ {
OnAllPlayersFinishedMove(allPlayers);
OnAllPlayersMoved(allPlayers);
AllPlayersMovedEvent?.Invoke(); AllPlayersMovedEvent?.Invoke();
} }
public void OnePlayerMoved(PlayerData player)
public void PlayerMoved(PlayerData player)
{ {
OnPlayerFinishedMove(player.character, player.client, player.character.CurrentBlock);
OnPlayerMoved(player.character, player.client, player.character.CurrentBlock);
} }
public void RoundEnd(PlayerData[] allPlayers) public void RoundEnd(PlayerData[] allPlayers)
@ -96,7 +112,7 @@ public abstract class GameMode : ScriptableObject
RoundEndEvent?.Invoke(); RoundEndEvent?.Invoke();
} }
public void GameOver(PlayerData[] allPlayers)
public void GameEnd(PlayerData[] allPlayers)
{ {
OnGameOver(allPlayers); OnGameOver(allPlayers);
OnGameOverEvent?.Invoke(); OnGameOverEvent?.Invoke();
@ -110,17 +126,29 @@ public abstract class GameMode : ScriptableObject
* GameStart(PlayerData[] AllPlayers); //Called once after players have Spawned * GameStart(PlayerData[] AllPlayers); //Called once after players have Spawned
* *
* OnInputStart(PlayerData[] AllPlayers) //Called when players enter input mode * OnInputStart(PlayerData[] AllPlayers) //Called when players enter input mode
* OnRecievedPlayerInput(PlayerData Player) //Called when a specific player has sent their input
* OnInputEnd(PlayerData[] AllPlayers) //Called when player input mode has finished * OnInputEnd(PlayerData[] AllPlayers) //Called when player input mode has finished
* *
* OnRoundStart(PlayerData[] AllPlayers) //Just before Players start moving * OnRoundStart(PlayerData[] AllPlayers) //Just before Players start moving
* *
* ------- Start Round Loop -------
*
* OnPlayerMoved(PlayerData player) //Called after each player moves * OnPlayerMoved(PlayerData player) //Called after each player moves
* IsGameOver(PlayerData[] AllPlayers) //Called after each player moves * IsGameOver(PlayerData[] AllPlayers) //Called after each player moves
* *
* AllPlayersMoved();
* AllPlayersMoved(PlayerData[] AllPlayers) //Called after all players have moved
* EnvironmentTurn() //Also called after all players have moved but should be kept specialised to environment stuff
* IsGameOver(PlayerData[] AllPlayers) //We need to check if the environment terminated the game
*
* --------- End Game Loop ---------
*
* OnRoundEnd(PlayerData[] AllPlayers) //Called after movements are made
* IsGameOver(PlayerData[] AllPlayers) //This needs to be checked again
*
* GameEnd(PlayerData[] AllPlayers)
* *
* *
* *
* *
* *
*/
*/

+ 67
- 43
Assets/Scripts/Managers/GameManager.cs View File

@ -40,7 +40,7 @@ public class GameManager : MonoBehaviour
/// <summary> /// <summary>
/// Easy access to IEnumerable in playerData so we can Enumerate through it /// Easy access to IEnumerable in playerData so we can Enumerate through it
/// </summary> /// </summary>
private IEnumerable<PlayerData> playerDataAsArray { get { return playerData.Values; } }
private PlayerData[] playerDataAsArray { get { return playerData.Values.ToArray(); } }
/// <summary> /// <summary>
/// Easy access to GameMode value in CurrentGameMode reference /// Easy access to GameMode value in CurrentGameMode reference
@ -80,6 +80,34 @@ public class GameManager : MonoBehaviour
} }
#endregion Unity Functions #endregion Unity Functions
private IEnumerator GameRoutine()
{
//Allows game mode to instantiate anything it might need;
gameMode.PreGameStart();
//Spawn Characters and tell let the GameMode do anything with the characters it might want
SpawnCharacters();
gameMode.GameStart(playerDataAsArray);
//Loop until the GameMode lets us know the game is over
while (!gameMode.isGameOver(playerDataAsArray))
{
//wait until we have recieved all player input
yield return StartCoroutine(WaitForPlayerInput());
//Routine for players movement
yield return StartCoroutine(RoundRoutine()); //it's pretty long so it gets it's own coroutine;
}
//Let the gamemode know that the game is over
gameMode.GameEnd(playerDataAsArray);
}
IEnumerator displayforSeconds(GameObject display, float time) IEnumerator displayforSeconds(GameObject display, float time)
{ {
display.SetActive (true); display.SetActive (true);
@ -87,63 +115,58 @@ public class GameManager : MonoBehaviour
display.SetActive (false); display.SetActive (false);
} }
private void DoRoundRoutine()
{
Debug.Log("Starting Round");
StartCoroutine(RoundRoutine());
}
private void StartRound()
private IEnumerator WaitForPlayerInput()
{ {
gameMode.RoundStart(playerDataAsArray.ToArray());
LogicProtocols.FloatMsg RoundTime = new LogicProtocols.FloatMsg( gameMode.GetRoundTime());
bspawn.Spawn();
LogicProtocols.FloatMsg RoundTime = new LogicProtocols.FloatMsg(gameMode.GetRoundTime());
playerDataAsArray.ForEach(p => p.client.conn.Send(LogicProtocols.SendRoundTime, RoundTime)); playerDataAsArray.ForEach(p => p.client.conn.Send(LogicProtocols.SendRoundTime, RoundTime));
ClientList.ForEach(p => p.ChangeScene("ClientScene"));
gameMode.InputStart(playerDataAsArray);
yield return new WaitUntil(() => playerData.All(p => p.Value.recievedList));
//reset
playerDataAsArray.ForEach(p => p.recievedList = false); //reset all players list
gameMode.InputEnd(playerDataAsArray);
} }
private IEnumerator RoundRoutine() private IEnumerator RoundRoutine()
{ {
playerDataAsArray.ForEach(p => p.recievedList = false);
//Debug.Log("Doing Round Routine");
//Tell the gamemode that we are starting a round
gameMode.RoundStart(playerDataAsArray);
//Loop until all players have finished moving
while (playerDataAsArray.Any(p => !p.blockReader.Finished)) while (playerDataAsArray.Any(p => !p.blockReader.Finished))
{ {
//Debug.Log("One Move");
//Loop through all players
foreach (PlayerData player in playerDataAsArray) foreach (PlayerData player in playerDataAsArray)
{ {
Debug.Log(player.client.Name);
StartCoroutine(RunOnce(player));
yield return new WaitUntil(() => player.waiting);
yield return StartCoroutine(MoveRoutine(player));//Move Player
gameMode.PlayerMoved(player);//LetGameModeKnow
} }
//Let Gamemode know all players have moved
gameMode.AllPlayersMoved(playerDataAsArray.ToArray());
playerDataAsArray.ForEach(p => p.client.SendScore()); //Update the players score
//wait until all players have finished
//yield return new WaitUntil(() => playerArray.All(p => p.waiting));
gameMode.FinishedMove(playerDataAsArray.ToArray());
playerDataAsArray.ForEach(p => p.client.SendScore());
//if Game is over break out of loop
if (gameMode.isGameOver(playerDataAsArray))
break;
} }
if (gameMode.isGameOver(playerDataAsArray.ToArray()))
{
Debug.Log("Game Over");
SceneManager.LoadScene("ScoreBoards");
}
//Let GameMode know that Round is Over
gameMode.RoundEnd(playerDataAsArray.ToArray()); gameMode.RoundEnd(playerDataAsArray.ToArray());
//Reset some player Data
foreach (PlayerData player in playerDataAsArray) foreach (PlayerData player in playerDataAsArray)
{ {
player.blockReader.Reset(); player.blockReader.Reset();
player.waiting = false;
player.client.SendInventory(); player.client.SendInventory();
player.client.ChangeScene("ClientScene");
} }
//Debug.Log("Finished Moving");
StartRound();
} }
private void SpawnCharacters() private void SpawnCharacters()
@ -223,28 +246,30 @@ public class GameManager : MonoBehaviour
#endregion Networking Functions #endregion Networking Functions
private IEnumerator RunOnce(PlayerData data)
private IEnumerator MoveRoutine(PlayerData data)
{ {
data.waiting = false;
bool blockFinished = false; bool blockFinished = false;
float waitTime; float waitTime;
//Loop until the current block indicates or the reader indicates it has finished
while (!blockFinished && !data.blockReader.Finished) while (!blockFinished && !data.blockReader.Finished)
{ {
//Debug.Log(data.client + "Moving once");
//If the current block hasn't already been removed, remove it from the player inventory
//We need to check if it has already been removed so loops don't eat an entire stack
if (data.blockReader.CurrentBlock != null && !data.blockReader.CurrentBlock.hasBeenRemoved) if (data.blockReader.CurrentBlock != null && !data.blockReader.CurrentBlock.hasBeenRemoved)
{ {
data.client.Inventory.Remove(data.blockReader.CurrentBlock); data.client.Inventory.Remove(data.blockReader.CurrentBlock);
data.blockReader.CurrentBlock.hasBeenRemoved = true; data.blockReader.CurrentBlock.hasBeenRemoved = true;
} }
//Process the move
blockFinished = data.blockReader.Read(data.character, AnimationTime,out waitTime); blockFinished = data.blockReader.Read(data.character, AnimationTime,out waitTime);
//Debug.Log("Waiting: " + waitTime);
//Wait for the animation to finish
yield return new WaitForSeconds(waitTime); yield return new WaitForSeconds(waitTime);
gameMode.OnePlayerMoved(data);
} }
data.waiting = true;
} }
} }
@ -255,7 +280,6 @@ public class PlayerData
public ClientData client; public ClientData client;
public bool recievedList; public bool recievedList;
public bool waiting;
public PlayerData(Character character, ClientData client) public PlayerData(Character character, ClientData client)
{ {

+ 1
- 1
Assets/Scripts/Managers/GameManagerRacetrack.cs View File

@ -182,7 +182,7 @@ public class GameManagerRacetrack : MonoBehaviour
//Debug.Log("Waiting: " + waitTime); //Debug.Log("Waiting: " + waitTime);
yield return new WaitForSeconds(waitTime); yield return new WaitForSeconds(waitTime);
gamemode.OnePlayerMoved(data);
gamemode.PlayerMoved(data);
} }
data.waiting = true; data.waiting = true;
} }

Loading…
Cancel
Save