GameManagerScript.cs 12 KB

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