Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using System;
- using System.Collections;
- public class BattleController : MonoBehaviour
- {
- private BattleTable bTable;
- private BattleAnimations bAnimations;
- public Animator TryToReconnect;
- public Animator CanvasLost;
- //Variaveis de Escolha do player
- /// <summary> Variaveis que compoem os "gatilhos" do loop de batalha </summary>
- /// controlam o fluxo de ações
- ///
- /// chooseCard = (True, pode escolher carta; false, ja escolheu)
- /// nextAction = inteiro que contem o tipo de ação escolhida pelo jogador (1 = ataque, 2 = defesa, 3 = trocar)
- private bool chooseCard;
- private int nextAction;
- //Variáveis que mantém dados para caso haja necessidade reenvio e variáves com dados extras
- //que servem para sincronizar o jogo caso haja perda de dados.
- private int holdAction;
- private int endTurn;
- private int Turn;
- private string typeData;
- //...//
- //Variaveis do oponente (Multiplayer)
- /// <summary> Gatilhos do oponente </summary>
- /// assim como os gatilhos do player, são as indicações das ações do oponente
- /// enemyChooseCard = (True, pode escolher carta; false, ja escolheu)
- /// enemyAction = inteiro que contem o tipo de ação escolhida pelo jogador (1 = ataque, 2 = defesa, 3 = trocar) (multiplayer)
- private int enemyAction;
- private bool enemyChooseCard;
- private bool dataReceived;
- private bool enemyHasCard;
- private bool hasDefended;
- public Button DefenseBTN;
- public GameObject NeedInfoRefresh;
- public Text vTime;
- private float actionTime;
- private float waitTime;
- private float disconnectTime;
- //Variaveis dos jogadores em batalha
- private bool playerCardIsAlive;
- private bool enemyCardIsAlive;
- //Variaveis de controle de batalha
- private bool canChooseCard;
- private bool canChooseAction;
- private bool repeatBattle;
- //Variaveis de DEBUG;
- public Text tex_ConcentraçãoCard;
- public Text tex_PoderCard;
- void Awake()
- {
- bTable = GameObject.FindWithTag("GameController").GetComponent<BattleTable>();
- bAnimations = GameObject.FindWithTag("GameController").GetComponent<BattleAnimations>();
- TryToReconnect = GameObject.Find("Aviso").GetComponent<Animator>();
- TryToReconnect.enabled = false;
- CanvasLost = GameObject.Find("LostCanvas").GetComponent<Animator>();
- CanvasLost.enabled = false;
- }
- void Start()
- {
- endTurn = 0;
- Turn = 0;
- NeedInfoRefresh.SetActive(false);
- }
- void Update()
- {
- if (bTable.GetPlayerCard() != null)
- {
- tex_ConcentraçãoCard.text = "Con: " + bTable.GetPlayerCard().GetCon();
- tex_PoderCard.text = "Pod: " + bTable.GetPlayerCard().GetPod();
- }
- if (hasDefended)
- {
- DefenseBTN.interactable = false;
- }else
- {
- DefenseBTN.interactable = true;
- }
- }
- public void BattleStart()
- {
- nextAction = 0;
- enemyAction = 0;
- MultiplayerController.Instance.SendMyUpdateEndTurn(1);
- Turn += 1;
- print("Mandei final de turno");
- StartCoroutine(ChooseAction());
- }
- public void enemyActionData(int action)
- {
- enemyAction = action;
- }
- void EnemyAnimationsLink()
- {
- if (enemyAction == 1)
- {
- bAnimations.EnemyAttacking();
- }
- else if (enemyAction == 2)
- {
- bAnimations.EnemyDefends();
- }
- else if (enemyAction == 3)
- {
- bAnimations.EnemyWantsToSwitch();
- }
- }
- void ResetAnimations()
- {
- bAnimations.PlayerIsNotDefending();
- }
- void CallButtons()
- {
- bAnimations.CallActionButtons();
- }
- public void IsTurnFinished(int _endTurn)
- {
- endTurn = _endTurn;
- print("Recebi final de turno: " + endTurn);
- }
- IEnumerator ChooseAction()
- {
- actionTime = bTable.GetGameTime();
- typeData = "X";
- waitTime = 25;
- disconnectTime = 85;
- //3. Espera a escolha de ação
- ///Ativa os botões de ação
- print("Escolha sua ação");
- MultiplayerController.Instance.SendMyUpdateEndTurn(1);
- canChooseAction = true;
- while (canChooseAction)
- {
- //Os botões de ação só aparecem se o turno estiver em sincronia.
- if (endTurn == 1)
- {
- CallButtons();
- }
- //Espera 10 segundos para que o "endTurn" se torne verdadeiro. Se não se tornar em 10 segundos.
- //Abre um botão para remandar as informações da carta, que provavelmente ocasionaram o delay.
- if (waitTime > 0)
- {
- waitTime -= Time.deltaTime;
- }
- else if (waitTime <= 0 && endTurn == 0)
- {
- TryToReconnect.enabled = true;
- TryToReconnect.Play("TryToReconnect");
- print("Ativou refresh");
- SendMyCardAgain("C");
- }
- if(disconnectTime > 0)
- {
- disconnectTime -= Time.deltaTime;
- vTime.text = "" + (int)disconnectTime;
- }
- else if (disconnectTime <= 0)
- {
- TryToReconnect.enabled = true;
- TryToReconnect.Play("TryToReconnect_back");
- DisconnectTimeOut();
- }
- //O actionTime só passa a funcionar se o turno estiver sincronizado.
- if (actionTime > 0 && endTurn == 1)
- {
- actionTime -= Time.deltaTime;
- GameObject.Find("txt_GameTime").GetComponent<Text>().text = "" + (int)actionTime;
- }
- else if(actionTime <= 0)
- {
- GameObject.Find("txt_GameTime").GetComponent<Text>().text = "" + 0;
- ChooseAction(1);
- }
- yield return null;
- }
- TryToReconnect.enabled = true;
- TryToReconnect.Play("TryToReconnect_back");
- print("Player Escolheu Ação!");
- PlayerAnimationsLink();
- bAnimations.RetractActionButtons();
- StartCoroutine(PlayerTimeAnimation());
- }
- public void DisconnectTimeOut()
- {
- MultiplayerController.Instance.LeaveRoom();
- CanvasLost.enabled = true;
- CanvasLost.Play("LostConnection");
- //GameObject LostConnection = GameObject.FindWithTag("LostCanvas");
- //LostConnection.GetComponent<LostConnection>().LostConnPopUp();
- }
- IEnumerator PlayerTimeAnimation()
- {
- yield return new WaitForSeconds(1f);
- StartCoroutine(EnemyChooseAction());
- //MultiplayerController.Instance.SendMyUpdateTypeAction(holdAction);
- }
- /*public void StopCount()
- {
- actionTime = 35;
- }*/
- IEnumerator EnemyChooseAction()
- {
- //4. Espera de escolha de ação do inimigo
- //Implementar função de recebimento de ação
- endTurn = 0;
- waitTime = 25;
- disconnectTime = 85;
- MultiplayerController.Instance.SendMyUpdateEndTurn(1);
- //MultiplayerController.Instance.SendMyUpdateTypeAction(holdAction);
- print("Espere o inimigo escolher a ação");
- while (enemyAction == 0)
- {
- //Espera 25 segundos (20 segundos do tempo de escolha de ação + 5 segundos extras)
- //Caso em 25 segundos o jogo não consiga prosseguir pra receber a ação do adversário.
- //Ele abre um botão de refresh para reenviar as informações de ação.
- if(waitTime > 0)
- {
- waitTime -= Time.deltaTime;
- }
- else if(waitTime <= 0)
- {
- TryToReconnect.enabled = true;
- TryToReconnect.Play("TryToReconnect");
- print("Chamou refresh _ inimigo");
- SendMyCardAgain("A");
- }
- if (disconnectTime > 0)
- {
- disconnectTime -= Time.deltaTime;
- vTime.text = "" + (int)disconnectTime;
- }
- else if (disconnectTime <= 0)
- {
- TryToReconnect.enabled = true;
- TryToReconnect.Play("TryToReconnect_back");
- DisconnectTimeOut();
- }
- yield return null;
- }
- TryToReconnect.enabled = true;
- TryToReconnect.Play("TryToReconnect_back");
- EnemyAnimationsLink();
- print("Ele escolheu!");
- StartCoroutine(EnemyTimeAnimation());
- }
- IEnumerator EnemyTimeAnimation()
- {
- yield return new WaitForSeconds(3.5f);
- StartCoroutine(BattleLoop());
- }
- IEnumerator BattleLoop()
- {
- //Receber os valores da cartas do inimigo
- //Criar uma função que crie um novo objeto, com os valores da carta do inimigo
- //Utilizar o objeto enemy action e enemy card nesta proxima função
- dataReceived = true;
- while (!dataReceived)
- {
- yield return null;
- }
- BattleAction(nextAction, enemyAction);
- }
- //realiza as ações de batalha
- void BattleAction(int Action1, int Action2)
- {
- if (Action1 == 1 && Action2 == 1)
- {
- print("Ambas atacaram");
- bAnimations.PlayerWasAttacked();
- bAnimations.EnemyWasAttacked();
- bTable.GetPlayerCard().ReceiveDamage(bTable.GetEnemyCard().GetPod());
- bTable.GetEnemyCard().ReceiveDamage(bTable.GetPlayerCard().GetPod());
- StartCoroutine(WaitForBattleHappens());
- }
- else if (Action1 == 2 && Action2 == 1)
- {
- print("Player defendeu, Inimigo Atacou");
- StartCoroutine(PlayerAnimationDefense());
- bAnimations.EnemyWasAttacked();
- bAnimations.PlayerWasAttackedWhileDefending();
- bTable.GetPlayerCard().ReceiveDamage(bTable.GetEnemyCard().GetPod() / 2);
- bTable.GetEnemyCard().ReceiveDamage(bTable.GetEnemyCard().GetPod() / 2);
- StartCoroutine(WaitForBattleHappens());
- }
- else if (Action1 == 1 && Action2 == 2)
- {
- print("Player atacou, Inimigo defendeu");
- StartCoroutine(EnemyAnimationDefense());
- bAnimations.PlayerWasAttacked();
- bAnimations.EnemyWasAttackedWhileDefending();
- bTable.GetPlayerCard().ReceiveDamage(bTable.GetPlayerCard().GetPod() / 2);
- bTable.GetEnemyCard().ReceiveDamage(bTable.GetPlayerCard().GetPod() / 2);
- StartCoroutine(WaitForBattleHappens());
- }
- else if (Action1 == 3 && Action2 == 1)
- {
- print("Player quer trocar a carta, Inimigo atacou");
- bAnimations.PlayerWasAttackedWithoutCard();
- bTable.GetPlayerCard().ReceiveDamage(bTable.GetEnemyCard().GetPod());
- //Checar se a carta contem concentração e voltar para escolha de carta.
- Exchange(1);
- } else if (Action1 == 3 && Action2 == 2)
- {
- StartCoroutine(EnemyAnimationDefense());
- print("Player quer trocar a carta, Inimigo defendeu");
- //voltar para escolha de carta;
- Exchange(1);
- }
- else if (Action1 == 1 && Action2 == 3)
- {
- print("Player atacou, Inimigo quer trocar a carta");
- bAnimations.EnemyWasAttackedWithoutCard();
- bTable.GetEnemyCard().ReceiveDamage(bTable.GetPlayerCard().GetPod());
- //voltar para escolha de carta
- Exchange(2);
- } else if (Action1 == 2 && Action2 == 3) {
- print("Player defendeu, Inimigo quer trocar a carta");
- StartCoroutine(PlayerAnimationDefense());
- //voltar para escolha de carta;
- Exchange(2);
- } else if (Action1 == 2 && Action2 == 2)
- {
- print("Ambos defenderam");
- StartCoroutine(PlayerAnimationDefense());
- StartCoroutine(EnemyAnimationDefense());
- Aftermath();
- }
- else if (Action1 == 3 && Action2 == 3)
- {
- print("Ambos querem trocar a carta");
- //voltar para escolha de carta;
- Exchange(3);
- }
- else
- {
- print("se voce esta lendo isso, provavelmente deu merda!");
- }
- }
- IEnumerator WaitForBattleHappens()
- {
- print("Coroutina Chamada");
- yield return new WaitForSeconds(3);
- Aftermath();
- }
- IEnumerator PlayerAnimationDefense()
- {
- yield return new WaitForSeconds(1.9f);
- bAnimations.PlayerIsNotDefending();
- }
- IEnumerator EnemyAnimationDefense()
- {
- yield return new WaitForSeconds(1.9f);
- bAnimations.EnemyIsNotDefending();
- }
- void Aftermath()
- {
- playerCardIsAlive = CheckIfAlive(bTable.GetPlayerCard());
- enemyCardIsAlive = CheckIfAlive(bTable.GetEnemyCard());
- if (playerCardIsAlive)
- {
- bTable.SetPlayerHasCard(true);
- if (enemyCardIsAlive)
- {
- bTable.SetEnemyHasCard(true);
- //Continuar jogo
- BattleStart();
- }
- else
- {
- bAnimations.EnemyCardDies();
- bTable.EnemyCardDestroyed();
- bTable.SetEnemyHasCard(false);
- //voltar para escolha de cartas (apenas inimigo)
- bTable.CheckCardNumbers();
- }
- }
- else
- {
- bAnimations.PlayerCardDies();
- bTable.PlayerCardDestroyed();
- bTable.SetPlayerHasCard(false);
- if (enemyCardIsAlive)
- {
- bTable.SetEnemyHasCard(true);
- //voltar para escolha de cartas (apenas player)
- bTable.CheckCardNumbers();
- }
- else
- {
- bAnimations.EnemyCardDies();
- bTable.EnemyCardDestroyed();
- bTable.SetEnemyHasCard(false);
- //voltar para escolha de cartas (ambos)
- bTable.CheckCardNumbers();
- }
- }
- }
- /// <summary>
- /// Versão de troca do aftermath (quando atroca nao é dependente apenas da carta ser destruida)
- /// </summary>
- /// <param name="type"></param>
- /// Type 1: apenas o player
- /// Type 2: apenas o inimigo
- /// Type 3: ambos
- void Exchange(int type)
- {
- playerCardIsAlive = CheckIfAlive(bTable.GetPlayerCard());
- enemyCardIsAlive = CheckIfAlive(bTable.GetEnemyCard());
- if(type == 1 && playerCardIsAlive)
- {
- bTable.SendCardToDeck();
- bTable.SetPlayerHasCard(false);
- bTable.SetEnemyHasCard(true);
- bTable.CheckCardNumbers();
- }
- else if(type == 1 && !playerCardIsAlive)
- {
- bTable.PlayerCardDestroyed();
- bAnimations.PlayerCardDies();
- bTable.SetPlayerHasCard(false);
- bTable.SetEnemyHasCard(true);
- bTable.CheckCardNumbers();
- }else if(type == 2 && enemyCardIsAlive)
- {
- bTable.SetPlayerHasCard(true);
- bTable.SetEnemyHasCard(false);
- bTable.CheckCardNumbers();
- }else if(type == 2 && !enemyCardIsAlive)
- {
- bTable.EnemyCardDestroyed();
- bAnimations.EnemyCardDies();
- bTable.SetPlayerHasCard(true);
- bTable.SetEnemyHasCard(false);
- bTable.CheckCardNumbers();
- }
- else if(type == 3)
- {
- bTable.SendCardToDeck();
- bTable.SetPlayerHasCard(false);
- bTable.SetEnemyHasCard(false);
- bTable.CheckCardNumbers();
- }
- }
- //Define a carta escolhida pelo player
- //Escolhe ação de batalha
- public void ChooseAction(int ActionValue)
- {
- holdAction = ActionValue;
- if (canChooseAction)
- {
- MultiplayerController.Instance.SendMyUpdateTypeAction(ActionValue);
- switch (ActionValue)
- {
- case 1: //Attackar
- nextAction = 1;
- canChooseAction = false;
- hasDefended = false;
- break;
- case 2: //Defender
- if (!hasDefended)
- {
- nextAction = 2;
- canChooseAction = false;
- hasDefended = true;
- }
- break;
- case 3: //Trocar
- nextAction = 3;
- canChooseAction = false;
- hasDefended = false;
- break;
- default:
- nextAction = 0;
- canChooseAction = true;
- break;
- }
- }
- }
- void PlayerAnimationsLink()
- {
- if(nextAction == 1)
- {
- bAnimations.PlayerAttacking();
- }else if(nextAction == 2)
- {
- bAnimations.PlayerDefends();
- }else if(nextAction == 3)
- {
- bAnimations.PlayerWantsToSwitch();
- }
- }
- bool CheckIfAlive(BattleCard card)
- {
- if (card.GetCon() > 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- //Reenvia as informações se necessário. "A" significa que é necessário reenviar a ação.
- //"C" significa que é necessário reenviar as infos da carta.
- public void SendMyCardAgain(string typeData)
- {
- if (typeData == "C")
- {
- NeedInfoRefresh.SetActive(false);
- waitTime = 10;
- bTable.SendMyCard();
- }
- if(typeData == "A")
- {
- NeedInfoRefresh.SetActive(false);
- waitTime = 10;
- bTable.IncreaseWaitTime(10);
- MultiplayerController.Instance.SendMyUpdateTypeAction(holdAction);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment