Browse Source

Cleaned up GameMode and GameManager

Josh_Dev_branch
Joshua Reason 5 years ago
parent
commit
8f6a4c49ba
11 changed files with 145 additions and 142 deletions
  1. +0
    -8
      Assets/Plugins/IngameDebugConsole/Android.meta
  2. +0
    -8
      Assets/Plugins/IngameDebugConsole/Prefabs.meta
  3. +0
    -8
      Assets/Plugins/IngameDebugConsole/Scripts.meta
  4. +0
    -8
      Assets/Plugins/IngameDebugConsole/Sprites/Unused.meta
  5. +1
    -1
      Assets/Scripts/Components.meta
  6. +3
    -3
      Assets/Scripts/GameMode/ColorGameMode/RacetrackGameMode.cs
  7. +70
    -18
      Assets/Scripts/GameMode/GameMode.cs
  8. +3
    -0
      Assets/Scripts/GameMode/GameModeEvents.cs
  9. +0
    -8
      Assets/Scripts/LogicBlocks.meta
  10. +66
    -78
      Assets/Scripts/Managers/GameManager.cs
  11. +2
    -2
      Assets/Scripts/Managers/GameManagerRacetrack.cs

+ 0
- 8
Assets/Plugins/IngameDebugConsole/Android.meta View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 3d7d7a61a5341904eb3c65af025b1d86
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

+ 0
- 8
Assets/Plugins/IngameDebugConsole/Prefabs.meta View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 7dbc36665bc0d684db9a4447e27a7a4b
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

+ 0
- 8
Assets/Plugins/IngameDebugConsole/Scripts.meta View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 860c08388401a6d4e858fe4910ea9337
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

+ 0
- 8
Assets/Plugins/IngameDebugConsole/Sprites/Unused.meta View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: f6caae32d463529478f2186f47c2e3fe
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

Assets/Models.meta → Assets/Scripts/Components.meta View File

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 296bd90e667df1f4697823a0aa45acf0
guid: 8d7394d70ec233849a60a26da5f23b75
folderAsset: yes folderAsset: yes
DefaultImporter: DefaultImporter:
externalObjects: {} externalObjects: {}

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

@ -66,7 +66,7 @@ public class RacetrackGameMode : 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 RacetrackGameMode : 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 RacetrackGameMode : 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);
} }
} }
} }

+ 70
- 18
Assets/Scripts/GameMode/GameMode.cs View File

@ -16,6 +16,8 @@ public abstract class GameMode : ScriptableObject
private List<float> RoundTimes; private List<float> RoundTimes;
public Action GameStartEvent; public Action GameStartEvent;
public Action InputStartEvent;
public Action RoundStartEvent; public Action RoundStartEvent;
public Action AllPlayersMovedEvent; public Action AllPlayersMovedEvent;
public Action RoundEndEvent; public Action RoundEndEvent;
@ -23,13 +25,39 @@ public abstract class GameMode : ScriptableObject
private List<float> gameTimes; private List<float> gameTimes;
protected abstract void OnGameStart(PlayerData[] allPlayers);
/// <summary>
/// Called once before any players have spawned
/// </summary>
protected virtual void OnPreGameStart() { }
/// <summary>
/// Called once after players have spawned
/// </summary>
/// <param name="allPlayers">All players in game</param>
protected virtual void OnGameStart(PlayerData[] allPlayers) { }
/// <summary>
/// Called every time players are moved to the input scene
/// </summary>
/// <param name="allPlayers">All players in game</param>
protected virtual void OnInputStart(PlayerData[] allPlayers) { }
/// <summary>
/// Called for each player when we recieve their input
/// </summary>
/// <param name="player">Player who's input we recieved</param>
protected virtual void OnRecievedPlayerInput(PlayerData player) { }
/// <summary>
/// Called after we have recieved all player input
/// </summary>
/// <param name="allPlayers">All players in game</param>
protected virtual void OnInputEnd(PlayerData[] allPlayers) { }
/// <summary> /// <summary>
/// Called at the beginning of a new Round /// Called at the beginning of a new Round
/// </summary> /// </summary>
protected abstract void OnRoundStart(PlayerData[] allPlayers);
protected virtual void OnRoundStart(PlayerData[] allPlayers) { }
/// <summary> /// <summary>
/// Called for each player after they finished one move. Even if they didn't move! /// Called for each player after they finished one move. Even if they didn't move!
@ -37,19 +65,34 @@ 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 OnPlayerMoved(Character character, ClientData client, Block currentBlock);
protected virtual 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 OnAllPlayersMoved(PlayerData[] allPlayers);
protected virtual void OnAllPlayersMoved(PlayerData[] allPlayers) { }
public abstract bool isGameOver(PlayerData[] allPlayers);
/// <summary>
/// Called after one round has finished
/// </summary>
/// <param name="allPlayers">all players in game</param>
protected virtual void OnRoundEnd(PlayerData[] allPlayers) { }
protected abstract void OnRoundEnd(PlayerData[] allPlayers);
/// <summary>
/// Called once at end of game
/// </summary>
/// <param name="allPlayers">All players in game</param>
protected virtual void OnGameOver(PlayerData[] allPlayers) { }
protected abstract void OnGameOver(PlayerData[] allPlayers);
/// <summary>
/// Called to check if victory condition has been met
/// </summary>
/// <param name="allPlayers">All players in game</param>
/// <returns>returns true if victory condition has been met</returns>
public abstract bool isGameOver(PlayerData[] allPlayers);
public float GetRoundTime() public float GetRoundTime()
{ {
@ -59,29 +102,36 @@ public abstract class GameMode : ScriptableObject
return retVal; return retVal;
} }
#region Public functions
public void PreGameStart() public void PreGameStart()
{ {
OnPreGameStart();
} }
public void GameStart(PlayerData[] allPlayers) public void GameStart(PlayerData[] allPlayers)
{ {
gameTimes = new List<float>(RoundTimes); gameTimes = new List<float>(RoundTimes);
if (!String.IsNullOrEmpty(GameModeScene)) 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; allPlayers[i].client.SceneScore = 0;
} }
OnGameStart(allPlayers); OnGameStart(allPlayers);
GameStartEvent?.Invoke();
} }
public void InputStart(PlayerData[] AllPlayers) public void InputStart(PlayerData[] AllPlayers)
{ {
OnInputStart(AllPlayers);
InputStartEvent?.Invoke();
}
public void RecievedPlayerInput(PlayerData player)
{
OnRecievedPlayerInput(player);
} }
public void InputEnd(PlayerData[] AllPlayers) public void InputEnd(PlayerData[] AllPlayers)
@ -95,15 +145,15 @@ public abstract class GameMode : ScriptableObject
RoundStartEvent?.Invoke(); 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) public void RoundEnd(PlayerData[] allPlayers)
@ -117,7 +167,9 @@ public abstract class GameMode : ScriptableObject
OnGameOver(allPlayers); OnGameOver(allPlayers);
OnGameOverEvent?.Invoke(); OnGameOverEvent?.Invoke();
} }
}
}
#endregion Public Functions
/* --- Calls we need --- /* --- Calls we need ---

+ 3
- 0
Assets/Scripts/GameMode/GameModeEvents.cs View File

@ -11,6 +11,7 @@ public class GameModeEvents : MonoBehaviour
[Header("Events")] [Header("Events")]
public UnityEvent OnGameStart = new UnityEvent(); public UnityEvent OnGameStart = new UnityEvent();
public UnityEvent OnInputStart = new UnityEvent();
public UnityEvent OnRoundStart = new UnityEvent(); public UnityEvent OnRoundStart = new UnityEvent();
public UnityEvent OnPlayersMoved = new UnityEvent(); public UnityEvent OnPlayersMoved = new UnityEvent();
public UnityEvent OnRoundEnd = new UnityEvent(); public UnityEvent OnRoundEnd = new UnityEvent();
@ -20,6 +21,7 @@ public class GameModeEvents : MonoBehaviour
private void OnEnable() private void OnEnable()
{ {
Reference.Value.GameStartEvent += GameStart; Reference.Value.GameStartEvent += GameStart;
Reference.Value.InputStartEvent += (() => OnInputStart.Invoke());
Reference.Value.RoundStartEvent += RoundStart; Reference.Value.RoundStartEvent += RoundStart;
Reference.Value.AllPlayersMovedEvent += PlayersMoved; Reference.Value.AllPlayersMovedEvent += PlayersMoved;
Reference.Value.RoundEndEvent += RoundEnd; Reference.Value.RoundEndEvent += RoundEnd;
@ -29,6 +31,7 @@ public class GameModeEvents : MonoBehaviour
private void OnDisable() private void OnDisable()
{ {
Reference.Value.GameStartEvent -= GameStart; Reference.Value.GameStartEvent -= GameStart;
Reference.Value.InputStartEvent -= (() => OnInputStart.Invoke());
Reference.Value.RoundStartEvent -= RoundStart; Reference.Value.RoundStartEvent -= RoundStart;
Reference.Value.AllPlayersMovedEvent -= PlayersMoved; Reference.Value.AllPlayersMovedEvent -= PlayersMoved;
Reference.Value.RoundEndEvent -= RoundEnd; Reference.Value.RoundEndEvent -= RoundEnd;

+ 0
- 8
Assets/Scripts/LogicBlocks.meta View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 8936b441d7647f74884c94f97bfb8931
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

+ 66
- 78
Assets/Scripts/Managers/GameManager.cs View File

@ -31,7 +31,6 @@ public class GameManager : MonoBehaviour
#endregion Inspector Field #endregion Inspector Field
#region Private Variables #region Private Variables
private Dictionary<int, PlayerData> playerData; private Dictionary<int, PlayerData> playerData;
#endregion Private Variables #endregion Private Variables
@ -48,22 +47,12 @@ public class GameManager : MonoBehaviour
private GameMode gameMode {get { return CurrentGameMode.Value; } } private GameMode gameMode {get { return CurrentGameMode.Value; } }
#endregion Read Only #endregion Read Only
public GameObject levelInfo;
public blockSpawn bspawn;
#region Unity Functions #region Unity Functions
public void Awake()
{
RegisterHandlers();
SpawnCharacters();
ClientList.ForEach(p => p.ChangeScene("ClientScene"));
}
private void Start() private void Start()
{ {
StartCoroutine(displayforSeconds(levelInfo, 5.0f));
gameMode.GameStart(playerDataAsArray.ToArray());
StartRound();
//Start Game
StartCoroutine(GameRoutine());
} }
private void Update() private void Update()
@ -73,6 +62,12 @@ public class GameManager : MonoBehaviour
server.ServerUpdate(); server.ServerUpdate();
} }
private void OnEnable()
{
//Let Server know we want to recieve some messages
RegisterHandlers();
}
private void OnDisable() private void OnDisable()
{ {
//Let server know to not send messages this way //Let server know to not send messages this way
@ -80,6 +75,7 @@ public class GameManager : MonoBehaviour
} }
#endregion Unity Functions #endregion Unity Functions
#region Class Functions
private IEnumerator GameRoutine() private IEnumerator GameRoutine()
{ {
@ -105,33 +101,6 @@ public class GameManager : MonoBehaviour
gameMode.GameEnd(playerDataAsArray); 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() private IEnumerator RoundRoutine()
{ {
//Tell the gamemode that we are starting a round //Tell the gamemode that we are starting a round
@ -147,7 +116,7 @@ public class GameManager : MonoBehaviour
yield return StartCoroutine(MoveRoutine(player));//Move Player yield return StartCoroutine(MoveRoutine(player));//Move Player
gameMode.PlayerMoved(player);//LetGameModeKnow gameMode.PlayerMoved(player);//LetGameModeKnow
} }
//Let Gamemode know all players have moved //Let Gamemode know all players have moved
gameMode.AllPlayersMoved(playerDataAsArray.ToArray()); gameMode.AllPlayersMoved(playerDataAsArray.ToArray());
playerDataAsArray.ForEach(p => p.client.SendScore()); //Update the players score playerDataAsArray.ForEach(p => p.client.SendScore()); //Update the players score
@ -156,7 +125,7 @@ public class GameManager : MonoBehaviour
if (gameMode.isGameOver(playerDataAsArray)) if (gameMode.isGameOver(playerDataAsArray))
break; break;
} }
//Let GameMode know that Round is Over //Let GameMode know that Round is Over
gameMode.RoundEnd(playerDataAsArray.ToArray()); gameMode.RoundEnd(playerDataAsArray.ToArray());
@ -166,7 +135,56 @@ public class GameManager : MonoBehaviour
player.blockReader.Reset(); player.blockReader.Reset();
player.client.SendInventory(); 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() private void SpawnCharacters()
@ -195,12 +213,12 @@ public class GameManager : MonoBehaviour
Block startingBlock = SpawnBlocks[spawnIndex++]; Block startingBlock = SpawnBlocks[spawnIndex++];
newChar.Initialise(startingBlock, client.Inventory, client.characterAnimal); newChar.Initialise(startingBlock, client.Inventory, client.characterAnimal);
newChar.transform.forward = startingBlock.SpawnDirection.ToVector(); 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; newChar.ClientLink = client;
client.playerCharacter = newChar;
client.playerCharacter = newChar;
} }
}
}
#endregion Class Functions
#region Networking Functions #region Networking Functions
@ -238,39 +256,9 @@ public class GameManager : MonoBehaviour
//Update player Data with recieved list //Update player Data with recieved list
playerData[msg.conn.connectionId].blockReader.LogicChain = new List<LogicBlock>(logicMsg.elements); playerData[msg.conn.connectionId].blockReader.LogicChain = new List<LogicBlock>(logicMsg.elements);
playerData[msg.conn.connectionId].recievedList = true; 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 public class PlayerData

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

@ -7,7 +7,7 @@ using Networking.Server;
using Networking; using Networking;
using UnityEngine.SceneManagement; using UnityEngine.SceneManagement;
/*
public class GameManagerRacetrack : MonoBehaviour public class GameManagerRacetrack : MonoBehaviour
{ {
#region Inspector Field #region Inspector Field
@ -186,4 +186,4 @@ public class GameManagerRacetrack : MonoBehaviour
} }
data.waiting = true; data.waiting = true;
} }
}
}*/

Loading…
Cancel
Save