GameManagerScript.cs 9.3 KB

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