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; [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() { var state = GameTransition.Instance.state; if (state == GameState.InGame && !endGameCoroutine){ var over = player1.delayState == GameBoard.DelayState.Loss || player2.delayState == GameBoard.DelayState.Loss; if (over){ player1.Stop(); player2.Stop(); StartCoroutine(EndGameTimer(5f)); } } else if (state == GameState.Continue){ var response = -1; if (NetworkManager.inRoom){ response = PlayerProperties.GetAllResponse(); if (PlayerProperties.playerResponse.GetLocal() == 2){ response = 2; } } else { response = PlayerProperties.playerResponse.GetLocal(); } switch(response){ case 0: SetupGame(); break; case 1: case -2: GameTransition.Instance.state = GameState.Lobby; player1.Clear(); player2.Clear(); break; case 2: if (NetworkManager.inRoom){ NetworkManager.net.OpLeaveRoom(); GameTransition.Instance.state = GameState.Multiplayer; } else { GameTransition.Instance.state = GameState.Menu; } player1.Clear(); player2.Clear(); break; case -3: GameTransition.Instance.state = GameState.Menu; player1.Clear(); player2.Clear(); break; default: break; } } } public void SetupGame(){ System.Action callback; player1.Clear(); player2.Clear(); 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; } GameTransition.Instance.state = GameState.InGame; StartCoroutine(StartGameTimer(3f, callback)); } private IEnumerator StartGameTimer(float timer, System.Action callback){ 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(); yield return new WaitForSeconds(2f); headerTextMesh.text = ""; } private bool endGameCoroutine; private IEnumerator EndGameTimer(float timer){ endGameCoroutine = true; headerTextMesh.text = Localization.GetString(gameSetKey); yield return new WaitForSeconds(timer); headerTextMesh.text = ""; GameTransition.Instance.state = GameState.Continue; Rematch.Instance.Setup(); endGameCoroutine = false; } private void StartSinglePlayer(){ player1.Setup(); player2.Setup(); player2.StartAI(new[] { 0.5f, 0.2f, 0.1f, 0f }[AIDifficulty]); } private void StartMultiPlayer(){ player1.Setup(); player2.Setup(); } }