Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Events;
- using TMPro;
- using Photon.Realtime;
- using Photon.Pun;
- using ExitGames.Client.Photon;
- public class GameManager : MonoBehaviour
- {
- public static GameManager Instance;
- PhotonView view;
- public int currentTurnPlayerNo = -1; // Determine player order
- # region Tokens
- [SerializeField] GameObject[] tokenPrefab = new GameObject[4];
- [SerializeField] GameObject[] blueTokenLocations = new GameObject[4];
- [SerializeField] GameObject blueParentLocation;
- [SerializeField] GameObject blueTokenPrefab;
- [Space(50)]
- [SerializeField] GameObject[] yellowTokenLocations = new GameObject[4];
- [SerializeField] GameObject yellowParentLocation;
- [SerializeField] GameObject yellowTokenPrefab;
- [Space(50)]
- # endregion
- [Header("Dont mind this variable, its a dummy variable to experiment on something")]
- [SerializeField] GameObject Entity;
- [SerializeField] GameObject Entity1;
- public List<PlayerController> allPlayerControllersInTurnOrder;
- //public GameType gameType;
- public List<PlayerProperties> allPlayers;
- public List<PlayerProperties> playersInCurrentGame;
- public bool giveTurnAgain;
- [Space(5)]
- [Header("Paths for Players")]
- [Space(10)]
- public List<PathBlockProperties> wholeRegularPath;
- public List<PathBlockProperties> bluePawnPath;
- public List<PathBlockProperties> redPawnPath;
- public List<PathBlockProperties> yellowPawnPath;
- public List<PathBlockProperties> greenPawnPath;
- [Header("Timer Related")]
- public float timeForEachTurn;
- public float turnTimePassed;
- //public bool tokenMoved;
- [Header("Board Highlight")]
- public float highlighterAlpha = 0.55f;
- public float highlighterAnimationSpeed = 1f;
- //[ContextMenu("Multiplayer")]
- //public GameObject photonViewPrefab;
- public string[] namesForBots;
- private void Awake()
- {
- Instance = this;
- view = GetComponent<PhotonView>();
- }
- private void Start()
- {
- namesForBots = new string[] { "Avery", "River","Parker","Quinn","Ryan","Dylan","Noah","Ezra","Emery","Kai","Nova","August",
- "Ellis","Everest","Onyx","Ridley","Tatum","Royal","Wren","Kris","Rene"};
- // = MainMenuManager.Instance.gameType;
- if (MainMenuManager.Instance.gameType == MainMenuManager.GameType.TwoPlayer)
- {
- playersInCurrentGame.Add(allPlayers[0]);
- allPlayers[1].playerController.transform.gameObject.SetActive(false);
- playersInCurrentGame.Add(allPlayers[2]);
- allPlayers[3].playerController.transform.gameObject.SetActive(false);
- UIManager.Instance.gameTypeText.text = "Two Players Mode";
- allPlayers[1].playerController.playerProfile.SetActive(false);
- allPlayers[3].playerController.playerProfile.SetActive(false);
- turnFuncCo = StartCoroutine(TurnFunctionalityCo()); // Initiator Game Flow
- #region
- //else if (MainMenuManager.Instance.gameType == MainMenuManager.GameType.FourPlayer)
- //{
- // playersInCurrentGame = allPlayers;
- // UIManager.Instance.gameTypeText.text = "Four Players Mode";
- //}
- //foreach (PlayerProperties pp in playersInCurrentGame)
- //{
- // pp.playerController.playerNameText.text = pp.playerName;
- //}
- //turnFuncCo = StartCoroutine(TurnFunctionalityCo());
- //if (AvatarManager.Instance != null)
- //{
- // SelectPlayerAvatarAndName();
- //}
- }
- //else
- //{
- // GameObject obj=PhotonNetwork.Instantiate("M_PlayerPrefab", Vector3.zero, Quaternion.identity);
- // //obj.GetComponent<PlayerPropertiesForMultiplayer>().gotPlayerDetails.playerId = PhotonManager.Instance.thisPlayerId;
- //}
- #endregion
- if (MainMenuManager.Instance.gameType == MainMenuManager.GameType.Multi_TwoPlayer)
- {
- view.RPC("SyncPlayerProfiles",RpcTarget.All);
- }
- }
- IEnumerator DelaySpawn()
- {
- yield return new WaitForSeconds(1.0f);
- var player = PhotonNetwork.Instantiate(Entity.name, transform.position, Quaternion.identity);
- }
- [PunRPC]
- public void SyncPlayerProfiles()
- {
- playersInCurrentGame.Add(allPlayers[0]);
- allPlayers[1].playerController.transform.gameObject.SetActive(false);
- playersInCurrentGame.Add(allPlayers[2]);
- allPlayers[3].playerController.transform.gameObject.SetActive(false);
- //playersInCurrentGame[3].playerType.
- UIManager.Instance.gameTypeText.text = "Two Players Mode";
- allPlayers[1].playerController.playerProfile.SetActive(false);
- allPlayers[3].playerController.playerProfile.SetActive(false);
- playersInCurrentGame[1].playerType = PlayerType.Human;
- //var player = PhotonNetwork.Instantiate(Entity.name, transform.position, Quaternion.identity);
- //for (int i = 0; i < 4; i++)// spawn 4 ludo tokens per player
- //{
- // // 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
- // var player = PhotonNetwork.Instantiate(tokenPrefab[PhotonNetwork.CurrentRoom.PlayerCount - 1].name, transform.position, Quaternion.identity);
- // //Debug.Log(player.GetComponent<PhotonView>().ViewID);
- //}
- if (view.IsMine)
- {
- for (int i = 0; i < 4; i++)
- {
- var player = PhotonNetwork.Instantiate(blueTokenPrefab.name, blueTokenLocations[i].transform.position, Quaternion.identity);
- player.transform.SetParent(blueParentLocation.transform);
- }
- for (int i = 0; i < 4; i++)
- {
- var player1 = PhotonNetwork.Instantiate(yellowTokenPrefab.name, yellowTokenLocations[i].transform.position, Quaternion.identity);
- player1.transform.SetParent(yellowParentLocation.transform);
- }
- }
- turnFuncCo = StartCoroutine(TurnFunctionalityCo()); // Initiator Game Flow
- }
- //void SelectPlayerAvatarAndName()
- //{
- // foreach(PlayerProperties plp in playersInCurrentGame)
- // {
- // if (plp.playerType == PlayerType.Bot)
- // {
- // plp.playerController.playerNameText.text = namesForBots[Random.Range(0, namesForBots.Length)];
- // plp.playerController.playerAvatarImage.sprite = AvatarManager.Instance.allAvatarSprites[Random.Range(0,
- // AvatarManager.Instance.allAvatarSprites.Count)];
- // }
- // else
- // {
- // string nameofMyPlayer= PlayerPrefs.GetString(ConstantsManager.playerName);
- // if (nameofMyPlayer == "")
- // {
- // nameofMyPlayer = "YOU";
- // }
- // plp.playerController.playerNameText.text = nameofMyPlayer;
- // plp.playerController.playerAvatarImage.sprite = AvatarManager.Instance.allAvatarSprites
- // [PlayerPrefs.GetInt(ConstantsManager.avatarNo)];
- // }
- // }
- //}
- public Coroutine turnFuncCo;
- [PunRPC]
- public void SyncDiceSprite()
- {
- playersInCurrentGame[currentTurnPlayerNo].playerController.diceSprite.transform.gameObject.SetActive(false);
- playersInCurrentGame[currentTurnPlayerNo].playerController.highlighter.SetActive(false);
- }
- IEnumerator TurnFunctionalityCo()
- {
- if (currentTurnPlayerNo >= 0 && playersInCurrentGame.Count>1)// checks to see if its not the players turn, then disable them to prepare for player 1
- {
- view.RPC("SyncDiceSprite",RpcTarget.All);
- }
- if (giveTurnAgain == false)
- {
- currentTurnPlayerNo++; // cycle thru players in the game
- if (currentTurnPlayerNo == playersInCurrentGame.Count)
- {
- currentTurnPlayerNo = 0;// Goes back to the first player restarting the cycle
- }
- }
- playersInCurrentGame[currentTurnPlayerNo].playerController.highlighter.SetActive(true);// Player 1 highlighter is active at the start but can also highlight other players
- DiceManager.Instance.RollTheDice();// Rolls the Dice, Gets the Result
- //DiceManager.Instance.SyncRollTheDice();
- yield return new WaitForSeconds(1.35f);// Gives time for the Dice to roll before proceeding
- //view.RPC("SyncTurnTimer", RpcTarget.All);
- turnTimerCo = StartCoroutine(TurnTimer()); // Starts timing the players in their turn to either move their tokens or wait it out to skip their turn.
- UIManager.Instance.turnPlayerText.text = playersInCurrentGame[currentTurnPlayerNo].playerController.playerTraits.teamColor.ToString();
- if (playersInCurrentGame[currentTurnPlayerNo].playerType == PlayerType.Human)// Check if hes a human player
- {
- foreach (LudoToken lt in playersInCurrentGame[currentTurnPlayerNo].playerController.totalTokens)
- {
- if (DiceManager.Instance.diceNumber == 6)
- {
- if (lt.tokenProperties.homed == false)// if none of the tokens are recently homed, allow its collider to be selectable
- {
- lt.transform.GetComponent<BoxCollider2D>().enabled = true;
- }
- }
- else
- {
- 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
- {
- lt.transform.GetComponent<BoxCollider2D>().enabled = true;
- }
- }
- }
- }
- else ///In case of AI Player
- {
- //if (playersInCurrentGame[currentTurnPlayerNo].playerController.tokensAvailableForTurn.Count > 0)
- // playersInCurrentGame[currentTurnPlayerNo].playerController.tokensAvailableForTurn.Clear();
- //if (DiceManager.Instance.agnosticValue == 6)// for AI
- //{
- // foreach (LudoToken lt in playersInCurrentGame[currentTurnPlayerNo].playerController.totalTokens)
- // {
- // if (lt.tokenProperties.homed == false)
- // {
- // playersInCurrentGame[currentTurnPlayerNo].playerController.tokensAvailableForTurn.Add(lt);
- // }
- // }
- //}
- //else
- //{
- // foreach (LudoToken lt in playersInCurrentGame[currentTurnPlayerNo].playerController.totalTokens)
- // {
- // if (lt.tokenProperties.homed == false && lt.tokenProperties.freed == true)
- // {
- // playersInCurrentGame[currentTurnPlayerNo].playerController.tokensAvailableForTurn.Add(lt);
- // }
- // }
- //}
- //if (playersInCurrentGame[currentTurnPlayerNo].playerController.tokensAvailableForTurn.Count > 0)
- //{
- // int randomTokenToMove = Random.Range(0, playersInCurrentGame[currentTurnPlayerNo].playerController.tokensAvailableForTurn.Count);
- // playersInCurrentGame[currentTurnPlayerNo].playerController.tokensAvailableForTurn[randomTokenToMove].MoveToken(DiceManager.Instance.agnosticValue);//for AI
- //}
- //Debug.Log("ended");
- }
- }
- [System.Serializable]
- public class PlayerProperties{
- public string playerId;
- public PlayerType playerType;
- public PlayerController playerController;
- public string playerName;
- public bool winStatus;
- public bool myPlayer;
- public PlayerProperties(string playerId,PlayerType playerType, PlayerController playerController,string playerName,bool winStatus,bool myPlayer)
- {
- this.playerId = playerId;
- this.playerType = playerType;
- this.playerController = playerController;
- this.playerName = playerName;
- this.winStatus = winStatus;
- this.myPlayer = myPlayer;
- }
- }
- public enum PlayerType
- {
- Human,
- Bot
- }
- //public enum GameType
- //{
- // TwoPlayer,
- // FourPlayer,
- // Multi_TwoPlayer,
- // Multi_FourPlayer
- //}
- Coroutine turnTimerCo;
- public System.Action tokenMoveEndedAction;
- //IEnumerator TurnTimer()
- //{
- // turnTimePassed = timeForEachTurn;
- // if (playersInCurrentGame[currentTurnPlayerNo].playerType == PlayerType.Human)// check if the player is human
- // {
- // if (DiceManager.Instance.diceNumber == 6)
- // {
- // foreach (LudoToken lt in playersInCurrentGame[currentTurnPlayerNo].playerController.totalTokens)// allow all the tokens to be moved either from home or pathway
- // {
- // lt.tokenHighlighter.SetActive(true);
- // lt.GetComponent<BoxCollider2D>().enabled = true;
- // }
- // }
- // else
- // {
- // foreach (LudoToken lt in playersInCurrentGame[currentTurnPlayerNo].playerController.totalTokens)// allow tokens that are freed to be moved in the pathway
- // {
- // if (lt.tokenProperties.freed == true)
- // {
- // lt.tokenHighlighter.SetActive(true);
- // lt.GetComponent<BoxCollider2D>().enabled = true;
- // }
- // }
- // }
- // }
- // while (turnTimePassed > 0)
- // {
- // turnTimePassed -= Time.deltaTime;
- // playersInCurrentGame[currentTurnPlayerNo].playerController.loadingBarImg.fillAmount = turnTimePassed / timeForEachTurn;// Tracks the fill image bar around the player profile
- // yield return null;
- // }
- // if (playersInCurrentGame[currentTurnPlayerNo].playerType == PlayerType.Human)
- // {
- // foreach (LudoToken lt in playersInCurrentGame[currentTurnPlayerNo].playerController.totalTokens)
- // {
- // Debug.Log(lt.tokenHighlighter);
- // lt.tokenHighlighter.SetActive(false);
- // lt.GetComponent<BoxCollider2D>().enabled = false;
- // }
- // }
- // turnFuncCo = StartCoroutine(TurnFunctionalityCo()); // Initiator Game Flow
- //}
- IEnumerator TurnTimer()
- {
- turnTimePassed = timeForEachTurn;
- if (currentTurnPlayerNo >= 0 && currentTurnPlayerNo < playersInCurrentGame.Count)
- {
- if (playersInCurrentGame[currentTurnPlayerNo].playerType == PlayerType.Human)
- {
- if (DiceManager.Instance.diceNumber == 6)
- {
- foreach (LudoToken lt in playersInCurrentGame[currentTurnPlayerNo].playerController.totalTokens)
- {
- lt.tokenHighlighter.SetActive(true);
- lt.GetComponent<BoxCollider2D>().enabled = true;
- }
- }
- else
- {
- foreach (LudoToken lt in playersInCurrentGame[currentTurnPlayerNo].playerController.totalTokens)
- {
- if (lt.tokenProperties.freed == true)
- {
- lt.tokenHighlighter.SetActive(true);
- lt.GetComponent<BoxCollider2D>().enabled = true;
- }
- }
- }
- }
- }
- while (turnTimePassed > 0)
- {
- turnTimePassed -= Time.deltaTime;
- if (currentTurnPlayerNo >= 0 && currentTurnPlayerNo < playersInCurrentGame.Count)
- {
- playersInCurrentGame[currentTurnPlayerNo].playerController.loadingBarImg.fillAmount = turnTimePassed / timeForEachTurn;
- }
- yield return null;
- }
- if (currentTurnPlayerNo >= 0 && currentTurnPlayerNo < playersInCurrentGame.Count)
- {
- if (playersInCurrentGame[currentTurnPlayerNo].playerType == PlayerType.Human)
- {
- foreach (LudoToken lt in playersInCurrentGame[currentTurnPlayerNo].playerController.totalTokens)
- {
- lt.tokenHighlighter.SetActive(false);
- lt.GetComponent<BoxCollider2D>().enabled = false;
- }
- }
- }
- turnFuncCo = StartCoroutine(TurnFunctionalityCo());
- }
- public void StopTimerCo()
- {
- //view.RPC("SyncFillAmount",RpcTarget.All);
- playersInCurrentGame[currentTurnPlayerNo].playerController.loadingBarImg.fillAmount = 0;
- StopCoroutine(turnTimerCo);// basically stops turn functionality coroutine
- }
- public void TokenMoveEnded()
- {
- turnFuncCo=StartCoroutine(TurnFunctionalityCo()); // Initiator Game Flow
- }
- public void CheckWinLoseStatus(int playerIndexToRemove)
- {
- if (playersInCurrentGame[playerIndexToRemove].myPlayer == true)
- {
- ShowResult(true);
- }
- else if (playersInCurrentGame.Count <= 2)
- {
- ShowResult(false);
- }
- playersInCurrentGame.RemoveAt(playerIndexToRemove);
- }
- public void ShowResult(bool won)
- {
- Time.timeScale = 0;
- if (won == true)
- {
- UIManager.Instance.gameOverText.text = "You Won";
- }
- else
- {
- UIManager.Instance.gameOverText.text = "You Lost";
- }
- UIManager.Instance.gameOverPanel.SetActive(true);
- }
- public void GoToMainMenu()
- {
- Time.timeScale = 1;
- UnityEngine.SceneManagement.SceneManager.LoadScene(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement