using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using TMPro; public class GameBoardInstance : MonoBehaviour { public static GameBoardInstance instance { get; private set; } public GameBoard player1, player2; public bool readyActive; public bool gameActive; public bool gameSetActive; [Header("Text")] public TextMeshProUGUI headerTextMesh; public string startKey; public string gameSetKey; [Header("Referneces")] public GameObject tilePrefab; public Sprite[] regular; public Sprite[] lit; public Sprite[] activators; [Header("AI difficulty, 0(easy)-3(hardest)")] public int AIDifficulty = 1; private void Awake() { instance = this; } private void Update() { if (GameTransition.Instance == null) return; var state = GameTransition.Instance.state; if (state == GameState.InGame && gameActive){ var over = player1.delayState == GameBoard.DelayState.Loss || player2.delayState == GameBoard.DelayState.Loss; if (over){ player1.Stop(); player2.Stop(); if (wasInDemo) { ExitGame(true); } else { StartCoroutine(EndGameTimer(5f)); } } } else if (state == GameState.Continue){ var response = -1; if (NetworkManager.inRoom){ response = PlayerProperties.GetAllResponse(); if (PlayerProperties.playerResponse.GetLocal() == 0){ response = 0; } } else { response = PlayerProperties.playerResponse.GetLocal(); } switch(response){ // leave room case 0: ExitGame(true); break; // return to lobby or not enough players case 1: case -2: GameTransition.Instance.state = GameState.Lobby; ClearGame(); break; // rematch case 2: SetupGame(); break; // disconnected case -3: GameTransition.Instance.state = GameState.Menu; ClearGame(); break; default: break; } } } public void ResetGame(){ player1.ResetGame(); player2.ResetGame(); } public void ClearGame(){ player1.Clear(); player2.Clear(); } public void SetupGame(bool asDemo = false){ System.Action callback; ClearGame(); if (NetworkManager.inRoom){ var players = NetworkManager.net.CurrentRoom.Players.Values.OrderBy(p => p.ID); var p1 = players.ElementAt(0); var p2 = players.ElementAt(1); player1.authorityID = p1.ID; player2.authorityID = p2.ID; callback = StartMultiPlayer; } else { player1.authorityID = -1; player2.authorityID = -1; callback = StartSinglePlayer; if (asDemo) callback = StartDemo; // why isn't this legal??? //callback = asDemo ? StartDemo : StartSinglePlayer; } player1.Setup(); player2.Setup(); GameTransition.Instance.state = GameState.InGame; StartCoroutine(StartGameTimer(3f, callback)); } private IEnumerator StartGameTimer(float timer, System.Action callback){ readyActive = true; var t = Time.time; while(Time.time - t <= timer){ headerTextMesh.text = Mathf.CeilToInt(timer - (Time.time - t)).ToString(); yield return null; } headerTextMesh.text = Localization.GetString(startKey); PlayerProperties.CreatePlayerHashtable(); callback(); gameActive = true; readyActive = false; yield return new WaitForSeconds(2f); headerTextMesh.text = ""; } private IEnumerator EndGameTimer(float timer){ gameActive = false; gameSetActive = true; headerTextMesh.text = Localization.GetString(gameSetKey); yield return new WaitForSeconds(timer); headerTextMesh.text = ""; gameSetActive = false; GameTransition.Instance.state = GameState.Continue; Rematch.Instance.Setup(); } static bool wasInDemo = false; private void StartSinglePlayer(){ player1.StartGame(); player2.StartGame(); //player2.StartAI(new[] { 0.5f, 0.2f, 0.1f, 0f }[AIDifficulty]); player2.StartAI(new[] { 2f, 0.5f, 0.2f, 0f }[AIDifficulty]); wasInDemo = false; } [ContextMenu("Start Demo")] private void StartDemo() { player1.StartGame(); player2.StartGame(); player1.StartAI(Random.value * 0.4f); player2.StartAI(Random.value * 0.4f); wasInDemo = true; } private void StartMultiPlayer(){ player1.StartGame(); player2.StartGame(); wasInDemo = false; } public void ExitGame(bool force = false){ if (gameActive || force){ gameActive = false; player1.Stop(); player2.Stop(); ClearGame(); if (NetworkManager.inRoom){ NetworkManager.net.OpLeaveRoom(); GameTransition.Instance.state = GameState.Multiplayer; } else { GameTransition.Instance.state = GameState.Menu; } } } }