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 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() {
|
|
|
|
|
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();
|
|
|
|
|
|
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:
|
|
|
|
|
if (NetworkManager.inRoom){
|
|
|
|
|
NetworkManager.net.OpLeaveRoom();
|
|
|
|
|
GameTransition.Instance.state = GameState.Multiplayer;
|
|
|
|
|
} else {
|
|
|
|
|
GameTransition.Instance.state = GameState.Menu;
|
|
|
|
|
}
|
|
|
|
|
player1.Clear();
|
|
|
|
|
player2.Clear();
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
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){
|
|
|
|
|
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]);
|
2020-08-22 05:29:00 +00:00
|
|
|
|
}
|
2020-08-22 08:28:17 +00:00
|
|
|
|
|
|
|
|
|
private void StartMultiPlayer(){
|
|
|
|
|
player1.Setup();
|
|
|
|
|
player2.Setup();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-22 05:29:00 +00:00
|
|
|
|
}
|