UPF/Assets/Game/Scripts/Minigame/RankedSplash.cs
2022-12-27 23:00:53 +05:30

92 lines
3.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class RankedSplash : MonoBehaviour
{
public static RankedSplash instance;
public int playerCountRequired;
public TMP_Text statusTxt;
public GameObject controlsUI;
public float startTimer = 120;
void Start()
{
instance = this;
statusTxt.text = "Waiting for confirmation";
controlsUI.SetActive(false);
}
bool allConnected= false;
string rules = @"1.Last one standing or the first to collect 30 ores wins!
2.Stay in safe zone to survive
3.Safe-Zone gets shrinked in 5 mins
4.Have fun!";
void FixedUpdate()
{
SpaceshipController[] players = FindObjectsOfType<SpaceshipController>();
// if(AutoConnect.instance.isClient){return;}
if(gameObject.activeSelf){
if(startTimer > 0){
startTimer-= Time.deltaTime;
if(startTimer <= 45 && players.Length == 0){
MessageDialog.instance.ShowMessage("Error", "Connection to the server timed out");
LoadingScreen.instance.LoadLevel("GameScene");
startTimer = -100;
}
if(startTimer <= 45 && players.Length == 1){
Debug.Log("Timed out");
MinigameManager.instance.EnemyForfeit();
players[0].WonRanked();
startTimer = -100;
}
}else{
if(SceneData.localPlayer.GetComponent<SpaceshipController>().ready){
MinigameManager.instance.EnemyForfeit();
SceneData.localPlayer.GetComponent<SpaceshipController>().WonRanked();
}
gameObject.SetActive(false);
}
}
if(players.Length >= playerCountRequired){
if(players.Length >= playerCountRequired && players[0].ready && players[1].ready){
//Start the match!
gameObject.SetActive(false);
controlsUI.SetActive(true);
startTimer = -100;
}
}else if(players.Length == 0){
statusTxt.text = "Waiting for server\nTimeout in " + (startTimer).ToString("n0") + " seconds";
}
if(MinigameManager.instance ==null){return;}
if(!MinigameManager.instance.RankedGameStarted){
if(!allConnected && players.Length < playerCountRequired){
statusTxt.text = "Waiting for opponents to connect\nTimeout in " + (startTimer).ToString("n0") + " seconds";
}else{
if(!allConnected){
allConnected = true;
MessageDialog.instance.ShowQuestion("Confirm", $"Click yes to confirm you understand the rules below.\n{rules}", OnAgree, ()=>{}, onlyYes:true);
// OnAgree();
}
if(players.Length > 1 && players[0].ready && players[1].ready){
//Start the match!
gameObject.SetActive(false);
startTimer = -100;
}else{
statusTxt.text = "Waiting for players to get ready\nRemaining " + startTimer.ToString("n0") + " seconds";
}
}
}
}
void OnAgree(){
SceneData.localPlayer.GetComponent<SpaceshipController>().SetReady(true);
}
}