GameManagerScript.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. statsScript = GameObject.Find("StatsPanel").GetComponent<StatsScript>();
  40. scrollViewScript = GameObject.FindGameObjectWithTag("AnswerLine").transform.parent.parent.GetComponent<ScrollViewScript>();
  41. currentPlayer = Database.Instance.GetCurrentPlayer(GameId, GetGameMode());
  42. }
  43. public void StartTimer()
  44. {
  45. TimerPanel.GetComponentInChildren<TimerScript>().ResetTimer();
  46. TimerPanel.GetComponentInChildren<TimerScript>().StartTimer();
  47. }
  48. public void StopTimer() {
  49. TimerScript timerScript = TimerPanel.GetComponentInChildren<TimerScript>();
  50. timerScript.StopTimer();
  51. timerScript.ResetTimer();
  52. }
  53. void Awake()
  54. {
  55. GameId = PlayerPrefs.GetInt("GameId");
  56. GameMode = PlayerPrefs.GetString("GameMode");
  57. if (Database.Instance.GetRoundValue(GameId, GameMode) == 1) {
  58. StartCoroutine(startAnimation());
  59. }
  60. }
  61. private IEnumerator startAnimation()
  62. {
  63. NewQuestionCardController newQuestionCardController = NewQuestionCard.GetComponent<NewQuestionCardController>();
  64. newQuestionCardController.RotateCard();
  65. while (!newQuestionCardController.getRotationDone())
  66. {
  67. yield return new WaitForSeconds(0.1f);
  68. }
  69. //NewQuestionCard.transform.position = AnswerLine.transform.position;
  70. StartCoroutine(MoveCardToAnswerLine());
  71. while (newQuestionCardMoving) {
  72. yield return new WaitForSeconds(0.1f);
  73. }
  74. NewQuestionData newQuestionData = NewQuestionCard.transform.parent.GetComponent<NewQuestionsPanel>().QuestionData;
  75. GameObject go = Instantiate(AnswerLineQuestionCard, Vector3.zero, Quaternion.identity);
  76. AnswerLineQuestionCard firstAnswerLineQuestion = go.GetComponent<AnswerLineQuestionCard>();
  77. firstAnswerLineQuestion.SetAnswerText(newQuestionData.Answer);
  78. firstAnswerLineQuestion.SetQuestionText(newQuestionData.Question);
  79. firstAnswerLineQuestion.SetId(newQuestionData.Id);
  80. firstAnswerLineQuestion.SetQuestionSafe();
  81. firstAnswerLineQuestion.transform.SetParent(AnswerLine.transform);
  82. firstAnswerLineQuestion.transform.SetSiblingIndex(0);
  83. firstAnswerLineQuestion.transform.localScale = new Vector3(1,1,1);
  84. CreateLeftSpacer();
  85. CreateRightSpacer();
  86. NewQuestionCard.GetComponent<NewQuestionCardController>().GenerateNewQuestion();
  87. // Destroy(NewQuestionCard.gameObject);
  88. StopTimer();
  89. }
  90. internal void RemoveUnlockedQuestions()
  91. {
  92. if (scrollViewScript == null) {
  93. scrollViewScript = GameObject.FindGameObjectWithTag("AnswerLine").transform.parent.parent.GetComponent<ScrollViewScript>();
  94. }
  95. scrollViewScript.RemoveUnlockedQuestions();
  96. }
  97. private void CreateLeftSpacer()
  98. {
  99. GameObject newSpacerLeft = Instantiate(DropZonePrefab);
  100. newSpacerLeft.GetComponent<LayoutElement>().preferredWidth = 30f;
  101. newSpacerLeft.name = "LeftDropZone";
  102. newSpacerLeft.GetComponent<NewDropZoneScript>().LeftmostSpacer = true;
  103. newSpacerLeft.transform.SetParent(AnswerLine.transform);
  104. newSpacerLeft.transform.SetAsFirstSibling();
  105. newSpacerLeft.transform.localScale = new Vector3(1,1,1);
  106. }
  107. private void CreateRightSpacer()
  108. {
  109. GameObject newSpacerRight = Instantiate(DropZonePrefab);
  110. newSpacerRight.GetComponent<LayoutElement>().preferredWidth = 30f;
  111. newSpacerRight.name = "RightDropZone";
  112. newSpacerRight.GetComponent<NewDropZoneScript>().RightmostSpacer = true;
  113. newSpacerRight.transform.SetParent(AnswerLine.transform);
  114. newSpacerRight.transform.SetAsLastSibling();
  115. newSpacerRight.transform.localScale = new Vector3(1,1,1);
  116. }
  117. private IEnumerator MoveCardToAnswerLine()
  118. {
  119. newQuestionCardMoving = true;
  120. Vector3 alPosition = AnswerLine.transform.position;
  121. float t = 0f;
  122. while (alPosition != NewQuestionCard.transform.position)
  123. {
  124. t += Time.deltaTime / 10.0f;
  125. float step = movementSpeed * Time.deltaTime;
  126. NewQuestionCard.transform.position = Vector3.Lerp(NewQuestionCard.transform.position, AnswerLine.transform.position, t);
  127. if (Vector3.Distance(NewQuestionCard.transform.position, AnswerLine.transform.position) < 0.001f)
  128. {
  129. NewQuestionCard.transform.position = AnswerLine.transform.position;
  130. }
  131. yield return null;
  132. }
  133. newQuestionCardMoving = false;
  134. }
  135. private List<KeyValuePair<string,int>> GetPlayersForGame() {
  136. players = db.GetPlayersForGame(GameId, GameMode);
  137. return players;
  138. }
  139. public void UpdateQuestiosLost(int questionsLost, string playerName) {
  140. db.SetQuestionsLost(GameId, playerName, questionsLost);
  141. }
  142. public void UpdateQuestionsInAnswerLine(string playerName, int count) {
  143. }
  144. public List<KeyValuePair<string, int>> GetPlayers() {
  145. if (players == null) {
  146. players = GetPlayersForGame();
  147. }
  148. return players;
  149. }
  150. public void NextRound() {
  151. StopTimer();
  152. if (scrollViewScript == null) {
  153. scrollViewScript = GameObject.FindGameObjectWithTag("AnswerLine").transform.parent.parent.GetComponent<ScrollViewScript>();
  154. }
  155. scrollViewScript.SetAllQuestionsLocked(true);
  156. scrollViewScript.RemoveEverythingFromAnswerline();
  157. NewQuestionCard.GetComponent<NewQuestionCardController>().GenerateNewQuestion();
  158. NextPlayer();
  159. }
  160. private void NextPlayer() {
  161. for (int i = 0; i < players.Count; i++) {
  162. if (players[i].Key.Equals(currentPlayer, StringComparison.InvariantCultureIgnoreCase)) {
  163. if (i + 1 < players.Count) {
  164. currentPlayer = players[i + 1].Key;
  165. break;
  166. } else {
  167. currentPlayer = players[0].Key;
  168. statsScript.IncreaseRoundValue();
  169. break;
  170. }
  171. }
  172. }
  173. GenericDialog dialog = GenericDialog.Instance();
  174. dialog.SetTitle(LocalizationManager.Instance.GetText("NEXT_PLAYER_DIALOG_TITLE"));
  175. string message = LocalizationManager.Instance.GetText("NEXT_PLAYER_DIALOG_MESSAGE");
  176. dialog.SetMessage(String.Format(message, currentPlayer));
  177. dialog.SetOnAccept(LocalizationManager.Instance.GetText("OK"), () => {
  178. scrollViewScript.RemoveEverythingFromAnswerline();
  179. List<NewQuestionData> questions = Database.Instance.GetPlayerQuestions(GameId, currentPlayer, GetGameMode());
  180. scrollViewScript.SetQuestionsInAnswerLine(questions);
  181. statsScript.SetQuestionsLost(Database.Instance.GetQuestionsLost(GameId, currentPlayer, GetGameMode()));
  182. statsScript.MakeBold(currentPlayer);
  183. Database.Instance.SetCurrentPlayer(GameId, currentPlayer, GetGameMode());
  184. InformationPanelScript ips = GameObject.Find("InformationPanel").GetComponent<InformationPanelScript>();
  185. ips.SetCurrentPlayer(currentPlayer);
  186. if (GameMode.Equals("Online")) {
  187. OnlineDatabase.Instance.SendNextPlayerMessage(GameId, currentPlayer);
  188. }
  189. dialog.Hide();
  190. });
  191. dialog.SetOnDecline("", () => dialog.Hide());
  192. dialog.Show();
  193. }
  194. public string GetGameMode() {
  195. if (gameMode == null) {
  196. gameMode = PlayerPrefs.GetString("GameMode");
  197. }
  198. return gameMode;
  199. }
  200. public static string GetCurrentPlayer() {
  201. return currentPlayer;
  202. }
  203. }