TouhouLS/Assets/RealCode/GameBoardInstance.cs

210 lines
4.8 KiB
C#
Raw Permalink Normal View History

2020-08-22 05:29:00 +00:00
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
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;
public bool readyActive;
2020-08-22 20:39:53 +00:00
public bool gameActive;
public bool gameSetActive;
2020-08-22 05:29:00 +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;
[Header("AI difficulty, 0(easy)-3(hardest)")]
public int AIDifficulty = 1;
2020-08-22 05:29:00 +00:00
private void Awake() {
instance = this;
}
private void Update() {
if (GameTransition.Instance == null) return;
var state = GameTransition.Instance.state;
2020-08-22 20:39:53 +00:00
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();
2020-08-22 10:46:33 +00:00
if (PlayerProperties.playerResponse.GetLocal() == 0){
response = 0;
}
} else {
response = PlayerProperties.playerResponse.GetLocal();
}
switch(response){
// leave room
case 0:
2020-08-22 20:39:53 +00:00
ExitGame(true);
break;
// return to lobby or not enough players
case 1:
case -2:
GameTransition.Instance.state = GameState.Lobby;
2020-08-23 00:31:57 +00:00
ClearGame();
break;
// rematch
case 2:
SetupGame();
break;
// disconnected
case -3:
GameTransition.Instance.state = GameState.Menu;
2020-08-23 00:31:57 +00:00
ClearGame();
break;
default: break;
}
}
}
2020-08-23 00:31:57 +00:00
public void ResetGame(){
player1.ResetGame();
player2.ResetGame();
}
2020-08-23 00:31:57 +00:00
public void ClearGame(){
player1.Clear();
player2.Clear();
2020-08-23 00:31:57 +00:00
}
public void SetupGame(bool asDemo = false){
2020-08-23 00:31:57 +00:00
System.Action callback;
ClearGame();
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;
callback = StartMultiPlayer;
2020-08-22 05:29:00 +00:00
} else {
player1.authorityID = -1;
player2.authorityID = -1;
2020-08-22 05:29:00 +00:00
callback = StartSinglePlayer;
if (asDemo) callback = StartDemo;
// why isn't this legal???
//callback = asDemo ? StartDemo : StartSinglePlayer;
}
2020-08-22 05:29:00 +00:00
player1.Setup();
player2.Setup();
2020-08-22 05:29:00 +00:00
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();
2020-08-22 20:39:53 +00:00
gameActive = true;
readyActive = false;
yield return new WaitForSeconds(2f);
headerTextMesh.text = "";
}
private IEnumerator EndGameTimer(float timer){
2020-08-22 20:39:53 +00:00
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;
}
2020-08-22 20:39:53 +00:00
public void ExitGame(bool force = false){
if (gameActive || force){
gameActive = false;
player1.Stop();
player2.Stop();
2020-08-23 00:31:57 +00:00
ClearGame();
2020-08-22 20:39:53 +00:00
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
}