|
@ -7,7 +7,6 @@ using Networking.Server; |
|
|
using Networking; |
|
|
using Networking; |
|
|
using UnityEngine.SceneManagement; |
|
|
using UnityEngine.SceneManagement; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class GameManager : MonoBehaviour |
|
|
public class GameManager : MonoBehaviour |
|
|
{ |
|
|
{ |
|
|
#region Inspector Field
|
|
|
#region Inspector Field
|
|
@ -17,7 +16,6 @@ public class GameManager : MonoBehaviour |
|
|
[SerializeField] |
|
|
[SerializeField] |
|
|
private GameModeReference CurrentGameMode; |
|
|
private GameModeReference CurrentGameMode; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Header("References")] |
|
|
[Header("References")] |
|
|
[SerializeField] |
|
|
[SerializeField] |
|
|
[Tooltip("Prefab of character for players to play")] |
|
|
[Tooltip("Prefab of character for players to play")] |
|
@ -30,7 +28,6 @@ public class GameManager : MonoBehaviour |
|
|
private ClientList ClientList; |
|
|
private ClientList ClientList; |
|
|
|
|
|
|
|
|
public blockSpawn spawnBlock; |
|
|
public blockSpawn spawnBlock; |
|
|
|
|
|
|
|
|
#endregion Inspector Field
|
|
|
#endregion Inspector Field
|
|
|
|
|
|
|
|
|
#region Private Variables
|
|
|
#region Private Variables
|
|
@ -51,19 +48,14 @@ public class GameManager : MonoBehaviour |
|
|
#endregion Read Only
|
|
|
#endregion Read Only
|
|
|
|
|
|
|
|
|
#region Unity Functions
|
|
|
#region Unity Functions
|
|
|
|
|
|
|
|
|
private void Start() |
|
|
private void Start() |
|
|
{ |
|
|
{ |
|
|
spawnBlock = gameObject.GetComponent<blockSpawn>(); |
|
|
spawnBlock = gameObject.GetComponent<blockSpawn>(); |
|
|
|
|
|
|
|
|
//Start Game
|
|
|
|
|
|
StartCoroutine(GameRoutine()); |
|
|
StartCoroutine(GameRoutine()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void Update() |
|
|
private void Update() |
|
|
{ |
|
|
{ |
|
|
//This is required so that the server can continue to recieve client messages
|
|
|
|
|
|
//(it is a unity thing)
|
|
|
|
|
|
server.ServerUpdate(); |
|
|
server.ServerUpdate(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -83,13 +75,9 @@ public class GameManager : MonoBehaviour |
|
|
#region Class Functions
|
|
|
#region Class Functions
|
|
|
private IEnumerator GameRoutine() |
|
|
private IEnumerator GameRoutine() |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
//Allows game mode to instantiate anything it might need;
|
|
|
//Allows game mode to instantiate anything it might need;
|
|
|
gameMode.PreGameStart(); |
|
|
gameMode.PreGameStart(); |
|
|
|
|
|
|
|
|
//gets list of items to check spawn locations
|
|
|
|
|
|
//spawnBlock.wakeup();
|
|
|
|
|
|
|
|
|
|
|
|
//Spawn Characters and tell let the GameMode do anything with the characters it might want
|
|
|
//Spawn Characters and tell let the GameMode do anything with the characters it might want
|
|
|
SpawnCharacters(); |
|
|
SpawnCharacters(); |
|
|
playerDataAsArray.ForEach(p => p.client.SendLives()); |
|
|
playerDataAsArray.ForEach(p => p.client.SendLives()); |
|
@ -101,34 +89,32 @@ public class GameManager : MonoBehaviour |
|
|
int playersalive_left = 0; |
|
|
int playersalive_left = 0; |
|
|
int spotinarray = 0; |
|
|
int spotinarray = 0; |
|
|
int players = 0; |
|
|
int players = 0; |
|
|
//check here if one player left
|
|
|
|
|
|
foreach(PlayerData data in playerDataAsArray) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach (PlayerData data in playerDataAsArray) |
|
|
{ |
|
|
{ |
|
|
players++; |
|
|
players++; |
|
|
if (data.character.lives > 0) |
|
|
|
|
|
|
|
|
if (data.client.Lives > 0) |
|
|
{ |
|
|
{ |
|
|
playersalive_left++; |
|
|
playersalive_left++; |
|
|
spotinarray = players; |
|
|
spotinarray = players; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
/*if(playersalive_left == 1) |
|
|
|
|
|
|
|
|
if(playersalive_left == 1) |
|
|
{ |
|
|
{ |
|
|
GlobalVariables.winneranimal = playerDataAsArray[spotinarray].client.characterAnimal; |
|
|
|
|
|
GlobalVariables.winnername = playerDataAsArray[spotinarray].client.Name; |
|
|
|
|
|
|
|
|
GlobalVariables.winneranimal = playerDataAsArray[spotinarray-1].client.characterAnimal; |
|
|
|
|
|
GlobalVariables.winnername = playerDataAsArray[spotinarray-1].client.Name; |
|
|
SceneManager.LoadScene("Winner"); |
|
|
SceneManager.LoadScene("Winner"); |
|
|
}*/ |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
//wait until we have recieved all player input
|
|
|
//wait until we have recieved all player input
|
|
|
yield return StartCoroutine(WaitForPlayerInput()); |
|
|
yield return StartCoroutine(WaitForPlayerInput()); |
|
|
|
|
|
|
|
|
//I hate having to do this
|
|
|
//I hate having to do this
|
|
|
EnvironmentBlocks = FindObjectsOfType<ActiveBlock>(); |
|
|
EnvironmentBlocks = FindObjectsOfType<ActiveBlock>(); |
|
|
////Debug.Log("Active blocks found: " + EnvironmentBlocks.Length);
|
|
|
|
|
|
|
|
|
|
|
|
//Routine for players movement
|
|
|
//Routine for players movement
|
|
|
yield return StartCoroutine(RoundRoutine()); //it's pretty long so it gets it's own coroutine;
|
|
|
yield return StartCoroutine(RoundRoutine()); //it's pretty long so it gets it's own coroutine;
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//Let the gamemode know that the game is over
|
|
|
//Let the gamemode know that the game is over
|
|
|
gameMode.GameEnd(playerDataAsArray); |
|
|
gameMode.GameEnd(playerDataAsArray); |
|
|
} |
|
|
} |
|
@ -144,12 +130,9 @@ public class GameManager : MonoBehaviour |
|
|
//Tell the gamemode that we are starting a round
|
|
|
//Tell the gamemode that we are starting a round
|
|
|
gameMode.RoundStart(playerDataAsArray); |
|
|
gameMode.RoundStart(playerDataAsArray); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Loop until all players have finished moving
|
|
|
//Loop until all players have finished moving
|
|
|
while (playerDataAsArray.Any(p => !p.blockReader.Finished)) |
|
|
while (playerDataAsArray.Any(p => !p.blockReader.Finished)) |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
//Loop through all players
|
|
|
//Loop through all players
|
|
|
foreach (PlayerData player in playerDataAsArray) |
|
|
foreach (PlayerData player in playerDataAsArray) |
|
|
{ |
|
|
{ |
|
@ -177,8 +160,6 @@ public class GameManager : MonoBehaviour |
|
|
gameMode.PlayerKilled(player); |
|
|
gameMode.PlayerKilled(player); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Let GameMode know that Round is Over
|
|
|
//Let GameMode know that Round is Over
|
|
|
yield return StartCoroutine(EnvironmentEnd()); |
|
|
yield return StartCoroutine(EnvironmentEnd()); |
|
|
gameMode.RoundEnd(playerDataAsArray.ToArray()); |
|
|
gameMode.RoundEnd(playerDataAsArray.ToArray()); |
|
@ -192,14 +173,12 @@ public class GameManager : MonoBehaviour |
|
|
{ |
|
|
{ |
|
|
if (player.client.Lives == 0) |
|
|
if (player.client.Lives == 0) |
|
|
{ |
|
|
{ |
|
|
//Debug.Log("Remove: " + player.client.characterAnimal);
|
|
|
|
|
|
removePlayer(player); |
|
|
removePlayer(player); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
//spawn collectible logic blocks
|
|
|
//spawn collectible logic blocks
|
|
|
spawnBlock.Spawn(); |
|
|
spawnBlock.Spawn(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Reset some player Data
|
|
|
//Reset some player Data
|
|
|
foreach (PlayerData player in playerDataAsArray) |
|
|
foreach (PlayerData player in playerDataAsArray) |
|
|
{ |
|
|
{ |
|
@ -211,9 +190,9 @@ public class GameManager : MonoBehaviour |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
playerData = playerData.OrderBy(unit => unit.Value.character.CurrentBlock.transform.position.x).ToDictionary(unit => unit.Key, unit => unit.Value); |
|
|
playerData = playerData.OrderBy(unit => unit.Value.character.CurrentBlock.transform.position.x).ToDictionary(unit => unit.Key, unit => unit.Value); |
|
|
Dictionary<int,PlayerData> filteredPlayers = playerData.Where(p => !p.Value.isDead).ToDictionary(unit => unit.Key, unit => unit.Value); |
|
|
|
|
|
|
|
|
//Dictionary<int,PlayerData> filteredPlayers = playerData.Where(p => !p.Value.isDead).ToDictionary(unit => unit.Key, unit => unit.Value);
|
|
|
int order = 1; |
|
|
int order = 1; |
|
|
foreach (PlayerData data in filteredPlayers.Values) |
|
|
|
|
|
|
|
|
foreach (PlayerData data in playerDataAsArray) |
|
|
{ |
|
|
{ |
|
|
data.character.runOrder = order; |
|
|
data.character.runOrder = order; |
|
|
order++; |
|
|
order++; |
|
@ -230,7 +209,6 @@ public class GameManager : MonoBehaviour |
|
|
{ |
|
|
{ |
|
|
if (player.client.Lives == 0) |
|
|
if (player.client.Lives == 0) |
|
|
{ |
|
|
{ |
|
|
//Debug.Log("Remove: " + player.client.characterAnimal);
|
|
|
|
|
|
removePlayer(player); |
|
|
removePlayer(player); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -277,13 +255,6 @@ public class GameManager : MonoBehaviour |
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
//Wait for the animation to finish
|
|
|
|
|
|
//yield return new WaitForSeconds(waitTime);
|
|
|
|
|
|
|
|
|
|
|
|
yield return StartCoroutine(data.blockReader.Read(data.character, AnimationTime)); |
|
|
yield return StartCoroutine(data.blockReader.Read(data.character, AnimationTime)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -292,24 +263,15 @@ public class GameManager : MonoBehaviour |
|
|
playerData = new Dictionary<int, PlayerData>(); |
|
|
playerData = new Dictionary<int, PlayerData>(); |
|
|
Block[] SpawnBlocks = FindObjectsOfType<Block>().Where(p => p.isSpawnable).ToArray(); |
|
|
Block[] SpawnBlocks = FindObjectsOfType<Block>().Where(p => p.isSpawnable).ToArray(); |
|
|
|
|
|
|
|
|
int blockIndex = 0; |
|
|
|
|
|
foreach (Block block in SpawnBlocks) |
|
|
|
|
|
{ |
|
|
|
|
|
////Debug.Log("Block #" + blockIndex++ + " (" + block.transform.position.x + ", " + block.transform.position.y + ", " + block.transform.position.z + ")");
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//int spawnIndex = 0;
|
|
|
//int spawnIndex = 0;
|
|
|
//If we have an odd number of players, then we start at spawn point 0 (in the middle)
|
|
|
//If we have an odd number of players, then we start at spawn point 0 (in the middle)
|
|
|
//If we have an even number, then we skip it
|
|
|
//If we have an even number, then we skip it
|
|
|
//int spawnIndex = ((ClientList.Count() + 1) % 2);
|
|
|
//int spawnIndex = ((ClientList.Count() + 1) % 2);
|
|
|
|
|
|
|
|
|
int spawnIndex = 0; |
|
|
int spawnIndex = 0; |
|
|
|
|
|
|
|
|
foreach (ClientData client in ClientList) |
|
|
foreach (ClientData client in ClientList) |
|
|
{ |
|
|
{ |
|
|
////Debug.Log("spawnIndex = " + spawnIndex);
|
|
|
|
|
|
Character newChar = Instantiate(characterPrefab); |
|
|
Character newChar = Instantiate(characterPrefab); |
|
|
//Block startingBlock = SpawnBlocks[(spawnIndex++ % ClientList.ConnectedClients.Count)];
|
|
|
|
|
|
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(); |
|
@ -329,7 +291,6 @@ public class GameManager : MonoBehaviour |
|
|
yield return new WaitUntil(() => InitiativeGroup.All(p => p.isFinished)); |
|
|
yield return new WaitUntil(() => InitiativeGroup.All(p => p.isFinished)); |
|
|
InitiativeGroup.ForEach(p => p.Reset()); |
|
|
InitiativeGroup.ForEach(p => p.Reset()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private IEnumerator EnvironmentEnd() |
|
|
private IEnumerator EnvironmentEnd() |
|
@ -351,7 +312,6 @@ public class GameManager : MonoBehaviour |
|
|
#endregion Class Functions
|
|
|
#endregion Class Functions
|
|
|
|
|
|
|
|
|
#region Networking Functions
|
|
|
#region Networking Functions
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
/// Registers functions which should deal with incoming messages from clients
|
|
|
/// Registers functions which should deal with incoming messages from clients
|
|
|
/// </summary>
|
|
|
/// </summary>
|
|
@ -374,20 +334,15 @@ public class GameManager : MonoBehaviour |
|
|
/// <param name="msg">messages passed by server</param>
|
|
|
/// <param name="msg">messages passed by server</param>
|
|
|
private void RecieveLogicList(NetworkMessage msg) |
|
|
private void RecieveLogicList(NetworkMessage msg) |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
//try to read base message as a logic message
|
|
|
//try to read base message as a logic message
|
|
|
LogicProtocols.LogicMsg logicMsg; |
|
|
LogicProtocols.LogicMsg logicMsg; |
|
|
if (!msg.TryRead(out logicMsg)) |
|
|
if (!msg.TryRead(out logicMsg)) |
|
|
return; |
|
|
return; |
|
|
|
|
|
|
|
|
////Debug that we have recieved it
|
|
|
|
|
|
////Debug.Log("Recieved function from " + ClientList[msg.conn.connectionId].Name);
|
|
|
|
|
|
|
|
|
|
|
|
//Update player Data with recieved list
|
|
|
//Update player Data with recieved list
|
|
|
playerData[msg.conn.Hash()].blockReader.LogicChain = new List<LogicBlock>(logicMsg.elements); |
|
|
playerData[msg.conn.Hash()].blockReader.LogicChain = new List<LogicBlock>(logicMsg.elements); |
|
|
playerData[msg.conn.Hash()].recievedList = true; |
|
|
playerData[msg.conn.Hash()].recievedList = true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
#endregion Networking Functions
|
|
|
#endregion Networking Functions
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|