You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

183 lines
4.9 KiB

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Networking;
using Networking.Server;
using Networking;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
#region Inspector Field
[Header("Settings")]
[SerializeField]
private float AnimationTime;
[SerializeField]
private GameModeReference CurrentGameMode;
[Header("References")]
[SerializeField]
[Tooltip("Prefab of character for players to play")]
private Character characterPrefab;
[SerializeField]
private ServerObject server;
[SerializeField]
private ClientList ClientList;
#endregion Inspector Field
#region Private Variables
private Dictionary<int, PlayerData> playerData;
private GameMode gamemode;
#endregion Private Variables
#region Read Only
private IEnumerable<PlayerData> playerArray { get { return playerData.Values; } }
#endregion Read Only
public void Awake()
{
gamemode = CurrentGameMode.Value;
RegisterHandlers();
SpawnCharacters();
ClientList.ForEach(p => p.ChangeScene("ClientScene"));
}
private void Start()
{
gamemode.GameStart(playerArray.ToArray());
StartRound();
}
private void Update()
{
server.ServerUpdate();
}
private void RecieveLogicList(NetworkMessage msg)
{
LogicProtocols.LogicMsg logicMsg;
if (!msg.TryRead(out logicMsg))
return;
Debug.Log("Recieved function from " + msg.conn.connectionId);
playerData[msg.conn.connectionId].blockReader.LogicChain = new List<LogicBlock>(logicMsg.elements);
playerData[msg.conn.connectionId].recievedList = true;
if (playerData.All(p => p.Value.recievedList))
DoRoundRoutine();
}
private void DoRoundRoutine()
{
Debug.Log("Starting Round");
StartCoroutine(RoundRoutine());
}
private void StartRound()
{
gamemode.RoundStart(playerArray.ToArray());
}
private IEnumerator RoundRoutine()
{
playerArray.ForEach(p => p.recievedList = false);
while (playerArray.Any(p => !p.blockReader.Finished))
{
foreach (PlayerData player in playerArray)
{
if (!player.waiting)
{
if (player.blockReader.CurrentBlock != null && !player.blockReader.CurrentBlock.hasBeenRemoved)
{
Debug.Log("used Block: " + player.blockReader.CurrentBlock.name);
player.client.Inventory.Remove(player.blockReader.CurrentBlock);
player.blockReader.CurrentBlock.hasBeenRemoved = true;
}
player.waiting = player.blockReader.Read(player.character, AnimationTime);
}
}
yield return new WaitForSeconds(AnimationTime);
gamemode.FinishedMove(playerArray.ToArray());
if (playerArray.All(p => p.waiting))
{
playerArray.ForEach(p => p.waiting = false);
Debug.Log("Finished one move");
}
}
if (gamemode.isGameOver(playerArray.ToArray()))
{
Debug.Log("Game Over");
SceneManager.LoadScene("ScoreBoards");
}
gamemode.RoundEnd(playerArray.ToArray());
foreach (PlayerData player in playerArray)
{
player.blockReader.Reset();
player.waiting = false;
player.client.SendInventory();
player.client.ChangeScene("ClientScene");
}
Debug.Log("Finished Moving");
StartRound();
}
private void SpawnCharacters()
{
playerData = new Dictionary<int, PlayerData>();
Block[] SpawnBlocks = FindObjectsOfType<Block>().Where(p => p.isSpawnable).ToArray();
int spawnIndex = 0;
foreach (ClientData client in ClientList)
{
Character newChar = Instantiate(characterPrefab);
Block startingBlock = SpawnBlocks[(spawnIndex++ % ClientList.ConnectedClients.Count)];
newChar.Initialise(startingBlock, client.Inventory, client.characterAnimal);
newChar.transform.forward = startingBlock.SpawnDirection.ToVector();
playerData.Add(client.ID, new PlayerData(newChar,client));
}
}
private void RegisterHandlers()
{
server.server.RegisterHandler(LogicProtocols.SendLogicList, RecieveLogicList);
}
}
public class PlayerData
{
public Character character;
public BlockReader blockReader;
public ClientData client;
public bool recievedList;
public bool waiting;
public PlayerData(Character character, ClientData client)
{
this.character = character;
this.client = client;
blockReader = new BlockReader();
}
}