GameManagerScript.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. }
  89. private void CreateLeftSpacer()
  90. {
  91. GameObject newSpacerLeft = Instantiate(DropZonePrefab);
  92. newSpacerLeft.GetComponent<LayoutElement>().preferredWidth = 30f;
  93. newSpacerLeft.name = "LeftDropZone";
  94. newSpacerLeft.GetComponent<NewDropZoneScript>().LeftmostSpacer = true;
  95. newSpacerLeft.transform.SetParent(AnswerLine.transform);
  96. newSpacerLeft.transform.SetAsFirstSibling();
  97. newSpacerLeft.transform.localScale = new Vector3(1,1,1);
  98. }
  99. private void CreateRightSpacer()
  100. {
  101. GameObject newSpacerRight = Instantiate(DropZonePrefab);
  102. newSpacerRight.GetComponent<LayoutElement>().preferredWidth = 30f;
  103. newSpacerRight.name = "RightDropZone";
  104. newSpacerRight.GetComponent<NewDropZoneScript>().RightmostSpacer = true;
  105. newSpacerRight.transform.SetParent(AnswerLine.transform);
  106. newSpacerRight.transform.SetAsLastSibling();
  107. newSpacerRight.transform.localScale = new Vector3(1,1,1);
  108. }
  109. private IEnumerator MoveCardToAnswerLine()
  110. {
  111. newQuestionCardMoving = true;
  112. Vector3 alPosition = AnswerLine.transform.position;
  113. float t = 0f;
  114. while (alPosition != NewQuestionCard.transform.position)
  115. {
  116. t += Time.deltaTime / 10.0f;
  117. float step = movementSpeed * Time.deltaTime;
  118. NewQuestionCard.transform.position = Vector3.Lerp(NewQuestionCard.transform.position, AnswerLine.transform.position, t);
  119. if (Vector3.Distance(NewQuestionCard.transform.position, AnswerLine.transform.position) < 0.001f)
  120. {
  121. NewQuestionCard.transform.position = AnswerLine.transform.position;
  122. }
  123. yield return null;
  124. }
  125. newQuestionCardMoving = false;
  126. }
  127. private List<KeyValuePair<string,int>> GetPlayersForGame() {
  128. players = db.GetPlayersForGame(GameId, GameMode);
  129. return players;
  130. }
  131. public void UpdateQuestiosLost(int questionsLost, string playerName) {
  132. db.SetQuestionsLost(GameId, playerName, questionsLost);
  133. }
  134. public void UpdateQuestionsInAnswerLine(string playerName, int count) {
  135. }
  136. public List<KeyValuePair<string, int>> GetPlayers() {
  137. if (players == null) {
  138. players = GetPlayersForGame();
  139. }
  140. return players;
  141. }
  142. public void NextRound() {
  143. StopTimer();
  144. if (scrollViewScript == null) {
  145. scrollViewScript = GameObject.FindGameObjectWithTag("AnswerLine").transform.parent.parent.GetComponent<ScrollViewScript>();
  146. }
  147. scrollViewScript.SetAllQuestionsLocked(true);
  148. scrollViewScript.RemoveEverythingFromAnswerline();
  149. NewQuestionCard.GetComponent<NewQuestionCardController>().GenerateNewQuestion();
  150. NextPlayer();
  151. }
  152. private void NextPlayer() {
  153. for (int i = 0; i < players.Count; i++) {
  154. if (players[i].Key.Equals(currentPlayer, StringComparison.InvariantCultureIgnoreCase)) {
  155. if (i + 1 < players.Count) {
  156. currentPlayer = players[i + 1].Key;
  157. break;
  158. } else {
  159. currentPlayer = players[0].Key;
  160. statsScript.IncreaseRoundValue();
  161. break;
  162. }
  163. }
  164. }
  165. GenericDialog dialog = GenericDialog.Instance();
  166. dialog.SetTitle(LocalizationManager.Instance.GetText("NEXT_PLAYER_DIALOG_TITLE"));
  167. string message = LocalizationManager.Instance.GetText("NEXT_PLAYER_DIALOG_MESSAGE");
  168. dialog.SetMessage(String.Format(message, currentPlayer));
  169. dialog.SetOnAccept(LocalizationManager.Instance.GetText("OK"), () => {
  170. scrollViewScript.RemoveEverythingFromAnswerline();
  171. List<NewQuestionData> questions = Database.Instance.GetPlayerQuestions(GameId, currentPlayer, GetGameMode());
  172. scrollViewScript.SetQuestionsInAnswerLine(questions);
  173. statsScript.SetQuestionsLost(Database.Instance.GetQuestionsLost(GameId, currentPlayer, GetGameMode()));
  174. statsScript.MakeBold(currentPlayer);
  175. Database.Instance.SetCurrentPlayer(GameId, currentPlayer, GetGameMode());
  176. InformationPanelScript ips = GameObject.Find("InformationPanel").GetComponent<InformationPanelScript>();
  177. ips.SetCurrentPlayer(currentPlayer);
  178. //ResetNewQuestionPosition();
  179. dialog.Hide();
  180. });
  181. dialog.SetOnDecline("", () => dialog.Hide());
  182. dialog.Show();
  183. }
  184. public string GetGameMode() {
  185. if (gameMode == null) {
  186. gameMode = PlayerPrefs.GetString("GameMode");
  187. }
  188. return gameMode;
  189. }
  190. public static string GetCurrentPlayer() {
  191. return currentPlayer;
  192. }
  193. }