From 8f6a4c49badfbe9522d01e49db9d8ec49298ab1a Mon Sep 17 00:00:00 2001 From: Joshua Reason Date: Mon, 26 Aug 2019 12:59:47 +1000 Subject: [PATCH] Cleaned up GameMode and GameManager --- .../Plugins/IngameDebugConsole/Android.meta | 8 - .../Plugins/IngameDebugConsole/Prefabs.meta | 8 - .../Plugins/IngameDebugConsole/Scripts.meta | 8 - .../IngameDebugConsole/Sprites/Unused.meta | 8 - .../{Models.meta => Scripts/Components.meta} | 2 +- .../ColorGameMode/RacetrackGameMode.cs | 6 +- Assets/Scripts/GameMode/GameMode.cs | 88 ++++++++--- Assets/Scripts/GameMode/GameModeEvents.cs | 3 + Assets/Scripts/LogicBlocks.meta | 8 - Assets/Scripts/Managers/GameManager.cs | 144 ++++++++---------- .../Scripts/Managers/GameManagerRacetrack.cs | 4 +- 11 files changed, 145 insertions(+), 142 deletions(-) delete mode 100644 Assets/Plugins/IngameDebugConsole/Android.meta delete mode 100644 Assets/Plugins/IngameDebugConsole/Prefabs.meta delete mode 100644 Assets/Plugins/IngameDebugConsole/Scripts.meta delete mode 100644 Assets/Plugins/IngameDebugConsole/Sprites/Unused.meta rename Assets/{Models.meta => Scripts/Components.meta} (77%) delete mode 100644 Assets/Scripts/LogicBlocks.meta diff --git a/Assets/Plugins/IngameDebugConsole/Android.meta b/Assets/Plugins/IngameDebugConsole/Android.meta deleted file mode 100644 index 12049b6..0000000 --- a/Assets/Plugins/IngameDebugConsole/Android.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 3d7d7a61a5341904eb3c65af025b1d86 -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Plugins/IngameDebugConsole/Prefabs.meta b/Assets/Plugins/IngameDebugConsole/Prefabs.meta deleted file mode 100644 index 6aa8bf2..0000000 --- a/Assets/Plugins/IngameDebugConsole/Prefabs.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 7dbc36665bc0d684db9a4447e27a7a4b -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Plugins/IngameDebugConsole/Scripts.meta b/Assets/Plugins/IngameDebugConsole/Scripts.meta deleted file mode 100644 index 72dcaac..0000000 --- a/Assets/Plugins/IngameDebugConsole/Scripts.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 860c08388401a6d4e858fe4910ea9337 -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Plugins/IngameDebugConsole/Sprites/Unused.meta b/Assets/Plugins/IngameDebugConsole/Sprites/Unused.meta deleted file mode 100644 index f3769b1..0000000 --- a/Assets/Plugins/IngameDebugConsole/Sprites/Unused.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: f6caae32d463529478f2186f47c2e3fe -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Models.meta b/Assets/Scripts/Components.meta similarity index 77% rename from Assets/Models.meta rename to Assets/Scripts/Components.meta index 5346456..5a95b09 100644 --- a/Assets/Models.meta +++ b/Assets/Scripts/Components.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 296bd90e667df1f4697823a0aa45acf0 +guid: 8d7394d70ec233849a60a26da5f23b75 folderAsset: yes DefaultImporter: externalObjects: {} diff --git a/Assets/Scripts/GameMode/ColorGameMode/RacetrackGameMode.cs b/Assets/Scripts/GameMode/ColorGameMode/RacetrackGameMode.cs index 8836473..274a16b 100644 --- a/Assets/Scripts/GameMode/ColorGameMode/RacetrackGameMode.cs +++ b/Assets/Scripts/GameMode/ColorGameMode/RacetrackGameMode.cs @@ -66,7 +66,7 @@ public class RacetrackGameMode : GameMode /// Character which moved /// Client of the character /// Block moved onto - 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); @@ -127,7 +127,7 @@ public class RacetrackGameMode : GameMode } - protected override void OnAllPlayersFinishedMove(PlayerData[] allPlayers) + protected override void OnAllPlayersMoved(PlayerData[] allPlayers) { foreach (PlayerData player in allPlayers) { @@ -207,7 +207,7 @@ public class RacetrackGameMode : GameMode RoundCount = 0; 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); } } } diff --git a/Assets/Scripts/GameMode/GameMode.cs b/Assets/Scripts/GameMode/GameMode.cs index 41d89e1..519a0e4 100644 --- a/Assets/Scripts/GameMode/GameMode.cs +++ b/Assets/Scripts/GameMode/GameMode.cs @@ -16,6 +16,8 @@ public abstract class GameMode : ScriptableObject private List RoundTimes; public Action GameStartEvent; + public Action InputStartEvent; + public Action RoundStartEvent; public Action AllPlayersMovedEvent; public Action RoundEndEvent; @@ -23,13 +25,39 @@ public abstract class GameMode : ScriptableObject private List gameTimes; - - protected abstract void OnGameStart(PlayerData[] allPlayers); + /// + /// Called once before any players have spawned + /// + protected virtual void OnPreGameStart() { } + + /// + /// Called once after players have spawned + /// + /// All players in game + protected virtual void OnGameStart(PlayerData[] allPlayers) { } + + /// + /// Called every time players are moved to the input scene + /// + /// All players in game + protected virtual void OnInputStart(PlayerData[] allPlayers) { } + + /// + /// Called for each player when we recieve their input + /// + /// Player who's input we recieved + protected virtual void OnRecievedPlayerInput(PlayerData player) { } + + /// + /// Called after we have recieved all player input + /// + /// All players in game + protected virtual void OnInputEnd(PlayerData[] allPlayers) { } /// /// Called at the beginning of a new Round /// - protected abstract void OnRoundStart(PlayerData[] allPlayers); + protected virtual void OnRoundStart(PlayerData[] allPlayers) { } /// /// Called for each player after they finished one move. Even if they didn't move! @@ -37,19 +65,34 @@ public abstract class GameMode : ScriptableObject /// Character which moved /// Client which controls character /// Block which the character finished on - protected abstract void OnPlayerMoved(Character character, ClientData client, Block currentBlock); + protected virtual void OnPlayerMoved(Character character, ClientData client, Block currentBlock) { } /// /// Called once after every player has finished one move /// /// List of all player Info - protected abstract void OnAllPlayersMoved(PlayerData[] allPlayers); + protected virtual void OnAllPlayersMoved(PlayerData[] allPlayers) { } - public abstract bool isGameOver(PlayerData[] allPlayers); + /// + /// Called after one round has finished + /// + /// all players in game + protected virtual void OnRoundEnd(PlayerData[] allPlayers) { } - protected abstract void OnRoundEnd(PlayerData[] allPlayers); + /// + /// Called once at end of game + /// + /// All players in game + protected virtual void OnGameOver(PlayerData[] allPlayers) { } - protected abstract void OnGameOver(PlayerData[] allPlayers); + /// + /// Called to check if victory condition has been met + /// + /// All players in game + /// returns true if victory condition has been met + public abstract bool isGameOver(PlayerData[] allPlayers); + + public float GetRoundTime() { @@ -59,29 +102,36 @@ public abstract class GameMode : ScriptableObject return retVal; } + #region Public functions public void PreGameStart() { - + OnPreGameStart(); } - public void GameStart(PlayerData[] allPlayers) { gameTimes = new List(RoundTimes); if (!String.IsNullOrEmpty(GameModeScene)) - UnityEngine.SceneManagement.SceneManager.LoadScene(GameModeScene,UnityEngine.SceneManagement.LoadSceneMode.Additive); + UnityEngine.SceneManagement.SceneManager.LoadScene(GameModeScene, UnityEngine.SceneManagement.LoadSceneMode.Additive); - for(int i = 0; i < allPlayers.Length; i++) + for (int i = 0; i < allPlayers.Length; i++) { allPlayers[i].client.SceneScore = 0; } OnGameStart(allPlayers); + GameStartEvent?.Invoke(); } public void InputStart(PlayerData[] AllPlayers) { + OnInputStart(AllPlayers); + InputStartEvent?.Invoke(); + } + public void RecievedPlayerInput(PlayerData player) + { + OnRecievedPlayerInput(player); } public void InputEnd(PlayerData[] AllPlayers) @@ -95,15 +145,15 @@ public abstract class GameMode : ScriptableObject RoundStartEvent?.Invoke(); } - public void AllPlayersMoved(PlayerData[] allPlayers) + public void PlayerMoved(PlayerData player) { - OnAllPlayersMoved(allPlayers); - AllPlayersMovedEvent?.Invoke(); + OnPlayerMoved(player.character, player.client, player.character.CurrentBlock); } - public void PlayerMoved(PlayerData player) + public void AllPlayersMoved(PlayerData[] allPlayers) { - OnPlayerMoved(player.character, player.client, player.character.CurrentBlock); + OnAllPlayersMoved(allPlayers); + AllPlayersMovedEvent?.Invoke(); } public void RoundEnd(PlayerData[] allPlayers) @@ -117,7 +167,9 @@ public abstract class GameMode : ScriptableObject OnGameOver(allPlayers); OnGameOverEvent?.Invoke(); } -} +} +#endregion Public Functions + /* --- Calls we need --- diff --git a/Assets/Scripts/GameMode/GameModeEvents.cs b/Assets/Scripts/GameMode/GameModeEvents.cs index 84fdf17..a9d9042 100644 --- a/Assets/Scripts/GameMode/GameModeEvents.cs +++ b/Assets/Scripts/GameMode/GameModeEvents.cs @@ -11,6 +11,7 @@ public class GameModeEvents : MonoBehaviour [Header("Events")] public UnityEvent OnGameStart = new UnityEvent(); + public UnityEvent OnInputStart = new UnityEvent(); public UnityEvent OnRoundStart = new UnityEvent(); public UnityEvent OnPlayersMoved = new UnityEvent(); public UnityEvent OnRoundEnd = new UnityEvent(); @@ -20,6 +21,7 @@ public class GameModeEvents : MonoBehaviour private void OnEnable() { Reference.Value.GameStartEvent += GameStart; + Reference.Value.InputStartEvent += (() => OnInputStart.Invoke()); Reference.Value.RoundStartEvent += RoundStart; Reference.Value.AllPlayersMovedEvent += PlayersMoved; Reference.Value.RoundEndEvent += RoundEnd; @@ -29,6 +31,7 @@ public class GameModeEvents : MonoBehaviour private void OnDisable() { Reference.Value.GameStartEvent -= GameStart; + Reference.Value.InputStartEvent -= (() => OnInputStart.Invoke()); Reference.Value.RoundStartEvent -= RoundStart; Reference.Value.AllPlayersMovedEvent -= PlayersMoved; Reference.Value.RoundEndEvent -= RoundEnd; diff --git a/Assets/Scripts/LogicBlocks.meta b/Assets/Scripts/LogicBlocks.meta deleted file mode 100644 index 98af9d1..0000000 --- a/Assets/Scripts/LogicBlocks.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 8936b441d7647f74884c94f97bfb8931 -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Managers/GameManager.cs b/Assets/Scripts/Managers/GameManager.cs index 424d3e9..0fd86d9 100644 --- a/Assets/Scripts/Managers/GameManager.cs +++ b/Assets/Scripts/Managers/GameManager.cs @@ -31,7 +31,6 @@ public class GameManager : MonoBehaviour #endregion Inspector Field - #region Private Variables private Dictionary playerData; #endregion Private Variables @@ -48,22 +47,12 @@ public class GameManager : MonoBehaviour private GameMode gameMode {get { return CurrentGameMode.Value; } } #endregion Read Only - public GameObject levelInfo; - public blockSpawn bspawn; - #region Unity Functions - public void Awake() - { - RegisterHandlers(); - SpawnCharacters(); - ClientList.ForEach(p => p.ChangeScene("ClientScene")); - } private void Start() { - StartCoroutine(displayforSeconds(levelInfo, 5.0f)); - gameMode.GameStart(playerDataAsArray.ToArray()); - StartRound(); + //Start Game + StartCoroutine(GameRoutine()); } private void Update() @@ -73,6 +62,12 @@ public class GameManager : MonoBehaviour server.ServerUpdate(); } + private void OnEnable() + { + //Let Server know we want to recieve some messages + RegisterHandlers(); + } + private void OnDisable() { //Let server know to not send messages this way @@ -80,6 +75,7 @@ public class GameManager : MonoBehaviour } #endregion Unity Functions + #region Class Functions private IEnumerator GameRoutine() { @@ -105,33 +101,6 @@ public class GameManager : MonoBehaviour gameMode.GameEnd(playerDataAsArray); } - - - - IEnumerator displayforSeconds(GameObject display, float time) - { - display.SetActive (true); - yield return new WaitForSeconds(time); - display.SetActive (false); - } - - private IEnumerator WaitForPlayerInput() - { - LogicProtocols.FloatMsg RoundTime = new LogicProtocols.FloatMsg(gameMode.GetRoundTime()); - 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() { //Tell the gamemode that we are starting a round @@ -147,7 +116,7 @@ public class GameManager : MonoBehaviour 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 @@ -156,7 +125,7 @@ public class GameManager : MonoBehaviour if (gameMode.isGameOver(playerDataAsArray)) break; } - + //Let GameMode know that Round is Over gameMode.RoundEnd(playerDataAsArray.ToArray()); @@ -166,7 +135,56 @@ public class GameManager : MonoBehaviour player.blockReader.Reset(); player.client.SendInventory(); } - + + } + + private IEnumerator WaitForPlayerInput() + { + //send round length to players + //#TODO make this only happen after first input + LogicProtocols.FloatMsg RoundTime = new LogicProtocols.FloatMsg(gameMode.GetRoundTime()); + playerDataAsArray.ForEach(p => p.client.conn.Send(LogicProtocols.SendRoundTime, RoundTime)); + + //Send players to input Scene + ClientList.ForEach(p => p.ChangeScene("ClientScene")); + + //Let gamemode know clients are input-ing + gameMode.InputStart(playerDataAsArray); + + //wait for all players to + yield return new WaitUntil(() => playerData.All(p => p.Value.recievedList)); + + //reset + playerDataAsArray.ForEach(p => p.recievedList = false); //reset all players list + + //Let gamemode know all inputs have been recieved + gameMode.InputEnd(playerDataAsArray); + } + + private IEnumerator MoveRoutine(PlayerData data) + { + bool blockFinished = false; + float waitTime; + + //Loop until the current block indicates or the reader indicates it has finished + while (!blockFinished && !data.blockReader.Finished) + { + + //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) + { + data.client.Inventory.Remove(data.blockReader.CurrentBlock); + data.blockReader.CurrentBlock.hasBeenRemoved = true; + } + + //Process the move + blockFinished = data.blockReader.Read(data.character, AnimationTime, out waitTime); + + //Wait for the animation to finish + yield return new WaitForSeconds(waitTime); + + } } private void SpawnCharacters() @@ -195,12 +213,12 @@ public class GameManager : MonoBehaviour Block startingBlock = SpawnBlocks[spawnIndex++]; newChar.Initialise(startingBlock, client.Inventory, client.characterAnimal); newChar.transform.forward = startingBlock.SpawnDirection.ToVector(); - playerData.Add(client.ID, new PlayerData(newChar,client)); + playerData.Add(client.ID, new PlayerData(newChar, client)); newChar.ClientLink = client; - client.playerCharacter = newChar; + client.playerCharacter = newChar; } - } - + } + #endregion Class Functions #region Networking Functions @@ -238,39 +256,9 @@ public class GameManager : MonoBehaviour //Update player Data with recieved list playerData[msg.conn.connectionId].blockReader.LogicChain = new List(logicMsg.elements); playerData[msg.conn.connectionId].recievedList = true; - - //if we have recieved all moves start round - if (playerData.All(p => p.Value.recievedList)) - DoRoundRoutine(); } - #endregion Networking Functions - - private IEnumerator MoveRoutine(PlayerData data) - { - bool blockFinished = false; - float waitTime; - - //Loop until the current block indicates or the reader indicates it has finished - while (!blockFinished && !data.blockReader.Finished) - { - - //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) - { - data.client.Inventory.Remove(data.blockReader.CurrentBlock); - data.blockReader.CurrentBlock.hasBeenRemoved = true; - } - - //Process the move - blockFinished = data.blockReader.Read(data.character, AnimationTime,out waitTime); - - //Wait for the animation to finish - yield return new WaitForSeconds(waitTime); - - } - } + #endregion Networking Functions } public class PlayerData diff --git a/Assets/Scripts/Managers/GameManagerRacetrack.cs b/Assets/Scripts/Managers/GameManagerRacetrack.cs index 82b3759..d380b63 100644 --- a/Assets/Scripts/Managers/GameManagerRacetrack.cs +++ b/Assets/Scripts/Managers/GameManagerRacetrack.cs @@ -7,7 +7,7 @@ using Networking.Server; using Networking; using UnityEngine.SceneManagement; - +/* public class GameManagerRacetrack : MonoBehaviour { #region Inspector Field @@ -186,4 +186,4 @@ public class GameManagerRacetrack : MonoBehaviour } data.waiting = true; } -} +}*/