Files
soccar2d/Assets/Scripts/GameCanvas.cs
T

318 lines
11 KiB
C#

using System.Collections;
using Mirror;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Serialization;
using UnityEngine.UI;
using DG.Tweening;
public class GameCanvas : MonoBehaviour
{
public static GameCanvas instance;
const int OpponentWaitTimeoutSeconds = 15;
static readonly WaitForSeconds WaitOneSecond = new WaitForSeconds(1f);
int opponentWaitSecondsElapsed;
bool hasShownOpponentTimeout;
Coroutine opponentWaitRoutine;
void Awake()
{
instance = this;
ShowWaitingForOpponentPanel();
HideEmotesPanel();
}
void OnDestroy()
{
StopOpponentWaitRoutine();
}
public EventTrigger gameInputPanel;
public Image turnTimerSliderTransform;
public Color redColor = Color.red;
public Color blueColor = Color.blue;
public TMP_Text blueTurnTimer;
[FormerlySerializedAs("txtTurnTimer")] public TMP_Text redTurnTimer;
public TMP_Text redName,blueName;
public TMP_Text txtRcPrize;
public TMP_Text txtWhosTurn;
public Image blueTurnTimerRound;
[FormerlySerializedAs("turnTimerRound")] public Image redTurnTimerRound;
public GameObject waitingForOpponentPanel;
public GameObject timeoutForOpponentPanel;
public Button btnLeaveGame;
[Header("Emotes")]
public Button btnEmote;
public Transform emotesPanel;
public Transform textEmotesParent;
public Transform emojiEmotesParent;
public GameObject textEmotePrefab;
public Transform EmoteSpawnRed, EmoteSpawnBlue;
public Vector3 textEmoteMovement = new Vector3(0,100,0);
public Vector3 emojiEmoteMovement = new Vector3(0,100,0);
public float textEmoteDuration = 5f;
public float emojiEmoteDuration = 5f;
bool isShowingEmotes=> emotesPanel.gameObject.activeSelf;
public RectTransform kickWarning;
void Start(){
if (btnLeaveGame != null)
btnLeaveGame.onClick.AddListener(OnLeaveGamePressed);
if(btnEmote != null)
btnEmote.onClick.AddListener(OnEmotePressed);
if(textEmotesParent != null){
for(int i=0; i<textEmotesParent.childCount; i++){
string txtName = textEmotesParent.GetChild(i).GetComponentInChildren<TMP_Text>().text;
textEmotesParent.GetChild(i).GetComponent<Button>().onClick.AddListener(() => OnEmoteTextPressed(txtName));
}
}
if(emojiEmotesParent != null){
for(int i=0; i<emojiEmotesParent.childCount; i++){
int index = i;
emojiEmotesParent.GetChild(i).GetComponent<Button>().onClick.AddListener(() => OnEmojiPressed(index));
}
}
try{
redName.text = GameManager.RedPlayer.Name;
blueName.text = GameManager.BluePlayer.Name;
}catch{
Logger.Log("Failed to fetch red and blue names, isServer?");
}
if (txtRcPrize != null)
txtRcPrize.text = CoinHelper.FormatString(GameManager.MatchRcPrizeCoins) + " RC";
}
void OnEmoteTextPressed(string txtName){
Debug.Log("OnEmoteTextPressed: " + txtName);
HideEmotesPanel();
NetPlayer.localPlayer.SendTextEmote(txtName);
}
void OnEmojiPressed(int id){
Debug.Log("OnEmojiPressed: " + id);
HideEmotesPanel();
NetPlayer.localPlayer.SendEmojiEmote(id);
}
public void ShowTextEmote(string txtName, Team team){
Debug.Log("Showing text emote: " + txtName +" from " + team);
GameObject textEmote = Instantiate(textEmotePrefab, team == Team.Red ? EmoteSpawnRed : EmoteSpawnBlue);
Destroy(textEmote.GetComponent<Button>());
textEmote.transform.localPosition = Vector3.zero;
textEmote.GetComponentInChildren<TMP_Text>().text = txtName;
textEmote.transform.localScale = new Vector3(0, 0, 0);
StartCoroutine(TextEmoteRoutine(textEmote));
}
IEnumerator TextEmoteRoutine(GameObject textEmote){
textEmote.transform.DOScale(1, 0.5f).SetEase(Ease.OutBack);
textEmote.transform.DOMove(textEmote.transform.position + textEmoteMovement, textEmoteDuration).SetEase(Ease.OutBack);
yield return new WaitForSeconds(textEmoteDuration);
textEmote.transform.DOScale(0, 0.5f).SetEase(Ease.InBack);
yield return new WaitForSeconds(0.5f);
Destroy(textEmote);
}
public void ShowEmojiEmote(int id, Team team){
Debug.Log("Showing emoji emote: " + id);
GameObject emojiEmote = Instantiate(emojiEmotesParent.GetChild(id).gameObject, team == Team.Red ? EmoteSpawnRed : EmoteSpawnBlue);
Destroy(emojiEmote.GetComponent<Button>());
emojiEmote.transform.localPosition = Vector3.zero;
emojiEmote.transform.localScale = new Vector3(0, 0, 0);
StartCoroutine(EmojiEmoteRoutine(emojiEmote));
}
IEnumerator EmojiEmoteRoutine(GameObject emojiEmote){
emojiEmote.transform.DOScale(1, 0.5f).SetEase(Ease.OutBack);
emojiEmote.transform.DOMove(emojiEmote.transform.position + emojiEmoteMovement, emojiEmoteDuration).SetEase(Ease.OutBack);
yield return new WaitForSeconds(emojiEmoteDuration);
emojiEmote.transform.DOScale(0, 0.5f).SetEase(Ease.InBack);
yield return new WaitForSeconds(0.5f);
Destroy(emojiEmote);
}
void OnEmotePressed()
{
if(isShowingEmotes){
HideEmotesPanel();
}else{
ShowEmotesPanel();
}
}
public void ShowEmotesPanel(){
emotesPanel.gameObject.SetActive(true);
emotesPanel.localScale = new Vector3(0, 0, 0);
emotesPanel.DOScale(1, 0.2f).SetEase(Ease.OutBack);
}
public void HideEmotesPanel(){
emotesPanel.DOScale(0, 0.25f).SetEase(Ease.InBack).OnComplete(() => {
emotesPanel.gameObject.SetActive(false);
});
}
void OnLeaveGamePressed()
{
if (CupidLobby.instance != null)
CupidLobby.instance.Cancel();
LoginManager.ClearMatchYourTeam();
NetManager.StopNetworkingForSceneChange();
LevelLoadManager.LoadLevel("MainMenu");
}
public void ShowWaitingForOpponentPanel(){
waitingForOpponentPanel.SetActive(true);
ResetOpponentTimeoutState();
StopOpponentWaitRoutine();
opponentWaitRoutine = StartCoroutine(OpponentWaitingTimeoutRoutine());
}
public void HideWaitingForOpponentPanel(){
StopOpponentWaitRoutine();
waitingForOpponentPanel.SetActive(false);
timeoutForOpponentPanel.SetActive(false);
ResetOpponentTimeoutState();
}
void StopOpponentWaitRoutine()
{
if (opponentWaitRoutine != null)
{
StopCoroutine(opponentWaitRoutine);
opponentWaitRoutine = null;
}
}
void Update(){
UpdateTurnTimer();
}
Team prevTeam = Team.Red;
bool init=false;
void UpdateTurnTimer(){
if(NetPlayer.localPlayer == null){return;}
if(!init){
init=true;
SwitchTeams();
}
float turnTimerFillAmount = 1 - (GameManager.instance.turnTimerCounter / GameManager.instance.turnTimer);
if(prevTeam != GameManager.instance.SelectedTeam){
SwitchTeams();
prevTeam = GameManager.instance.SelectedTeam;
}
turnTimerSliderTransform.transform.localScale = new Vector3(turnTimerFillAmount, 1, 1);
string timerString = (GameManager.instance.turnTimer - GameManager.instance.turnTimerCounter).ToString("F0");
if (GameManager.instance.SelectedTeam == Team.Red)
{
if (redTurnTimer != null) redTurnTimer.text = ":"+timerString;
if (blueTurnTimer != null) blueTurnTimer.text = string.Empty;
}
else
{
if (blueTurnTimer != null) blueTurnTimer.text = ":"+timerString;
if (redTurnTimer != null) redTurnTimer.text = string.Empty;
}
if (GameManager.instance.SelectedTeam == Team.Red)
{
if (redTurnTimerRound != null) redTurnTimerRound.fillAmount = turnTimerFillAmount;
if (blueTurnTimerRound != null) blueTurnTimerRound.fillAmount = 0f;
}
else
{
if (blueTurnTimerRound != null) blueTurnTimerRound.fillAmount = turnTimerFillAmount;
if (redTurnTimerRound != null) redTurnTimerRound.fillAmount = 0f;
}
}
void SwitchTeams(){
bool isMyTurn = GameManager.instance.SelectedTeam == NetPlayer.localPlayer.myTeam;
Color newColor = GameManager.instance.SelectedTeam == Team.Red ? redColor : blueColor;
turnTimerSliderTransform.DOColor(newColor, 0.5f);
if (txtWhosTurn != null)
{
txtWhosTurn.DOFade(0, 0.25f).OnComplete(() => {
txtWhosTurn.text = isMyTurn ? "Your Turn" : "Opponent's Turn";
txtWhosTurn.DOFade(1, 0.25f);
});
}
}
IEnumerator OpponentWaitingTimeoutRoutine()
{
while (true)
{
yield return WaitOneSecond;
if (waitingForOpponentPanel == null || timeoutForOpponentPanel == null)
yield break;
if (!waitingForOpponentPanel.activeSelf)
{
timeoutForOpponentPanel.SetActive(false);
ResetOpponentTimeoutState();
yield break;
}
bool isSoloPlayer = FindObjectsByType<NetPlayer>(FindObjectsSortMode.None).Length <= 1;
if (!isSoloPlayer)
{
timeoutForOpponentPanel.SetActive(false);
ResetOpponentTimeoutState();
continue;
}
if (hasShownOpponentTimeout)
continue;
opponentWaitSecondsElapsed++;
if (opponentWaitSecondsElapsed >= OpponentWaitTimeoutSeconds)
{
timeoutForOpponentPanel.transform.localScale = new Vector3(1, 0, 1);
timeoutForOpponentPanel.transform.DOScaleY(1,0.1f).SetEase(Ease.InOutBounce);
timeoutForOpponentPanel.SetActive(true);
hasShownOpponentTimeout = true;
}
}
}
void ResetOpponentTimeoutState()
{
opponentWaitSecondsElapsed = 0;
hasShownOpponentTimeout = false;
timeoutForOpponentPanel.SetActive(false);
}
public void ShowKickWarning(){
kickWarning.transform.localScale = new Vector3(0, 0, 0);
kickWarning.transform.DOScale(1, 0.2f).SetEase(Ease.OutBack);
kickWarning.gameObject.SetActive(true);
}
public void HideKickWarning(){
kickWarning.transform.DOScale(0, 0.2f).SetEase(Ease.InBack).OnComplete(() => {
kickWarning.gameObject.SetActive(false);
});
}
}