Roxaq

BattleController

Nov 9th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.27 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System;
  4. using System.Collections;
  5.  
  6. public class BattleController : MonoBehaviour
  7. {
  8.  
  9. private BattleTable bTable;
  10. private BattleAnimations bAnimations;
  11.  
  12. public Animator TryToReconnect;
  13. public Animator CanvasLost;
  14.  
  15. //Variaveis de Escolha do player
  16. /// <summary> Variaveis que compoem os "gatilhos" do loop de batalha </summary>
  17. /// controlam o fluxo de ações
  18. ///
  19. /// chooseCard = (True, pode escolher carta; false, ja escolheu)
  20. /// nextAction = inteiro que contem o tipo de ação escolhida pelo jogador (1 = ataque, 2 = defesa, 3 = trocar)
  21. private bool chooseCard;
  22. private int nextAction;
  23. //Variáveis que mantém dados para caso haja necessidade reenvio e variáves com dados extras
  24. //que servem para sincronizar o jogo caso haja perda de dados.
  25. private int holdAction;
  26. private int endTurn;
  27. private int Turn;
  28. private string typeData;
  29. //...//
  30.  
  31. //Variaveis do oponente (Multiplayer)
  32. /// <summary> Gatilhos do oponente </summary>
  33. /// assim como os gatilhos do player, são as indicações das ações do oponente
  34. /// enemyChooseCard = (True, pode escolher carta; false, ja escolheu)
  35. /// enemyAction = inteiro que contem o tipo de ação escolhida pelo jogador (1 = ataque, 2 = defesa, 3 = trocar) (multiplayer)
  36. private int enemyAction;
  37. private bool enemyChooseCard;
  38. private bool dataReceived;
  39. private bool enemyHasCard;
  40.  
  41. private bool hasDefended;
  42. public Button DefenseBTN;
  43. public GameObject NeedInfoRefresh;
  44. public Text vTime;
  45.  
  46. private float actionTime;
  47. private float waitTime;
  48. private float disconnectTime;
  49.  
  50. //Variaveis dos jogadores em batalha
  51. private bool playerCardIsAlive;
  52. private bool enemyCardIsAlive;
  53.  
  54. //Variaveis de controle de batalha
  55. private bool canChooseCard;
  56. private bool canChooseAction;
  57. private bool repeatBattle;
  58.  
  59. //Variaveis de DEBUG;
  60. public Text tex_ConcentraçãoCard;
  61. public Text tex_PoderCard;
  62.  
  63. void Awake()
  64. {
  65. bTable = GameObject.FindWithTag("GameController").GetComponent<BattleTable>();
  66. bAnimations = GameObject.FindWithTag("GameController").GetComponent<BattleAnimations>();
  67. TryToReconnect = GameObject.Find("Aviso").GetComponent<Animator>();
  68. TryToReconnect.enabled = false;
  69. CanvasLost = GameObject.Find("LostCanvas").GetComponent<Animator>();
  70. CanvasLost.enabled = false;
  71. }
  72.  
  73. void Start()
  74. {
  75. endTurn = 0;
  76. Turn = 0;
  77. NeedInfoRefresh.SetActive(false);
  78. }
  79.  
  80. void Update()
  81. {
  82. if (bTable.GetPlayerCard() != null)
  83. {
  84. tex_ConcentraçãoCard.text = "Con: " + bTable.GetPlayerCard().GetCon();
  85. tex_PoderCard.text = "Pod: " + bTable.GetPlayerCard().GetPod();
  86. }
  87.  
  88. if (hasDefended)
  89. {
  90. DefenseBTN.interactable = false;
  91. }else
  92. {
  93. DefenseBTN.interactable = true;
  94. }
  95. }
  96.  
  97. public void BattleStart()
  98. {
  99. nextAction = 0;
  100. enemyAction = 0;
  101. MultiplayerController.Instance.SendMyUpdateEndTurn(1);
  102. Turn += 1;
  103. print("Mandei final de turno");
  104. StartCoroutine(ChooseAction());
  105. }
  106.  
  107. public void enemyActionData(int action)
  108. {
  109. enemyAction = action;
  110. }
  111.  
  112. void EnemyAnimationsLink()
  113. {
  114. if (enemyAction == 1)
  115. {
  116. bAnimations.EnemyAttacking();
  117. }
  118. else if (enemyAction == 2)
  119. {
  120. bAnimations.EnemyDefends();
  121. }
  122. else if (enemyAction == 3)
  123. {
  124. bAnimations.EnemyWantsToSwitch();
  125. }
  126. }
  127.  
  128. void ResetAnimations()
  129. {
  130. bAnimations.PlayerIsNotDefending();
  131. }
  132.  
  133. void CallButtons()
  134. {
  135. bAnimations.CallActionButtons();
  136. }
  137.  
  138. public void IsTurnFinished(int _endTurn)
  139. {
  140. endTurn = _endTurn;
  141. print("Recebi final de turno: " + endTurn);
  142. }
  143.  
  144. IEnumerator ChooseAction()
  145. {
  146. actionTime = bTable.GetGameTime();
  147. typeData = "X";
  148. waitTime = 25;
  149. disconnectTime = 85;
  150. //3. Espera a escolha de ação
  151. ///Ativa os botões de ação
  152. print("Escolha sua ação");
  153. MultiplayerController.Instance.SendMyUpdateEndTurn(1);
  154. canChooseAction = true;
  155. while (canChooseAction)
  156. {
  157. //Os botões de ação só aparecem se o turno estiver em sincronia.
  158. if (endTurn == 1)
  159. {
  160. CallButtons();
  161. }
  162. //Espera 10 segundos para que o "endTurn" se torne verdadeiro. Se não se tornar em 10 segundos.
  163. //Abre um botão para remandar as informações da carta, que provavelmente ocasionaram o delay.
  164. if (waitTime > 0)
  165. {
  166. waitTime -= Time.deltaTime;
  167. }
  168. else if (waitTime <= 0 && endTurn == 0)
  169. {
  170. TryToReconnect.enabled = true;
  171. TryToReconnect.Play("TryToReconnect");
  172. print("Ativou refresh");
  173. SendMyCardAgain("C");
  174. }
  175. if(disconnectTime > 0)
  176. {
  177. disconnectTime -= Time.deltaTime;
  178. vTime.text = "" + (int)disconnectTime;
  179. }
  180. else if (disconnectTime <= 0)
  181. {
  182. TryToReconnect.enabled = true;
  183. TryToReconnect.Play("TryToReconnect_back");
  184. DisconnectTimeOut();
  185. }
  186. //O actionTime só passa a funcionar se o turno estiver sincronizado.
  187. if (actionTime > 0 && endTurn == 1)
  188. {
  189. actionTime -= Time.deltaTime;
  190. GameObject.Find("txt_GameTime").GetComponent<Text>().text = "" + (int)actionTime;
  191. }
  192. else if(actionTime <= 0)
  193. {
  194. GameObject.Find("txt_GameTime").GetComponent<Text>().text = "" + 0;
  195. ChooseAction(1);
  196. }
  197. yield return null;
  198. }
  199. TryToReconnect.enabled = true;
  200. TryToReconnect.Play("TryToReconnect_back");
  201. print("Player Escolheu Ação!");
  202. PlayerAnimationsLink();
  203. bAnimations.RetractActionButtons();
  204. StartCoroutine(PlayerTimeAnimation());
  205. }
  206.  
  207. public void DisconnectTimeOut()
  208. {
  209. MultiplayerController.Instance.LeaveRoom();
  210. CanvasLost.enabled = true;
  211. CanvasLost.Play("LostConnection");
  212. //GameObject LostConnection = GameObject.FindWithTag("LostCanvas");
  213. //LostConnection.GetComponent<LostConnection>().LostConnPopUp();
  214. }
  215.  
  216. IEnumerator PlayerTimeAnimation()
  217. {
  218. yield return new WaitForSeconds(1f);
  219. StartCoroutine(EnemyChooseAction());
  220. //MultiplayerController.Instance.SendMyUpdateTypeAction(holdAction);
  221. }
  222.  
  223. /*public void StopCount()
  224. {
  225. actionTime = 35;
  226. }*/
  227.  
  228. IEnumerator EnemyChooseAction()
  229. {
  230. //4. Espera de escolha de ação do inimigo
  231. //Implementar função de recebimento de ação
  232. endTurn = 0;
  233. waitTime = 25;
  234. disconnectTime = 85;
  235. MultiplayerController.Instance.SendMyUpdateEndTurn(1);
  236. //MultiplayerController.Instance.SendMyUpdateTypeAction(holdAction);
  237. print("Espere o inimigo escolher a ação");
  238. while (enemyAction == 0)
  239. {
  240. //Espera 25 segundos (20 segundos do tempo de escolha de ação + 5 segundos extras)
  241. //Caso em 25 segundos o jogo não consiga prosseguir pra receber a ação do adversário.
  242. //Ele abre um botão de refresh para reenviar as informações de ação.
  243. if(waitTime > 0)
  244. {
  245. waitTime -= Time.deltaTime;
  246. }
  247. else if(waitTime <= 0)
  248. {
  249. TryToReconnect.enabled = true;
  250. TryToReconnect.Play("TryToReconnect");
  251. print("Chamou refresh _ inimigo");
  252. SendMyCardAgain("A");
  253. }
  254. if (disconnectTime > 0)
  255. {
  256. disconnectTime -= Time.deltaTime;
  257. vTime.text = "" + (int)disconnectTime;
  258. }
  259. else if (disconnectTime <= 0)
  260. {
  261. TryToReconnect.enabled = true;
  262. TryToReconnect.Play("TryToReconnect_back");
  263. DisconnectTimeOut();
  264. }
  265. yield return null;
  266. }
  267. TryToReconnect.enabled = true;
  268. TryToReconnect.Play("TryToReconnect_back");
  269. EnemyAnimationsLink();
  270. print("Ele escolheu!");
  271. StartCoroutine(EnemyTimeAnimation());
  272. }
  273.  
  274. IEnumerator EnemyTimeAnimation()
  275. {
  276. yield return new WaitForSeconds(3.5f);
  277. StartCoroutine(BattleLoop());
  278. }
  279.  
  280. IEnumerator BattleLoop()
  281. {
  282. //Receber os valores da cartas do inimigo
  283. //Criar uma função que crie um novo objeto, com os valores da carta do inimigo
  284. //Utilizar o objeto enemy action e enemy card nesta proxima função
  285. dataReceived = true;
  286. while (!dataReceived)
  287. {
  288. yield return null;
  289. }
  290. BattleAction(nextAction, enemyAction);
  291.  
  292. }
  293.  
  294.  
  295. //realiza as ações de batalha
  296. void BattleAction(int Action1, int Action2)
  297. {
  298. if (Action1 == 1 && Action2 == 1)
  299. {
  300. print("Ambas atacaram");
  301. bAnimations.PlayerWasAttacked();
  302. bAnimations.EnemyWasAttacked();
  303. bTable.GetPlayerCard().ReceiveDamage(bTable.GetEnemyCard().GetPod());
  304. bTable.GetEnemyCard().ReceiveDamage(bTable.GetPlayerCard().GetPod());
  305.  
  306. StartCoroutine(WaitForBattleHappens());
  307. }
  308. else if (Action1 == 2 && Action2 == 1)
  309. {
  310. print("Player defendeu, Inimigo Atacou");
  311. StartCoroutine(PlayerAnimationDefense());
  312. bAnimations.EnemyWasAttacked();
  313. bAnimations.PlayerWasAttackedWhileDefending();
  314. bTable.GetPlayerCard().ReceiveDamage(bTable.GetEnemyCard().GetPod() / 2);
  315. bTable.GetEnemyCard().ReceiveDamage(bTable.GetEnemyCard().GetPod() / 2);
  316.  
  317. StartCoroutine(WaitForBattleHappens());
  318. }
  319. else if (Action1 == 1 && Action2 == 2)
  320. {
  321. print("Player atacou, Inimigo defendeu");
  322. StartCoroutine(EnemyAnimationDefense());
  323. bAnimations.PlayerWasAttacked();
  324. bAnimations.EnemyWasAttackedWhileDefending();
  325. bTable.GetPlayerCard().ReceiveDamage(bTable.GetPlayerCard().GetPod() / 2);
  326. bTable.GetEnemyCard().ReceiveDamage(bTable.GetPlayerCard().GetPod() / 2);
  327.  
  328. StartCoroutine(WaitForBattleHappens());
  329. }
  330. else if (Action1 == 3 && Action2 == 1)
  331. {
  332. print("Player quer trocar a carta, Inimigo atacou");
  333. bAnimations.PlayerWasAttackedWithoutCard();
  334. bTable.GetPlayerCard().ReceiveDamage(bTable.GetEnemyCard().GetPod());
  335. //Checar se a carta contem concentração e voltar para escolha de carta.
  336. Exchange(1);
  337. } else if (Action1 == 3 && Action2 == 2)
  338. {
  339. StartCoroutine(EnemyAnimationDefense());
  340. print("Player quer trocar a carta, Inimigo defendeu");
  341. //voltar para escolha de carta;
  342. Exchange(1);
  343. }
  344. else if (Action1 == 1 && Action2 == 3)
  345. {
  346. print("Player atacou, Inimigo quer trocar a carta");
  347. bAnimations.EnemyWasAttackedWithoutCard();
  348. bTable.GetEnemyCard().ReceiveDamage(bTable.GetPlayerCard().GetPod());
  349. //voltar para escolha de carta
  350. Exchange(2);
  351. } else if (Action1 == 2 && Action2 == 3) {
  352. print("Player defendeu, Inimigo quer trocar a carta");
  353. StartCoroutine(PlayerAnimationDefense());
  354. //voltar para escolha de carta;
  355. Exchange(2);
  356. } else if (Action1 == 2 && Action2 == 2)
  357. {
  358. print("Ambos defenderam");
  359. StartCoroutine(PlayerAnimationDefense());
  360. StartCoroutine(EnemyAnimationDefense());
  361. Aftermath();
  362. }
  363. else if (Action1 == 3 && Action2 == 3)
  364. {
  365. print("Ambos querem trocar a carta");
  366. //voltar para escolha de carta;
  367. Exchange(3);
  368. }
  369. else
  370. {
  371. print("se voce esta lendo isso, provavelmente deu merda!");
  372. }
  373. }
  374.  
  375. IEnumerator WaitForBattleHappens()
  376. {
  377. print("Coroutina Chamada");
  378. yield return new WaitForSeconds(3);
  379. Aftermath();
  380. }
  381.  
  382. IEnumerator PlayerAnimationDefense()
  383. {
  384. yield return new WaitForSeconds(1.9f);
  385. bAnimations.PlayerIsNotDefending();
  386. }
  387.  
  388. IEnumerator EnemyAnimationDefense()
  389. {
  390. yield return new WaitForSeconds(1.9f);
  391. bAnimations.EnemyIsNotDefending();
  392. }
  393.  
  394.  
  395.  
  396. void Aftermath()
  397. {
  398. playerCardIsAlive = CheckIfAlive(bTable.GetPlayerCard());
  399. enemyCardIsAlive = CheckIfAlive(bTable.GetEnemyCard());
  400.  
  401. if (playerCardIsAlive)
  402. {
  403. bTable.SetPlayerHasCard(true);
  404. if (enemyCardIsAlive)
  405. {
  406. bTable.SetEnemyHasCard(true);
  407. //Continuar jogo
  408. BattleStart();
  409. }
  410. else
  411. {
  412. bAnimations.EnemyCardDies();
  413. bTable.EnemyCardDestroyed();
  414. bTable.SetEnemyHasCard(false);
  415. //voltar para escolha de cartas (apenas inimigo)
  416. bTable.CheckCardNumbers();
  417. }
  418. }
  419. else
  420. {
  421. bAnimations.PlayerCardDies();
  422. bTable.PlayerCardDestroyed();
  423. bTable.SetPlayerHasCard(false);
  424. if (enemyCardIsAlive)
  425. {
  426. bTable.SetEnemyHasCard(true);
  427. //voltar para escolha de cartas (apenas player)
  428. bTable.CheckCardNumbers();
  429. }
  430. else
  431. {
  432. bAnimations.EnemyCardDies();
  433. bTable.EnemyCardDestroyed();
  434. bTable.SetEnemyHasCard(false);
  435. //voltar para escolha de cartas (ambos)
  436. bTable.CheckCardNumbers();
  437. }
  438. }
  439. }
  440.  
  441. /// <summary>
  442. /// Versão de troca do aftermath (quando atroca nao é dependente apenas da carta ser destruida)
  443. /// </summary>
  444. /// <param name="type"></param>
  445. /// Type 1: apenas o player
  446. /// Type 2: apenas o inimigo
  447. /// Type 3: ambos
  448. void Exchange(int type)
  449. {
  450. playerCardIsAlive = CheckIfAlive(bTable.GetPlayerCard());
  451. enemyCardIsAlive = CheckIfAlive(bTable.GetEnemyCard());
  452.  
  453.  
  454. if(type == 1 && playerCardIsAlive)
  455. {
  456.  
  457. bTable.SendCardToDeck();
  458. bTable.SetPlayerHasCard(false);
  459. bTable.SetEnemyHasCard(true);
  460. bTable.CheckCardNumbers();
  461. }
  462. else if(type == 1 && !playerCardIsAlive)
  463. {
  464. bTable.PlayerCardDestroyed();
  465. bAnimations.PlayerCardDies();
  466. bTable.SetPlayerHasCard(false);
  467. bTable.SetEnemyHasCard(true);
  468. bTable.CheckCardNumbers();
  469. }else if(type == 2 && enemyCardIsAlive)
  470. {
  471. bTable.SetPlayerHasCard(true);
  472. bTable.SetEnemyHasCard(false);
  473. bTable.CheckCardNumbers();
  474. }else if(type == 2 && !enemyCardIsAlive)
  475. {
  476. bTable.EnemyCardDestroyed();
  477. bAnimations.EnemyCardDies();
  478. bTable.SetPlayerHasCard(true);
  479. bTable.SetEnemyHasCard(false);
  480. bTable.CheckCardNumbers();
  481. }
  482. else if(type == 3)
  483. {
  484. bTable.SendCardToDeck();
  485. bTable.SetPlayerHasCard(false);
  486. bTable.SetEnemyHasCard(false);
  487. bTable.CheckCardNumbers();
  488. }
  489. }
  490.  
  491. //Define a carta escolhida pelo player
  492. //Escolhe ação de batalha
  493. public void ChooseAction(int ActionValue)
  494. {
  495. holdAction = ActionValue;
  496. if (canChooseAction)
  497. {
  498. MultiplayerController.Instance.SendMyUpdateTypeAction(ActionValue);
  499.  
  500. switch (ActionValue)
  501. {
  502. case 1: //Attackar
  503. nextAction = 1;
  504. canChooseAction = false;
  505. hasDefended = false;
  506. break;
  507. case 2: //Defender
  508. if (!hasDefended)
  509. {
  510. nextAction = 2;
  511. canChooseAction = false;
  512. hasDefended = true;
  513. }
  514. break;
  515. case 3: //Trocar
  516. nextAction = 3;
  517. canChooseAction = false;
  518. hasDefended = false;
  519. break;
  520. default:
  521. nextAction = 0;
  522. canChooseAction = true;
  523. break;
  524. }
  525. }
  526. }
  527.  
  528. void PlayerAnimationsLink()
  529. {
  530. if(nextAction == 1)
  531. {
  532. bAnimations.PlayerAttacking();
  533. }else if(nextAction == 2)
  534. {
  535. bAnimations.PlayerDefends();
  536. }else if(nextAction == 3)
  537. {
  538. bAnimations.PlayerWantsToSwitch();
  539. }
  540. }
  541.  
  542. bool CheckIfAlive(BattleCard card)
  543. {
  544. if (card.GetCon() > 0)
  545. {
  546. return true;
  547. }
  548. else
  549. {
  550. return false;
  551. }
  552. }
  553.  
  554. //Reenvia as informações se necessário. "A" significa que é necessário reenviar a ação.
  555. //"C" significa que é necessário reenviar as infos da carta.
  556. public void SendMyCardAgain(string typeData)
  557. {
  558. if (typeData == "C")
  559. {
  560. NeedInfoRefresh.SetActive(false);
  561. waitTime = 10;
  562. bTable.SendMyCard();
  563. }
  564. if(typeData == "A")
  565. {
  566. NeedInfoRefresh.SetActive(false);
  567. waitTime = 10;
  568. bTable.IncreaseWaitTime(10);
  569. MultiplayerController.Instance.SendMyUpdateTypeAction(holdAction);
  570. }
  571. }
  572.  
  573. }
Advertisement
Add Comment
Please, Sign In to add comment