GameManagerScript.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Firebase;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. public class GameManagerScript : MonoBehaviour {
  8. private int playerCount;
  9. private int questionTimer;
  10. private string gameMode;
  11. public Database db;
  12. public OnlineDatabase odb;
  13. StatsScript statsScript;
  14. ScrollViewScript scrollViewScript;
  15. [SerializeField] GameObject NewQuestionCard;
  16. [SerializeField] GameObject AnswerLine;
  17. [SerializeField] GameObject AnswerLineQuestionCard;
  18. [SerializeField] GameObject DropZonePrefab;
  19. [SerializeField] GameObject TimerPanel;
  20. public string GameMode { get => gameMode; set => gameMode = value; }
  21. public int GameId { get; internal set; }
  22. public int QuestionTimer { get => questionTimer; set => questionTimer = value; }
  23. static string currentPlayer;
  24. private bool newQuestionCardMoving = false;
  25. [SerializeField] float movementSpeed = 1.0f;
  26. List<KeyValuePair<string, int>> players;
  27. private FirebaseApp app;
  28. // Start is called before the first frame update
  29. void Start() {
  30. Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => {
  31. var dependencyStatus = task.Result;
  32. if (dependencyStatus == Firebase.DependencyStatus.Available) {
  33. // Create and hold a reference to your FirebaseApp,
  34. // where app is a Firebase.FirebaseApp property of your application class.
  35. app = Firebase.FirebaseApp.DefaultInstance;
  36. // Set a flag here to indicate whether Firebase is ready to use by your app.
  37. } else {
  38. UnityEngine.Debug.LogError(System.String.Format(
  39. "Could not resolve all Firebase dependencies: {0}", dependencyStatus));
  40. // Firebase Unity SDK is not safe to use here.
  41. }
  42. });
  43. GameId = PlayerPrefs.GetInt("GameId");
  44. GameMode = PlayerPrefs.GetString("GameMode");
  45. QuestionTimer = PlayerPrefs.GetInt("QuestionTimer");
  46. if (GameMode.Equals("Local")) {
  47. db = Database.Instance;
  48. db.SetLocalOrOnline(GameMode);
  49. db.SetLastPlayedDate(GameId);
  50. } else if (GameMode.Equals("Online")) {
  51. odb = OnlineDatabase.Instance;
  52. odb.SetLastPlayedDate(GameId);
  53. }
  54. statsScript = GameObject.Find("StatsPanel").GetComponent<StatsScript>();
  55. scrollViewScript = GameObject.FindGameObjectWithTag("AnswerLine").transform.parent.parent.GetComponent<ScrollViewScript>();
  56. currentPlayer = Database.Instance.GetCurrentPlayer(GameId, GetGameMode());
  57. }
  58. public void StartTimer()
  59. {
  60. TimerPanel.GetComponentInChildren<TimerScript>().ResetTimer();
  61. TimerPanel.GetComponentInChildren<TimerScript>().StartTimer();
  62. }
  63. public void StopTimer() {
  64. TimerScript timerScript = TimerPanel.GetComponentInChildren<TimerScript>();
  65. timerScript.StopTimer();
  66. timerScript.ResetTimer();
  67. }
  68. void Awake()
  69. {
  70. GameId = PlayerPrefs.GetInt("GameId");
  71. GameMode = PlayerPrefs.GetString("GameMode");
  72. if (Database.Instance.GetRoundValue(GameId, GameMode) == 1) {
  73. StartCoroutine(startAnimation());
  74. }
  75. }
  76. private IEnumerator startAnimation()
  77. {
  78. NewQuestionCardController newQuestionCardController = NewQuestionCard.GetComponent<NewQuestionCardController>();
  79. newQuestionCardController.RotateCard();
  80. while (!newQuestionCardController.getRotationDone())
  81. {
  82. yield return new WaitForSeconds(0.1f);
  83. }
  84. //NewQuestionCard.transform.position = AnswerLine.transform.position;
  85. StartCoroutine(MoveCardToAnswerLine());
  86. while (newQuestionCardMoving) {
  87. yield return new WaitForSeconds(0.1f);
  88. }
  89. NewQuestionData newQuestionData = NewQuestionCard.transform.parent.GetComponent<NewQuestionsPanel>().QuestionData;
  90. GameObject go = Instantiate(AnswerLineQuestionCard, Vector3.zero, Quaternion.identity);
  91. AnswerLineQuestionCard firstAnswerLineQuestion = go.GetComponent<AnswerLineQuestionCard>();
  92. firstAnswerLineQuestion.SetAnswerText(newQuestionData.Answer);
  93. firstAnswerLineQuestion.SetQuestionText(newQuestionData.Question);
  94. firstAnswerLineQuestion.SetId(newQuestionData.Id);
  95. firstAnswerLineQuestion.SetQuestionSafe();
  96. firstAnswerLineQuestion.transform.SetParent(AnswerLine.transform);
  97. firstAnswerLineQuestion.transform.SetSiblingIndex(0);
  98. firstAnswerLineQuestion.transform.localScale = new Vector3(1,1,1);
  99. CreateLeftSpacer();
  100. CreateRightSpacer();
  101. NewQuestionCard.GetComponent<NewQuestionCardController>().GenerateNewQuestion();
  102. // Destroy(NewQuestionCard.gameObject);
  103. StopTimer();
  104. }
  105. internal void RemoveUnlockedQuestions()
  106. {
  107. if (scrollViewScript == null) {
  108. scrollViewScript = GameObject.FindGameObjectWithTag("AnswerLine").transform.parent.parent.GetComponent<ScrollViewScript>();
  109. }
  110. scrollViewScript.RemoveUnlockedQuestions();
  111. }
  112. private void CreateLeftSpacer()
  113. {
  114. GameObject newSpacerLeft = Instantiate(DropZonePrefab);
  115. newSpacerLeft.GetComponent<LayoutElement>().preferredWidth = 30f;
  116. newSpacerLeft.name = "LeftDropZone";
  117. newSpacerLeft.GetComponent<NewDropZoneScript>().LeftmostSpacer = true;
  118. newSpacerLeft.transform.SetParent(AnswerLine.transform);
  119. newSpacerLeft.transform.SetAsFirstSibling();
  120. newSpacerLeft.transform.localScale = new Vector3(1,1,1);
  121. }
  122. private void CreateRightSpacer()
  123. {
  124. GameObject newSpacerRight = Instantiate(DropZonePrefab);
  125. newSpacerRight.GetComponent<LayoutElement>().preferredWidth = 30f;
  126. newSpacerRight.name = "RightDropZone";
  127. newSpacerRight.GetComponent<NewDropZoneScript>().RightmostSpacer = true;
  128. newSpacerRight.transform.SetParent(AnswerLine.transform);
  129. newSpacerRight.transform.SetAsLastSibling();
  130. newSpacerRight.transform.localScale = new Vector3(1,1,1);
  131. }
  132. private IEnumerator MoveCardToAnswerLine()
  133. {
  134. newQuestionCardMoving = true;
  135. Vector3 alPosition = AnswerLine.transform.position;
  136. float t = 0f;
  137. while (alPosition != NewQuestionCard.transform.position)
  138. {
  139. t += Time.deltaTime / 10.0f;
  140. float step = movementSpeed * Time.deltaTime;
  141. NewQuestionCard.transform.position = Vector3.Lerp(NewQuestionCard.transform.position, AnswerLine.transform.position, t);
  142. if (Vector3.Distance(NewQuestionCard.transform.position, AnswerLine.transform.position) < 0.001f)
  143. {
  144. NewQuestionCard.transform.position = AnswerLine.transform.position;
  145. }
  146. yield return null;
  147. }
  148. newQuestionCardMoving = false;
  149. }
  150. private List<KeyValuePair<string,int>> GetPlayersForGame() {
  151. players = db.GetPlayersForGame(GameId, GameMode);
  152. return players;
  153. }
  154. public void UpdateQuestiosLost(int questionsLost, string playerName) {
  155. db.SetQuestionsLost(GameId, playerName, questionsLost);
  156. }
  157. public void UpdateQuestionsInAnswerLine(string playerName, int count) {
  158. }
  159. public List<KeyValuePair<string, int>> GetPlayers() {
  160. if (players == null) {
  161. players = GetPlayersForGame();
  162. }
  163. return players;
  164. }
  165. public void NextRound() {
  166. StopTimer();
  167. if (scrollViewScript == null) {
  168. scrollViewScript = GameObject.FindGameObjectWithTag("AnswerLine").transform.parent.parent.GetComponent<ScrollViewScript>();
  169. }
  170. scrollViewScript.SetAllQuestionsLocked(true);
  171. scrollViewScript.RemoveEverythingFromAnswerline();
  172. NewQuestionCard.GetComponent<NewQuestionCardController>().GenerateNewQuestion();
  173. NextPlayer();
  174. }
  175. private void NextPlayer() {
  176. for (int i = 0; i < players.Count; i++) {
  177. if (players[i].Key.Equals(currentPlayer, StringComparison.InvariantCultureIgnoreCase)) {
  178. if (i + 1 < players.Count) {
  179. currentPlayer = players[i + 1].Key;
  180. break;
  181. } else {
  182. currentPlayer = players[0].Key;
  183. statsScript.IncreaseRoundValue();
  184. break;
  185. }
  186. }
  187. }
  188. GenericDialog dialog = GenericDialog.Instance();
  189. dialog.SetTitle(LocalizationManager.Instance.GetText("NEXT_PLAYER_DIALOG_TITLE"));
  190. string message = LocalizationManager.Instance.GetText("NEXT_PLAYER_DIALOG_MESSAGE");
  191. dialog.SetMessage(String.Format(message, currentPlayer));
  192. dialog.SetOnAccept(LocalizationManager.Instance.GetText("OK"), () => {
  193. scrollViewScript.RemoveEverythingFromAnswerline();
  194. List<NewQuestionData> questions = Database.Instance.GetPlayerQuestions(GameId, currentPlayer, GetGameMode());
  195. scrollViewScript.SetQuestionsInAnswerLine(questions);
  196. statsScript.SetQuestionsLost(Database.Instance.GetQuestionsLost(GameId, currentPlayer, GetGameMode()));
  197. statsScript.MakeBold(currentPlayer);
  198. Database.Instance.SetCurrentPlayer(GameId, currentPlayer, GetGameMode());
  199. InformationPanelScript ips = GameObject.Find("InformationPanel").GetComponent<InformationPanelScript>();
  200. ips.SetCurrentPlayer(currentPlayer);
  201. //ResetNewQuestionPosition();
  202. dialog.Hide();
  203. });
  204. dialog.SetOnDecline("", () => dialog.Hide());
  205. dialog.Show();
  206. }
  207. public string GetGameMode() {
  208. if (gameMode == null) {
  209. gameMode = PlayerPrefs.GetString("GameMode");
  210. }
  211. return gameMode;
  212. }
  213. public static string GetCurrentPlayer() {
  214. return currentPlayer;
  215. }
  216. }