Advertisement
Guest User

Untitled

a guest
Aug 17th, 2023
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.42 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using TMPro;
  6. using Photon.Realtime;
  7. using Photon.Pun;
  8. using ExitGames.Client.Photon;
  9.  
  10. public class GameManager : MonoBehaviour
  11. {
  12. public static GameManager Instance;
  13. PhotonView view;
  14. public int currentTurnPlayerNo = -1; // Determine player order
  15. # region Tokens
  16. [SerializeField] GameObject[] tokenPrefab = new GameObject[4];
  17.  
  18. [SerializeField] GameObject[] blueTokenLocations = new GameObject[4];
  19. [SerializeField] GameObject blueParentLocation;
  20. [SerializeField] GameObject blueTokenPrefab;
  21. [Space(50)]
  22. [SerializeField] GameObject[] yellowTokenLocations = new GameObject[4];
  23. [SerializeField] GameObject yellowParentLocation;
  24. [SerializeField] GameObject yellowTokenPrefab;
  25. [Space(50)]
  26. # endregion
  27. [Header("Dont mind this variable, its a dummy variable to experiment on something")]
  28. [SerializeField] GameObject Entity;
  29. [SerializeField] GameObject Entity1;
  30.  
  31. public List<PlayerController> allPlayerControllersInTurnOrder;
  32.  
  33. //public GameType gameType;
  34. public List<PlayerProperties> allPlayers;
  35. public List<PlayerProperties> playersInCurrentGame;
  36. public bool giveTurnAgain;
  37.  
  38. [Space(5)]
  39. [Header("Paths for Players")]
  40. [Space(10)]
  41. public List<PathBlockProperties> wholeRegularPath;
  42. public List<PathBlockProperties> bluePawnPath;
  43. public List<PathBlockProperties> redPawnPath;
  44. public List<PathBlockProperties> yellowPawnPath;
  45. public List<PathBlockProperties> greenPawnPath;
  46.  
  47. [Header("Timer Related")]
  48. public float timeForEachTurn;
  49. public float turnTimePassed;
  50. //public bool tokenMoved;
  51.  
  52.  
  53. [Header("Board Highlight")]
  54. public float highlighterAlpha = 0.55f;
  55. public float highlighterAnimationSpeed = 1f;
  56.  
  57. //[ContextMenu("Multiplayer")]
  58. //public GameObject photonViewPrefab;
  59.  
  60. public string[] namesForBots;
  61.  
  62. private void Awake()
  63. {
  64. Instance = this;
  65. view = GetComponent<PhotonView>();
  66.  
  67. }
  68.  
  69. private void Start()
  70. {
  71. namesForBots = new string[] { "Avery", "River","Parker","Quinn","Ryan","Dylan","Noah","Ezra","Emery","Kai","Nova","August",
  72. "Ellis","Everest","Onyx","Ridley","Tatum","Royal","Wren","Kris","Rene"};
  73.  
  74. // = MainMenuManager.Instance.gameType;
  75.  
  76. if (MainMenuManager.Instance.gameType == MainMenuManager.GameType.TwoPlayer)
  77. {
  78.  
  79. playersInCurrentGame.Add(allPlayers[0]);
  80. allPlayers[1].playerController.transform.gameObject.SetActive(false);
  81. playersInCurrentGame.Add(allPlayers[2]);
  82. allPlayers[3].playerController.transform.gameObject.SetActive(false);
  83.  
  84. UIManager.Instance.gameTypeText.text = "Two Players Mode";
  85.  
  86. allPlayers[1].playerController.playerProfile.SetActive(false);
  87. allPlayers[3].playerController.playerProfile.SetActive(false);
  88.  
  89. turnFuncCo = StartCoroutine(TurnFunctionalityCo()); // Initiator Game Flow
  90. #region
  91. //else if (MainMenuManager.Instance.gameType == MainMenuManager.GameType.FourPlayer)
  92. //{
  93. // playersInCurrentGame = allPlayers;
  94. // UIManager.Instance.gameTypeText.text = "Four Players Mode";
  95. //}
  96.  
  97. //foreach (PlayerProperties pp in playersInCurrentGame)
  98. //{
  99. // pp.playerController.playerNameText.text = pp.playerName;
  100. //}
  101.  
  102. //turnFuncCo = StartCoroutine(TurnFunctionalityCo());
  103.  
  104. //if (AvatarManager.Instance != null)
  105. //{
  106. // SelectPlayerAvatarAndName();
  107. //}
  108. }
  109. //else
  110. //{
  111. // GameObject obj=PhotonNetwork.Instantiate("M_PlayerPrefab", Vector3.zero, Quaternion.identity);
  112. // //obj.GetComponent<PlayerPropertiesForMultiplayer>().gotPlayerDetails.playerId = PhotonManager.Instance.thisPlayerId;
  113.  
  114. //}
  115. #endregion
  116. if (MainMenuManager.Instance.gameType == MainMenuManager.GameType.Multi_TwoPlayer)
  117. {
  118.  
  119. view.RPC("SyncPlayerProfiles",RpcTarget.All);
  120.  
  121. }
  122. }
  123.  
  124.  
  125. IEnumerator DelaySpawn()
  126. {
  127. yield return new WaitForSeconds(1.0f);
  128. var player = PhotonNetwork.Instantiate(Entity.name, transform.position, Quaternion.identity);
  129. }
  130. [PunRPC]
  131. public void SyncPlayerProfiles()
  132. {
  133. playersInCurrentGame.Add(allPlayers[0]);
  134. allPlayers[1].playerController.transform.gameObject.SetActive(false);
  135. playersInCurrentGame.Add(allPlayers[2]);
  136. allPlayers[3].playerController.transform.gameObject.SetActive(false);
  137. //playersInCurrentGame[3].playerType.
  138. UIManager.Instance.gameTypeText.text = "Two Players Mode";
  139.  
  140. allPlayers[1].playerController.playerProfile.SetActive(false);
  141. allPlayers[3].playerController.playerProfile.SetActive(false);
  142. playersInCurrentGame[1].playerType = PlayerType.Human;
  143.  
  144.  
  145. //var player = PhotonNetwork.Instantiate(Entity.name, transform.position, Quaternion.identity);
  146.  
  147.  
  148.  
  149. //for (int i = 0; i < 4; i++)// spawn 4 ludo tokens per player
  150. //{
  151. // // the -1 is used to instantiate a prefab at element[0], it has to be set to that number because by the time this line of code runs there would have been an increment
  152.  
  153. // var player = PhotonNetwork.Instantiate(tokenPrefab[PhotonNetwork.CurrentRoom.PlayerCount - 1].name, transform.position, Quaternion.identity);
  154.  
  155. // //Debug.Log(player.GetComponent<PhotonView>().ViewID);
  156. //}
  157. if (view.IsMine)
  158. {
  159. for (int i = 0; i < 4; i++)
  160. {
  161. var player = PhotonNetwork.Instantiate(blueTokenPrefab.name, blueTokenLocations[i].transform.position, Quaternion.identity);
  162. player.transform.SetParent(blueParentLocation.transform);
  163. }
  164.  
  165. for (int i = 0; i < 4; i++)
  166. {
  167. var player1 = PhotonNetwork.Instantiate(yellowTokenPrefab.name, yellowTokenLocations[i].transform.position, Quaternion.identity);
  168. player1.transform.SetParent(yellowParentLocation.transform);
  169. }
  170. }
  171. turnFuncCo = StartCoroutine(TurnFunctionalityCo()); // Initiator Game Flow
  172. }
  173.  
  174. //void SelectPlayerAvatarAndName()
  175. //{
  176. // foreach(PlayerProperties plp in playersInCurrentGame)
  177. // {
  178. // if (plp.playerType == PlayerType.Bot)
  179. // {
  180. // plp.playerController.playerNameText.text = namesForBots[Random.Range(0, namesForBots.Length)];
  181. // plp.playerController.playerAvatarImage.sprite = AvatarManager.Instance.allAvatarSprites[Random.Range(0,
  182. // AvatarManager.Instance.allAvatarSprites.Count)];
  183. // }
  184. // else
  185. // {
  186. // string nameofMyPlayer= PlayerPrefs.GetString(ConstantsManager.playerName);
  187.  
  188. // if (nameofMyPlayer == "")
  189. // {
  190. // nameofMyPlayer = "YOU";
  191. // }
  192. // plp.playerController.playerNameText.text = nameofMyPlayer;
  193. // plp.playerController.playerAvatarImage.sprite = AvatarManager.Instance.allAvatarSprites
  194. // [PlayerPrefs.GetInt(ConstantsManager.avatarNo)];
  195. // }
  196. // }
  197. //}
  198.  
  199. public Coroutine turnFuncCo;
  200. [PunRPC]
  201. public void SyncDiceSprite()
  202. {
  203. playersInCurrentGame[currentTurnPlayerNo].playerController.diceSprite.transform.gameObject.SetActive(false);
  204. playersInCurrentGame[currentTurnPlayerNo].playerController.highlighter.SetActive(false);
  205. }
  206. IEnumerator TurnFunctionalityCo()
  207. {
  208. if (currentTurnPlayerNo >= 0 && playersInCurrentGame.Count>1)// checks to see if its not the players turn, then disable them to prepare for player 1
  209. {
  210. view.RPC("SyncDiceSprite",RpcTarget.All);
  211. }
  212.  
  213.  
  214. if (giveTurnAgain == false)
  215. {
  216. currentTurnPlayerNo++; // cycle thru players in the game
  217.  
  218. if (currentTurnPlayerNo == playersInCurrentGame.Count)
  219. {
  220. currentTurnPlayerNo = 0;// Goes back to the first player restarting the cycle
  221. }
  222. }
  223.  
  224. playersInCurrentGame[currentTurnPlayerNo].playerController.highlighter.SetActive(true);// Player 1 highlighter is active at the start but can also highlight other players
  225.  
  226. DiceManager.Instance.RollTheDice();// Rolls the Dice, Gets the Result
  227. //DiceManager.Instance.SyncRollTheDice();
  228.  
  229. yield return new WaitForSeconds(1.35f);// Gives time for the Dice to roll before proceeding
  230.  
  231. //view.RPC("SyncTurnTimer", RpcTarget.All);
  232. turnTimerCo = StartCoroutine(TurnTimer()); // Starts timing the players in their turn to either move their tokens or wait it out to skip their turn.
  233.  
  234. UIManager.Instance.turnPlayerText.text = playersInCurrentGame[currentTurnPlayerNo].playerController.playerTraits.teamColor.ToString();
  235.  
  236. if (playersInCurrentGame[currentTurnPlayerNo].playerType == PlayerType.Human)// Check if hes a human player
  237. {
  238. foreach (LudoToken lt in playersInCurrentGame[currentTurnPlayerNo].playerController.totalTokens)
  239. {
  240. if (DiceManager.Instance.diceNumber == 6)
  241. {
  242. if (lt.tokenProperties.homed == false)// if none of the tokens are recently homed, allow its collider to be selectable
  243. {
  244. lt.transform.GetComponent<BoxCollider2D>().enabled = true;
  245. }
  246. }
  247. else
  248. {
  249. if (lt.tokenProperties.freed == true && lt.tokenProperties.homed == false)// Allow tokens that are not homed and in the pathway of the board to be moved
  250. {
  251. lt.transform.GetComponent<BoxCollider2D>().enabled = true;
  252. }
  253. }
  254. }
  255. }
  256.  
  257. else ///In case of AI Player
  258. {
  259. //if (playersInCurrentGame[currentTurnPlayerNo].playerController.tokensAvailableForTurn.Count > 0)
  260. // playersInCurrentGame[currentTurnPlayerNo].playerController.tokensAvailableForTurn.Clear();
  261.  
  262. //if (DiceManager.Instance.agnosticValue == 6)// for AI
  263. //{
  264. // foreach (LudoToken lt in playersInCurrentGame[currentTurnPlayerNo].playerController.totalTokens)
  265. // {
  266. // if (lt.tokenProperties.homed == false)
  267. // {
  268. // playersInCurrentGame[currentTurnPlayerNo].playerController.tokensAvailableForTurn.Add(lt);
  269. // }
  270. // }
  271. //}
  272.  
  273. //else
  274. //{
  275. // foreach (LudoToken lt in playersInCurrentGame[currentTurnPlayerNo].playerController.totalTokens)
  276. // {
  277. // if (lt.tokenProperties.homed == false && lt.tokenProperties.freed == true)
  278. // {
  279. // playersInCurrentGame[currentTurnPlayerNo].playerController.tokensAvailableForTurn.Add(lt);
  280. // }
  281. // }
  282. //}
  283.  
  284. //if (playersInCurrentGame[currentTurnPlayerNo].playerController.tokensAvailableForTurn.Count > 0)
  285. //{
  286. // int randomTokenToMove = Random.Range(0, playersInCurrentGame[currentTurnPlayerNo].playerController.tokensAvailableForTurn.Count);
  287.  
  288. // playersInCurrentGame[currentTurnPlayerNo].playerController.tokensAvailableForTurn[randomTokenToMove].MoveToken(DiceManager.Instance.agnosticValue);//for AI
  289. //}
  290. //Debug.Log("ended");
  291. }
  292. }
  293.  
  294.  
  295. [System.Serializable]
  296. public class PlayerProperties{
  297. public string playerId;
  298. public PlayerType playerType;
  299. public PlayerController playerController;
  300. public string playerName;
  301. public bool winStatus;
  302. public bool myPlayer;
  303.  
  304. public PlayerProperties(string playerId,PlayerType playerType, PlayerController playerController,string playerName,bool winStatus,bool myPlayer)
  305. {
  306. this.playerId = playerId;
  307. this.playerType = playerType;
  308. this.playerController = playerController;
  309. this.playerName = playerName;
  310. this.winStatus = winStatus;
  311. this.myPlayer = myPlayer;
  312. }
  313. }
  314.  
  315. public enum PlayerType
  316. {
  317. Human,
  318. Bot
  319. }
  320.  
  321. //public enum GameType
  322. //{
  323. // TwoPlayer,
  324. // FourPlayer,
  325. // Multi_TwoPlayer,
  326. // Multi_FourPlayer
  327. //}
  328.  
  329. Coroutine turnTimerCo;
  330. public System.Action tokenMoveEndedAction;
  331.  
  332. //IEnumerator TurnTimer()
  333. //{
  334. // turnTimePassed = timeForEachTurn;
  335.  
  336. // if (playersInCurrentGame[currentTurnPlayerNo].playerType == PlayerType.Human)// check if the player is human
  337. // {
  338. // if (DiceManager.Instance.diceNumber == 6)
  339. // {
  340. // foreach (LudoToken lt in playersInCurrentGame[currentTurnPlayerNo].playerController.totalTokens)// allow all the tokens to be moved either from home or pathway
  341. // {
  342. // lt.tokenHighlighter.SetActive(true);
  343. // lt.GetComponent<BoxCollider2D>().enabled = true;
  344. // }
  345.  
  346. // }
  347. // else
  348. // {
  349. // foreach (LudoToken lt in playersInCurrentGame[currentTurnPlayerNo].playerController.totalTokens)// allow tokens that are freed to be moved in the pathway
  350. // {
  351. // if (lt.tokenProperties.freed == true)
  352. // {
  353. // lt.tokenHighlighter.SetActive(true);
  354. // lt.GetComponent<BoxCollider2D>().enabled = true;
  355. // }
  356. // }
  357.  
  358. // }
  359. // }
  360.  
  361.  
  362. // while (turnTimePassed > 0)
  363. // {
  364. // turnTimePassed -= Time.deltaTime;
  365. // playersInCurrentGame[currentTurnPlayerNo].playerController.loadingBarImg.fillAmount = turnTimePassed / timeForEachTurn;// Tracks the fill image bar around the player profile
  366. // yield return null;
  367. // }
  368.  
  369. // if (playersInCurrentGame[currentTurnPlayerNo].playerType == PlayerType.Human)
  370. // {
  371. // foreach (LudoToken lt in playersInCurrentGame[currentTurnPlayerNo].playerController.totalTokens)
  372. // {
  373.  
  374. // Debug.Log(lt.tokenHighlighter);
  375. // lt.tokenHighlighter.SetActive(false);
  376. // lt.GetComponent<BoxCollider2D>().enabled = false;
  377. // }
  378.  
  379.  
  380. // }
  381. // turnFuncCo = StartCoroutine(TurnFunctionalityCo()); // Initiator Game Flow
  382.  
  383. //}
  384. IEnumerator TurnTimer()
  385. {
  386. turnTimePassed = timeForEachTurn;
  387.  
  388. if (currentTurnPlayerNo >= 0 && currentTurnPlayerNo < playersInCurrentGame.Count)
  389. {
  390. if (playersInCurrentGame[currentTurnPlayerNo].playerType == PlayerType.Human)
  391. {
  392. if (DiceManager.Instance.diceNumber == 6)
  393. {
  394. foreach (LudoToken lt in playersInCurrentGame[currentTurnPlayerNo].playerController.totalTokens)
  395. {
  396. lt.tokenHighlighter.SetActive(true);
  397. lt.GetComponent<BoxCollider2D>().enabled = true;
  398. }
  399. }
  400. else
  401. {
  402. foreach (LudoToken lt in playersInCurrentGame[currentTurnPlayerNo].playerController.totalTokens)
  403. {
  404. if (lt.tokenProperties.freed == true)
  405. {
  406. lt.tokenHighlighter.SetActive(true);
  407. lt.GetComponent<BoxCollider2D>().enabled = true;
  408. }
  409. }
  410. }
  411. }
  412. }
  413.  
  414. while (turnTimePassed > 0)
  415. {
  416. turnTimePassed -= Time.deltaTime;
  417. if (currentTurnPlayerNo >= 0 && currentTurnPlayerNo < playersInCurrentGame.Count)
  418. {
  419. playersInCurrentGame[currentTurnPlayerNo].playerController.loadingBarImg.fillAmount = turnTimePassed / timeForEachTurn;
  420. }
  421. yield return null;
  422. }
  423.  
  424. if (currentTurnPlayerNo >= 0 && currentTurnPlayerNo < playersInCurrentGame.Count)
  425. {
  426. if (playersInCurrentGame[currentTurnPlayerNo].playerType == PlayerType.Human)
  427. {
  428. foreach (LudoToken lt in playersInCurrentGame[currentTurnPlayerNo].playerController.totalTokens)
  429. {
  430. lt.tokenHighlighter.SetActive(false);
  431. lt.GetComponent<BoxCollider2D>().enabled = false;
  432. }
  433. }
  434. }
  435.  
  436. turnFuncCo = StartCoroutine(TurnFunctionalityCo());
  437. }
  438.  
  439. public void StopTimerCo()
  440. {
  441. //view.RPC("SyncFillAmount",RpcTarget.All);
  442. playersInCurrentGame[currentTurnPlayerNo].playerController.loadingBarImg.fillAmount = 0;
  443. StopCoroutine(turnTimerCo);// basically stops turn functionality coroutine
  444. }
  445.  
  446. public void TokenMoveEnded()
  447. {
  448. turnFuncCo=StartCoroutine(TurnFunctionalityCo()); // Initiator Game Flow
  449. }
  450.  
  451. public void CheckWinLoseStatus(int playerIndexToRemove)
  452. {
  453. if (playersInCurrentGame[playerIndexToRemove].myPlayer == true)
  454. {
  455. ShowResult(true);
  456. }
  457. else if (playersInCurrentGame.Count <= 2)
  458. {
  459. ShowResult(false);
  460. }
  461. playersInCurrentGame.RemoveAt(playerIndexToRemove);
  462. }
  463.  
  464.  
  465. public void ShowResult(bool won)
  466. {
  467. Time.timeScale = 0;
  468.  
  469. if (won == true)
  470. {
  471. UIManager.Instance.gameOverText.text = "You Won";
  472. }
  473. else
  474. {
  475. UIManager.Instance.gameOverText.text = "You Lost";
  476. }
  477. UIManager.Instance.gameOverPanel.SetActive(true);
  478. }
  479.  
  480. public void GoToMainMenu()
  481. {
  482. Time.timeScale = 1;
  483. UnityEngine.SceneManagement.SceneManager.LoadScene(0);
  484. }
  485. }
  486.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement