2020-08-22 05:29:00 +00:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
2020-08-22 08:28:17 +00:00
|
|
|
|
using TMPro;
|
|
|
|
|
|
2020-08-22 05:29:00 +00:00
|
|
|
|
public class GameBoardInstance : MonoBehaviour {
|
|
|
|
|
|
|
|
|
|
public static GameBoardInstance instance { get; private set; }
|
|
|
|
|
|
|
|
|
|
public GameBoard player1, player2;
|
2020-08-22 23:43:30 +00:00
|
|
|
|
public bool readyActive;
|
2020-08-22 20:39:53 +00:00
|
|
|
|
public bool gameActive;
|
2020-08-22 23:43:30 +00:00
|
|
|
|
public bool gameSetActive;
|
2020-08-22 05:29:00 +00:00
|
|
|
|
|
2020-08-22 08:28:17 +00:00
|
|
|
|
[Header("Text")]
|
|
|
|
|
public TextMeshProUGUI headerTextMesh;
|
|
|
|
|
public string startKey;
|
|
|
|
|
public string gameSetKey;
|
|
|
|
|
|
|
|
|
|
[Header("Referneces")]
|
2020-08-22 05:29:00 +00:00
|
|
|
|
public GameObject tilePrefab;
|
|
|
|
|
public Sprite[] regular;
|
|
|
|
|
public Sprite[] lit;
|
|
|
|
|
public Sprite[] activators;
|
|
|
|
|
|
2020-08-22 07:22:16 +00:00
|
|
|
|
[Header("AI difficulty, 0(easy)-3(hardest)")]
|
|
|
|
|
public int AIDifficulty = 1;
|
|
|
|
|
|
2020-08-22 05:29:00 +00:00
|
|
|
|
private void Awake() {
|
|
|
|
|
instance = this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-22 08:28:17 +00:00
|
|
|
|
private void Update() {
|
2020-08-22 18:49:29 +00:00
|
|
|
|
if (GameTransition.Instance == null) return;
|
|
|
|
|
|
2020-08-22 08:28:17 +00:00
|
|
|
|
var state = GameTransition.Instance.state;
|
2020-08-22 20:39:53 +00:00
|
|
|
|
if (state == GameState.InGame && gameActive){
|
2020-08-22 08:28:17 +00:00
|
|
|
|
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();
|
|
|
|
|
|
2020-08-22 10:46:33 +00:00
|
|
|
|
if (PlayerProperties.playerResponse.GetLocal() == 0){
|
2020-08-22 10:42:25 +00:00
|
|
|
|
response = 0;
|
2020-08-22 08:28:17 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
response = PlayerProperties.playerResponse.GetLocal();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch(response){
|
2020-08-22 10:42:25 +00:00
|
|
|
|
// leave room
|
2020-08-22 08:28:17 +00:00
|
|
|
|
case 0:
|
2020-08-22 20:39:53 +00:00
|
|
|
|
ExitGame(true);
|
2020-08-22 08:28:17 +00:00
|
|
|
|
break;
|
2020-08-22 10:42:25 +00:00
|
|
|
|
// return to lobby or not enough players
|
|
|
|
|
case 1:
|
|
|
|
|
case -2:
|
|
|
|
|
GameTransition.Instance.state = GameState.Lobby;
|
|
|
|
|
player1.Clear();
|
|
|
|
|
player2.Clear();
|
|
|
|
|
break;
|
|
|
|
|
// rematch
|
|
|
|
|
case 2:
|
|
|
|
|
SetupGame();
|
|
|
|
|
break;
|
|
|
|
|
// disconnected
|
2020-08-22 08:28:17 +00:00
|
|
|
|
case -3:
|
|
|
|
|
GameTransition.Instance.state = GameState.Menu;
|
|
|
|
|
player1.Clear();
|
|
|
|
|
player2.Clear();
|
|
|
|
|
break;
|
|
|
|
|
default: break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-22 05:29:00 +00:00
|
|
|
|
public void SetupGame(){
|
2020-08-22 08:28:17 +00:00
|
|
|
|
System.Action callback;
|
|
|
|
|
|
|
|
|
|
player1.Clear();
|
|
|
|
|
player2.Clear();
|
2020-08-22 07:34:54 +00:00
|
|
|
|
|
2020-08-22 05:29:00 +00:00
|
|
|
|
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;
|
|
|
|
|
|
2020-08-22 08:28:17 +00:00
|
|
|
|
callback = StartMultiPlayer;
|
2020-08-22 05:29:00 +00:00
|
|
|
|
} else {
|
|
|
|
|
player1.authorityID = -1;
|
2020-08-22 07:22:16 +00:00
|
|
|
|
player2.authorityID = -1;
|
2020-08-22 05:29:00 +00:00
|
|
|
|
|
2020-08-22 08:28:17 +00:00
|
|
|
|
callback = StartSinglePlayer;
|
2020-08-22 07:22:16 +00:00
|
|
|
|
}
|
2020-08-22 05:29:00 +00:00
|
|
|
|
|
2020-08-22 23:43:30 +00:00
|
|
|
|
player1.Setup();
|
|
|
|
|
player2.Setup();
|
|
|
|
|
|
2020-08-22 05:29:00 +00:00
|
|
|
|
GameTransition.Instance.state = GameState.InGame;
|
2020-08-22 08:28:17 +00:00
|
|
|
|
StartCoroutine(StartGameTimer(3f, callback));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerator StartGameTimer(float timer, System.Action callback){
|
2020-08-22 23:43:30 +00:00
|
|
|
|
readyActive = true;
|
|
|
|
|
|
2020-08-22 08:28:17 +00:00
|
|
|
|
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();
|
2020-08-22 20:39:53 +00:00
|
|
|
|
gameActive = true;
|
2020-08-22 23:43:30 +00:00
|
|
|
|
readyActive = false;
|
2020-08-22 08:28:17 +00:00
|
|
|
|
|
|
|
|
|
yield return new WaitForSeconds(2f);
|
|
|
|
|
headerTextMesh.text = "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private IEnumerator EndGameTimer(float timer){
|
2020-08-22 20:39:53 +00:00
|
|
|
|
gameActive = false;
|
2020-08-22 23:43:30 +00:00
|
|
|
|
gameSetActive = true;
|
2020-08-22 08:28:17 +00:00
|
|
|
|
|
|
|
|
|
headerTextMesh.text = Localization.GetString(gameSetKey);
|
|
|
|
|
yield return new WaitForSeconds(timer);
|
|
|
|
|
headerTextMesh.text = "";
|
|
|
|
|
|
2020-08-22 23:43:30 +00:00
|
|
|
|
gameSetActive = false;
|
2020-08-22 08:28:17 +00:00
|
|
|
|
GameTransition.Instance.state = GameState.Continue;
|
|
|
|
|
Rematch.Instance.Setup();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void StartSinglePlayer(){
|
2020-08-22 23:43:30 +00:00
|
|
|
|
player1.StartGame();
|
|
|
|
|
player2.StartGame();
|
2020-08-22 08:28:17 +00:00
|
|
|
|
player2.StartAI(new[] { 0.5f, 0.2f, 0.1f, 0f }[AIDifficulty]);
|
2020-08-22 05:29:00 +00:00
|
|
|
|
}
|
2020-08-22 08:28:17 +00:00
|
|
|
|
|
|
|
|
|
private void StartMultiPlayer(){
|
2020-08-22 23:43:30 +00:00
|
|
|
|
player1.StartGame();
|
|
|
|
|
player2.StartGame();
|
2020-08-22 08:28:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-22 20:39:53 +00:00
|
|
|
|
public void ExitGame(bool force = false){
|
|
|
|
|
if (gameActive || force){
|
|
|
|
|
gameActive = false;
|
|
|
|
|
|
|
|
|
|
player1.Stop();
|
|
|
|
|
player2.Stop();
|
|
|
|
|
|
|
|
|
|
player1.Clear();
|
|
|
|
|
player2.Clear();
|
|
|
|
|
|
|
|
|
|
if (NetworkManager.inRoom){
|
|
|
|
|
NetworkManager.net.OpLeaveRoom();
|
|
|
|
|
GameTransition.Instance.state = GameState.Multiplayer;
|
|
|
|
|
} else {
|
|
|
|
|
GameTransition.Instance.state = GameState.Menu;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-22 05:29:00 +00:00
|
|
|
|
}
|