Advertisement
Learning000001

Untitled

Jun 18th, 2024
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.36 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using DG.Tweening;
  7. using Photon.Pun;
  8. using UnityEditor.Rendering;
  9. using Unity.VisualScripting;
  10.  
  11. public class GameplayManager : MonoBehaviour
  12. {
  13. public static GameplayManager Instance;
  14. public static bool IsPvpGame;
  15. public static Action UpdatedRound;
  16. public static Action UpdatedGameState;
  17. public static Action UpdatedBet;
  18. public static Action OnFinishedGameplayLoop;
  19. public static Action<GameResult> GameEnded;
  20. public static Action<int, Color, int> OnFlashPlace;
  21. public static Action<LaneLocation, bool, Color, int> OnFlashWholePlace;
  22. public static Action<LaneLocation, bool, Color> OnHighlihtWholePlace;
  23. public static Action<LaneLocation, bool> OnHighlihtWholePlaceDotted;
  24. public static Action<LaneLocation, bool, Color, int> OnFlashAllSpotsOnLocation;
  25. public static Action<LaneLocation, bool, Color> OnHideHighlightWholePlace;
  26. public static Action<LaneLocation, bool> OnHideHighlightWholePlaceDotted;
  27. public static bool DrewCardDirectlyToHand;
  28.  
  29. public GameplayPlayer MyPlayer;
  30. public GameplayPlayer OpponentPlayer;
  31. public Dictionary<LaneDisplay, LaneAbility> LaneAbilities = new Dictionary<LaneDisplay, LaneAbility>();
  32.  
  33. [field: SerializeField] public int MaxAmountOfCardsInHand { get; private set; }
  34. [field: SerializeField] public int DurationOfRound { get; private set; }
  35. [field: SerializeField] public TableHandler TableHandler { get; private set; }
  36.  
  37. public CommandsHandler CommandsHandler = new CommandsHandler();
  38.  
  39. [SerializeField] protected EndTurnHandler endTurnHandler;
  40. [SerializeField] protected int maxRounds = 6;
  41. [SerializeField] protected List<LaneDisplay> lanes;
  42. [SerializeField] protected GameObject[] flags;
  43. [SerializeField] protected GameObject[] playsFirstDisplays;
  44. [SerializeField] private TutorialImages tutorialImages;
  45.  
  46. private GameplayState gameplayState = GameplayState.StartingAnimation;
  47. private int currentRound;
  48.  
  49. protected bool opponentFinished;
  50. protected bool iFinished;
  51. protected bool resolvedEndOfTheRound;
  52. protected int startingAmountOfCards = 3;
  53. protected int currentBet = 1;
  54. protected List<int> excludeLaneAbilities = new List<int>();
  55. protected bool locationRevealed;
  56.  
  57. public bool IFinished => iFinished;
  58.  
  59. public bool IsLastRound => CurrentRound == maxRounds;
  60.  
  61. public GameplayState GameplayState
  62. {
  63. get
  64. {
  65. return gameplayState;
  66. }
  67. set
  68. {
  69. gameplayState = value;
  70. UpdatedGameState?.Invoke();
  71. }
  72. }
  73.  
  74. public int CurrentRound
  75. {
  76. get
  77. {
  78. return currentRound;
  79. }
  80. set
  81. {
  82. currentRound = value;
  83. UpdatedRound?.Invoke();
  84. }
  85. }
  86.  
  87. public List<LaneDisplay> Lanes => lanes;
  88.  
  89. public int CurrentBet => currentBet;
  90.  
  91. public int MaxAmountOfRounds => maxRounds;
  92.  
  93. protected virtual void OnEnable()
  94. {
  95. EndTurnHandler.OnEndTurn += EndTurn;
  96. FlagClickHandler.OnForefiet += Forfiet;
  97. GameEnded += UpdateQommonsWinLose;
  98. GameEnded += TriggerGameEndEvents;
  99. }
  100.  
  101. protected virtual void OnDisable()
  102. {
  103. CommandsHandler.Close();
  104. EndTurnHandler.OnEndTurn -= EndTurn;
  105. FlagClickHandler.OnForefiet -= Forfiet;
  106. GameEnded -= UpdateQommonsWinLose;
  107. GameEnded -= TriggerGameEndEvents;
  108. }
  109.  
  110. protected virtual void EndTurn()
  111. {
  112. GameplayState = GameplayState.Waiting;
  113. iFinished = true;
  114. MyPlayer.FinishedTurn?.Invoke();
  115. }
  116.  
  117. protected virtual void Forfiet()
  118. {
  119. StopAllCoroutines();
  120. GameEnded?.Invoke(GameResult.IForefiet);
  121. }
  122.  
  123. public void ForceEndGame(GameResult _result)
  124. {
  125. StopAllCoroutines();
  126. GameEnded?.Invoke(_result);
  127. }
  128.  
  129. private void UpdateQommonsWinLose(GameResult _result)
  130. {
  131. switch (_result)
  132. {
  133. case GameResult.IForefiet or GameResult.ILost:
  134. FirebaseManager.Instance.UpdateCardsWinLoseCount(DataManager.Instance.PlayerData.CardIdsInDeck, false);
  135. break;
  136. case GameResult.IWon or GameResult.Escaped:
  137. FirebaseManager.Instance.UpdateCardsWinLoseCount(DataManager.Instance.PlayerData.CardIdsInDeck, true);
  138. break;
  139. }
  140. }
  141.  
  142. private void TriggerGameEndEvents(GameResult _result)
  143. {
  144. if(!DataManager.Instance.PlayerData.HasPlayedFirstGame)
  145. {
  146. DataManager.Instance.PlayerData.HasPlayedFirstGame = true;
  147. DataManager.Instance.PlayerData.HasFinishedFirstGame = true;
  148. }
  149. else
  150. {
  151. DataManager.Instance.CanShowPwaOverlay = true;
  152. }
  153.  
  154. if (_result is not (GameResult.IWon or GameResult.Escaped))
  155. {
  156. return;
  157. }
  158.  
  159. if (CurrentBet>2)
  160. {
  161. EventsManager.WinMatchWithADouble?.Invoke();
  162. }
  163. EventsManager.WinMatch?.Invoke();
  164.  
  165. CheckForPowerEvents(TableHandler.GetPower(true,LaneLocation.Top));
  166. CheckForPowerEvents(TableHandler.GetPower(true,LaneLocation.Mid));
  167. CheckForPowerEvents(TableHandler.GetPower(true,LaneLocation.Bot));
  168.  
  169. CheckForCardEvents(TableHandler.GetCards(true, LaneLocation.Top).Count);
  170. CheckForCardEvents(TableHandler.GetCards(true, LaneLocation.Mid).Count);
  171. CheckForCardEvents(TableHandler.GetCards(true, LaneLocation.Bot).Count);
  172.  
  173. void CheckForPowerEvents(float _power)
  174. {
  175. if (_power<=100)
  176. {
  177. EventsManager.WinALocationWithPowerLess100?.Invoke();
  178. }
  179. else if (_power>=200)
  180. {
  181. EventsManager.WinALocationWithPowerMore200?.Invoke();
  182. }
  183. }
  184.  
  185. void CheckForCardEvents(int _cardAmount)
  186. {
  187. if (_cardAmount==1)
  188. {
  189. EventsManager.WinALocationWith1Card?.Invoke();
  190. }
  191. else if (_cardAmount==4)
  192. {
  193. EventsManager.WinALocationWith4Card?.Invoke();
  194. }
  195. }
  196. }
  197.  
  198. protected virtual void Awake()
  199. {
  200. Instance = this;
  201. IsPvpGame = false;
  202. }
  203.  
  204. private void Start()
  205. {
  206. AudioManager.Instance.ChangeBackgroundMusic(AudioManager.GAME);
  207. GameplayUI.Instance.StartingAnimations(StartGameplay);
  208. }
  209.  
  210. public virtual void AddPowerOfQoomonOnPlace(int _placeId, int _power)
  211. {
  212. LanePlaceIdentifier _place = FindObjectsOfType<LanePlaceIdentifier>().ToList().Find(_place => _place.Id == _placeId);
  213. CardObject _cardOnPlace = _place.GetComponentInChildren<CardObject>();
  214. _cardOnPlace.Stats.Power += _power;
  215. }
  216.  
  217. protected virtual void StartGameplay()
  218. {
  219. StartCoroutine(StartRoutine());
  220. IEnumerator StartRoutine()
  221. {
  222. CommandsHandler.Setup();
  223. CurrentRound = 0;
  224. SetupPlayers();
  225. TableHandler.Setup();
  226.  
  227. bool _canContinue = false;
  228.  
  229. if (!DataManager.Instance.PlayerData.HasPlayedFirstGame && !DataManager.Instance.PlayerData.HasFinishedFirstGame)
  230. {
  231. tutorialImages.Setup(AllowContinue);
  232. }
  233. else
  234. {
  235. _canContinue = true;
  236. }
  237.  
  238. yield return new WaitUntil(() => _canContinue);
  239. StartCoroutine(GameplayRoutine());
  240.  
  241. void AllowContinue()
  242. {
  243. _canContinue = true;
  244. }
  245. }
  246. }
  247.  
  248. protected virtual void SetupPlayers()
  249. {
  250. MyPlayer.Setup();
  251. OpponentPlayer.Setup();
  252. }
  253.  
  254. protected virtual IEnumerator InitialDraw()
  255. {
  256. yield return StartCoroutine(InitialDraw(MyPlayer, startingAmountOfCards));
  257. yield return StartCoroutine(InitialDraw(OpponentPlayer, startingAmountOfCards));
  258. }
  259.  
  260. protected IEnumerator InitialDraw(GameplayPlayer _player, int _startingAmountOfCards)
  261. {
  262. yield return StartCoroutine(CheckForCardsThatShouldMoveToHand(_player));
  263.  
  264. int _amountOfCardsInHand = _player.AmountOfCardsInHand;
  265. for (int i = 0; i < startingAmountOfCards-_amountOfCardsInHand; i++)
  266. {
  267. DrawCard(_player);
  268. }
  269. }
  270.  
  271. public virtual void DrawCard()
  272. {
  273. DrawCard(MyPlayer);
  274. DrawCard(OpponentPlayer);
  275. }
  276.  
  277. public void DrawCard(GameplayPlayer _player)
  278. {
  279. int _amountOfCardsInHand = _player.AmountOfCardsInHand;
  280. if (_amountOfCardsInHand >= MaxAmountOfCardsInHand)
  281. {
  282. return;
  283. }
  284.  
  285. CardObject _drawnCard = _player.DrawCard();
  286. if (_drawnCard==null)
  287. {
  288. return;
  289. }
  290. _player.AddCardToHand(_drawnCard);
  291. }
  292.  
  293. public virtual void DrawCardFromOpponentsDeck(bool _isMy)
  294. {
  295. GameplayPlayer _player = _isMy ? MyPlayer : OpponentPlayer;
  296. GameplayPlayer _opponentPlayer = _isMy ? OpponentPlayer : MyPlayer;
  297. int _amountOfCardsInHand = _player.AmountOfCardsInHand;
  298. if (_amountOfCardsInHand >= MaxAmountOfCardsInHand)
  299. {
  300. return;
  301. }
  302.  
  303. CardObject _drawnCard = _opponentPlayer.DrawCard();
  304. if (_drawnCard==null)
  305. {
  306. return;
  307. }
  308.  
  309. int _cardId = _drawnCard.Details.Id;
  310. Destroy(_drawnCard.gameObject);
  311. _player.AddCardToHand(CardsManager.Instance.CreateCard(_cardId, _isMy));
  312. }
  313.  
  314. protected virtual IEnumerator GameplayRoutine()
  315. {
  316. yield return new WaitUntil(ReadyToStart);
  317. yield return StartCoroutine(InitialDraw());
  318. yield return new WaitForSeconds(1); //wait for cards in hand to get to position
  319. while (CurrentRound < maxRounds)
  320. {
  321. CommandsHandler.MyOriginalCommandsThisTurn.Clear();
  322. int _whoPlaysFirst = TableHandler.WhichCardsToRevealFrist();
  323. ShowFlag(_whoPlaysFirst);
  324. opponentFinished = false;
  325. iFinished = false;
  326. resolvedEndOfTheRound = false;
  327. GameplayState = GameplayState.ResolvingBeginingOfRound;
  328. CurrentRound++;
  329. if (currentRound <= 3)
  330. {
  331. locationRevealed = false;
  332. }
  333. yield return new WaitForSeconds(1f); //duration of round animation
  334.  
  335. AudioManager.Instance.PlaySoundEffect(AudioManager.REVEAL);
  336. StartCoroutine(RevealLocation());
  337. StartCoroutine(ShowRevealText());
  338. yield return new WaitUntil(() => locationRevealed);
  339. yield return StartCoroutine(RoundCheckForCardsThatShouldMoveToHand());
  340. if (!DrewCardDirectlyToHand||CurrentRound==1)
  341. {
  342. RoundDrawCard();
  343. }
  344.  
  345. DrewCardDirectlyToHand = false;
  346.  
  347. GameplayState = GameplayState.Playing;
  348. yield return new WaitUntil(() => iFinished && opponentFinished);
  349.  
  350. GameplayState = GameplayState.ResolvingEndOfRound;
  351. var _addQommonOnEndOfTheTurn = FindObjectOfType<LaneAbilityOnTurnXAllPutCardHere>();
  352. if (_addQommonOnEndOfTheTurn)
  353. {
  354. if (_addQommonOnEndOfTheTurn.Round == currentRound)
  355. {
  356. yield return new WaitForSeconds(2);
  357. }
  358. }
  359. StartCoroutine(RevealCards(_whoPlaysFirst));
  360. yield return new WaitUntil(() => resolvedEndOfTheRound);
  361. yield return new WaitForSeconds(1);
  362. OnFinishedGameplayLoop?.Invoke();
  363. }
  364.  
  365. AcceptAutoBet();
  366.  
  367. bool _playBackgroundMusic = DataManager.Instance.PlayerData.PlayBackgroundMusic;
  368. DataManager.Instance.PlayerData.PlayBackgroundMusic = false;
  369. yield return new WaitForSeconds(0.5f);
  370.  
  371. bool _canContinue = false;
  372. for (int i = 0; i < Lanes.Count; i++)
  373. {
  374. Lanes[i].ShowWinner(Continue);
  375. yield return new WaitUntil(() => _canContinue);
  376. _canContinue = false;
  377. }
  378. yield return new WaitForSeconds(1);
  379. DataManager.Instance.PlayerData.PlayBackgroundMusic = _playBackgroundMusic;
  380. GameResult _result = TableHandler.CalculateWinner();
  381. GameEnded?.Invoke(_result);
  382.  
  383. void Continue()
  384. {
  385. _canContinue = true;
  386. }
  387. }
  388.  
  389. protected virtual void AcceptAutoBet()
  390. {
  391. BetClickHandler.Instance.AcceptAutoBet();
  392. OpponentAcceptedBet();
  393. }
  394.  
  395. protected virtual bool ReadyToStart()
  396. {
  397. return true;
  398. }
  399.  
  400. protected virtual IEnumerator RoundCheckForCardsThatShouldMoveToHand()
  401. {
  402. yield return StartCoroutine(CheckForCardsThatShouldMoveToHand(MyPlayer));
  403. yield return StartCoroutine(CheckForCardsThatShouldMoveToHand(OpponentPlayer));
  404. }
  405.  
  406. protected virtual void RoundDrawCard()
  407. {
  408. DrawCard(MyPlayer);
  409. DrawCard(OpponentPlayer);
  410. }
  411.  
  412. protected virtual IEnumerator RevealLocation()
  413. {
  414. if (currentRound > 3)
  415. {
  416. yield break;
  417. }
  418.  
  419. LaneAbility _laneAbility = GetLaneAbility();
  420. yield return RevealLocation(_laneAbility.Id);
  421. Destroy(_laneAbility.gameObject);
  422. }
  423.  
  424. protected IEnumerator ShowRevealText()
  425. {
  426. if (currentRound > 3)
  427. {
  428. yield break;
  429. }
  430.  
  431. int _laneCounter = 0;
  432. for (int _i =currentRound; _i < 3; _i++)
  433. {
  434. string _text = string.Empty;
  435. if (_laneCounter==0)
  436. {
  437. _text = "Will be revealed next turn";
  438. }
  439. else if (_laneCounter == 1)
  440. {
  441. _text = "Will be revealed in 2 turns";
  442. }
  443.  
  444. _laneCounter++;
  445. lanes[_i].AbilityDisplay.Reveal(_text);
  446. }
  447. }
  448.  
  449. protected virtual LaneAbility GetLaneAbility()
  450. {
  451. return LaneAbilityManager.Instance.GetLaneAbility(excludeLaneAbilities);
  452. }
  453.  
  454. protected IEnumerator RevealLocation(int _abilityID)
  455. {
  456. bool _canContinue = false;
  457. LaneAbility _laneAbility = LaneAbilityManager.Instance.GetLaneAbility(_abilityID);
  458. LaneAbilities.Add(Lanes[currentRound - 1], _laneAbility);
  459. excludeLaneAbilities.Add(_abilityID);
  460. int _laneIndex = currentRound - 1;
  461. _laneAbility.Setup(lanes[_laneIndex]);
  462. lanes[_laneIndex].AbilityDisplay.Reveal(_laneAbility.Description,_laneAbility.FontSize, Revealed);
  463. _canContinue = false;
  464.  
  465. yield return new WaitUntil(() => _canContinue);
  466. yield return new WaitForSeconds(0.5f); //add small delay
  467. locationRevealed = true;
  468.  
  469. void Revealed()
  470. {
  471. _canContinue = true;
  472. }
  473. }
  474.  
  475. protected IEnumerator CheckForCardsThatShouldMoveToHand(GameplayPlayer _player)
  476. {
  477. bool _finished = false;
  478. _player.CheckForCardsThatShouldMoveToHand(Finished);
  479. yield return new WaitUntil(() => _finished);
  480.  
  481. void Finished()
  482. {
  483. _finished = true;
  484. }
  485. }
  486.  
  487. protected IEnumerator RevealCards(int _whoPlaysFirst)
  488. {
  489. while (CommandsHandler.MyCommands.Count>0|| CommandsHandler.OpponentCommands.Count>0)
  490. {
  491. foreach (var _command in CommandsHandler.OpponentCommands)
  492. {
  493. _command.Card.PrepareForReveal();
  494. }
  495.  
  496. AddCommands(CommandsHandler.MyCommands,true);
  497. AddCommands(CommandsHandler.OpponentCommands,false);
  498.  
  499. yield return StartCoroutine(TableHandler.RevealCards(_whoPlaysFirst == -1 ? CommandsHandler.MyCommands : CommandsHandler.OpponentCommands)); //show first set of cards
  500. yield return StartCoroutine(TableHandler.RevealCards(_whoPlaysFirst == -1 ? CommandsHandler.OpponentCommands : CommandsHandler.MyCommands)); // show secound set of cards
  501.  
  502. yield return new WaitForSeconds(2);//some delay
  503.  
  504. void AddCommands(List<PlaceCommand> _commands, bool _isMy)
  505. {
  506. List<PlaceCommand> _commandsThisTurn =
  507. _isMy ? CommandsHandler.MyCommandsThisTurn : CommandsHandler.OpponentCommandsThisTurn;
  508.  
  509. foreach (var _command in _commands.ToList())
  510. {
  511. if (_commandsThisTurn.Contains(_command))
  512. {
  513. continue;
  514. }
  515.  
  516. _commandsThisTurn.Add(_command);
  517. }
  518. }
  519. }
  520.  
  521. CommandsHandler.MyCommandsThisTurn.Clear();
  522. CommandsHandler.OpponentCommandsThisTurn.Clear();
  523. resolvedEndOfTheRound = true;
  524. }
  525.  
  526. protected void ShowFlag(int _whoPlaysFirst)
  527. {
  528. if (_whoPlaysFirst == -1)
  529. {
  530. flags[0].SetActive(true);
  531. playsFirstDisplays[0].SetActive(true);
  532. flags[1].SetActive(false);
  533. playsFirstDisplays[1].SetActive(false);
  534. }
  535. else
  536. {
  537. flags[0].SetActive(false);
  538. playsFirstDisplays[0].SetActive(false);
  539. flags[1].SetActive(true);
  540. playsFirstDisplays[1].SetActive(true);
  541. }
  542. }
  543.  
  544. public virtual void ReturnToWaitingState()
  545. {
  546. if (endTurnHandler.TimeLeft > 2)
  547. {
  548. GameplayState = GameplayState.Playing;
  549. }
  550.  
  551. foreach (var _command in CommandsHandler.MyCommands)
  552. {
  553. _command.Card.GetComponent<CardInteractions>().CanDrag = true;
  554. }
  555. iFinished = false;
  556. }
  557.  
  558. public virtual void Bet()
  559. {
  560. StartCoroutine(BetRoutine());
  561.  
  562. IEnumerator BetRoutine()
  563. {
  564. int _currentRound = currentRound;
  565. yield return new WaitForSeconds(3);
  566. if (_currentRound!=CurrentRound)
  567. {
  568. yield break;
  569. }
  570. OpponentAcceptedBet();
  571. }
  572. }
  573.  
  574.  
  575. public virtual void OpponentAcceptedBet()
  576. {
  577. currentBet *= 2;
  578. UpdatedBet?.Invoke();
  579. }
  580.  
  581. public void OpponentFinished()
  582. {
  583. opponentFinished = true;
  584. }
  585.  
  586. public virtual void UpdateQommonCosts(int _amount)
  587. {
  588. MyPlayer.UpdateQommonCost(_amount);
  589. OpponentPlayer.UpdateQommonCost(_amount);
  590. }
  591.  
  592. public void FlashLocation(int _locationId, Color _color, int _amount)
  593. {
  594. OnFlashPlace?.Invoke(_locationId, _color, _amount);
  595. }
  596.  
  597. public void FlashWholeLocation(LaneLocation _location, bool _mySide, Color _color, int _amount)
  598. {
  599. OnFlashWholePlace?.Invoke(_location, _mySide, _color, _amount);
  600. }
  601.  
  602. public void HighlihtWholeLocation(LaneLocation _location, bool _mySide, Color _color)
  603. {
  604. OnHighlihtWholePlace?.Invoke(_location, _mySide, _color);
  605. }
  606.  
  607. public void HighlihtWholeLocationDotted(LaneLocation _location, bool _mySide)
  608. {
  609. OnHighlihtWholePlaceDotted?.Invoke(_location, _mySide);
  610. }
  611.  
  612. public void FlashAllSpotsOnLocation(LaneLocation _location, bool _mySide, Color _color, int _amount)
  613. {
  614. OnFlashAllSpotsOnLocation?.Invoke(_location, _mySide, _color, _amount);
  615. }
  616.  
  617. public void HideHighlihtWholeLocation(LaneLocation _location, bool _mySide, Color _color)
  618. {
  619. OnHideHighlightWholePlace?.Invoke(_location, _mySide, _color);
  620. }
  621.  
  622. public void HideHighlihtWholeLocationDotted(LaneLocation _location, bool _mySide)
  623. {
  624. OnHideHighlightWholePlaceDotted?.Invoke(_location,_mySide);
  625. }
  626. private void OnDestroy()
  627. {
  628. DOTween.KillAll();
  629. }
  630.  
  631. public virtual void TellOpponentThatIDiscardedACard(CardObject _card)
  632. {
  633. if (_card.IsMy)
  634. {
  635. return;
  636. }
  637. ShowOpponentDiscardedACard(_card.Details.Id);
  638. }
  639.  
  640. protected void ShowOpponentDiscardedACard(int _cardId)
  641. {
  642. OpponentDiscardedCardDisplay.Instance.Show(_cardId);
  643. }
  644.  
  645. public void SetCurrentRoundWithoutUpdate(int _amount)
  646. {
  647. currentRound = _amount;
  648. }
  649. }
  650.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement