GameManagerScript.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using UnityEngine.SceneManagement;
  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("GameId");
  33. GameMode = PlayerPrefs.GetString("GameMode");
  34. QuestionTimer = PlayerPrefs.GetInt("QuestionTimer");
  35. NeededForWin = PlayerPrefs.GetInt("NeededForWin");
  36. if (GameMode.Equals("Local")) {
  37. db = Database.Instance;
  38. db.SetLocalOrOnline(GameMode);
  39. db.SetLastPlayedDate(GameId);
  40. } else if (GameMode.Equals("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("narKampen")) {
  53. currentPlayer = Database.Instance.GetCurrentPlayer(GameId, GameMode);
  54. }
  55. }
  56. public void StartTimer()
  57. {
  58. TimerPanel.GetComponentInChildren<TimerScript>().ResetTimer();
  59. TimerPanel.GetComponentInChildren<TimerScript>().StartTimer();
  60. }
  61. public void StopTimer() {
  62. TimerScript timerScript = TimerPanel.GetComponentInChildren<TimerScript>();
  63. timerScript.StopTimer();
  64. timerScript.ResetTimer();
  65. }
  66. void Awake()
  67. {
  68. if (SceneManager.GetActiveScene().name.Equals("narKampen")) {
  69. GameId = PlayerPrefs.GetInt("GameId");
  70. GameMode = PlayerPrefs.GetString("GameMode");
  71. if (Database.Instance.GetRoundValue(GameId, GameMode) == 1) {
  72. StartCoroutine(startAnimation());
  73. }
  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. RectTransform newQuestionRect = GameObject.Find("NewQuestion").GetComponent<RectTransform>();
  90. NewQuestionData newQuestionData = NewQuestionCard.transform.parent.parent.GetComponent<NewQuestionsPanel>().QuestionData;
  91. GameObject go = Instantiate(AnswerLineQuestionCard, Vector3.zero, Quaternion.identity);
  92. AnswerLineQuestionCard firstAnswerLineQuestion = go.GetComponent<AnswerLineQuestionCard>();
  93. firstAnswerLineQuestion.SetAnswerText(newQuestionData.Answer);
  94. firstAnswerLineQuestion.SetQuestionText(newQuestionData.Question);
  95. firstAnswerLineQuestion.SetId(newQuestionData.Id);
  96. firstAnswerLineQuestion.SetQuestionSafe();
  97. firstAnswerLineQuestion.SetBackCategoryText(newQuestionData.CategoryName);
  98. firstAnswerLineQuestion.SetQuestionCategoryColor(newQuestionData.CategoryColor);
  99. firstAnswerLineQuestion.GetComponent<LayoutElement>().preferredHeight = newQuestionRect.rect.height;
  100. firstAnswerLineQuestion.GetComponent<LayoutElement>().preferredWidth = newQuestionRect.rect.width;
  101. firstAnswerLineQuestion.transform.SetParent(AnswerLine.transform);
  102. firstAnswerLineQuestion.transform.SetSiblingIndex(0);
  103. firstAnswerLineQuestion.transform.localScale = new Vector3(1,1,1);
  104. CreateLeftSpacer(newQuestionRect.rect.width);
  105. CreateRightSpacer(newQuestionRect.rect.width);
  106. NewQuestionCard.GetComponent<NewQuestionCardController>().GenerateNewQuestion();
  107. // Destroy(NewQuestionCard.gameObject);
  108. StopTimer();
  109. }
  110. internal void CheckWin(List<QuestionCard> answerLine)
  111. {
  112. if (answerLine.Count == NeededForWin) {
  113. GenericDialog dialog = GenericDialog.Instance();
  114. dialog.SetTitle(LocalizationManager.Instance.GetText("WINNER_DIALOG_TITLE"));
  115. string message = LocalizationManager.Instance.GetText("WINNER_DIALOG_MESSAGE");
  116. dialog.SetMessage(String.Format(message, NeededForWin,
  117. Database.Instance.GetQuestionsLost(GameId, currentPlayer, GameMode),
  118. Database.Instance.GetRoundValue(GameId, GameMode)));
  119. dialog.SetOnAccept(LocalizationManager.Instance.GetText("OK"), () => {
  120. GameStatus = "FINISHED";
  121. Database.Instance.SetGameFinished(GameId, GameMode);
  122. scrollViewScript.SetAllQuestionsLocked(true);
  123. if (GameMode.Equals("Online")) {
  124. OnlineDatabase.Instance.SendGameOverMessage(GameId, players, currentPlayer, Database.Instance.GetRoundValue(GameId, GameMode));
  125. }
  126. dialog.Hide();
  127. });
  128. dialog.SetOnDecline("", () => dialog.Hide());
  129. dialog.Show();
  130. }
  131. }
  132. internal void RemoveUnlockedQuestions()
  133. {
  134. if (scrollViewScript == null) {
  135. scrollViewScript = GameObject.FindGameObjectWithTag("AnswerLine").transform.parent.parent.GetComponent<ScrollViewScript>();
  136. }
  137. scrollViewScript.RemoveUnlockedQuestions();
  138. }
  139. internal int GetUnlockedQuestionsCount() {
  140. if (scrollViewScript == null) {
  141. scrollViewScript = GameObject.FindGameObjectWithTag("AnswerLine").transform.parent.parent.GetComponent<ScrollViewScript>();
  142. }
  143. return scrollViewScript.GetUnlockedQuestionCount();
  144. }
  145. private void CreateLeftSpacer(float width)
  146. {
  147. GameObject newSpacerLeft = Instantiate(DropZonePrefab);
  148. newSpacerLeft.GetComponent<LayoutElement>().preferredWidth = width;
  149. newSpacerLeft.name = "LeftDropZone";
  150. newSpacerLeft.GetComponent<NewDropZoneScript>().LeftmostSpacer = true;
  151. newSpacerLeft.transform.SetParent(AnswerLine.transform);
  152. newSpacerLeft.transform.SetAsFirstSibling();
  153. newSpacerLeft.transform.localScale = new Vector3(1,1,1);
  154. }
  155. private void CreateRightSpacer(float width)
  156. {
  157. GameObject newSpacerRight = Instantiate(DropZonePrefab);
  158. newSpacerRight.GetComponent<LayoutElement>().preferredWidth = width;
  159. newSpacerRight.name = "RightDropZone";
  160. newSpacerRight.GetComponent<NewDropZoneScript>().RightmostSpacer = true;
  161. newSpacerRight.transform.SetParent(AnswerLine.transform);
  162. newSpacerRight.transform.SetAsLastSibling();
  163. newSpacerRight.transform.localScale = new Vector3(1,1,1);
  164. }
  165. private IEnumerator MoveCardToAnswerLine()
  166. {
  167. newQuestionCardMoving = true;
  168. Vector3 alPosition = AnswerLine.transform.position;
  169. float t = 0f;
  170. while (alPosition != NewQuestionCard.transform.position)
  171. {
  172. t += Time.deltaTime / 10.0f;
  173. float step = movementSpeed * Time.deltaTime;
  174. NewQuestionCard.transform.position = Vector3.Lerp(NewQuestionCard.transform.position, AnswerLine.transform.position, t);
  175. if (Vector3.Distance(NewQuestionCard.transform.position, AnswerLine.transform.position) < 0.01f)
  176. {
  177. NewQuestionCard.transform.position = AnswerLine.transform.position;
  178. }
  179. yield return null;
  180. }
  181. newQuestionCardMoving = false;
  182. }
  183. private List<KeyValuePair<string,int>> GetPlayersForGame() {
  184. players = db.GetPlayersForGame(GameId, GameMode);
  185. return players;
  186. }
  187. public void UpdateQuestiosLost(int questionsLost, string playerName) {
  188. db.SetQuestionsLost(GameId, playerName, questionsLost);
  189. }
  190. public void UpdateQuestionsInAnswerLine(string playerName, int count) {
  191. }
  192. public List<KeyValuePair<string, int>> GetPlayers() {
  193. if (players == null) {
  194. players = GetPlayersForGame();
  195. }
  196. return players;
  197. }
  198. public void NextRound() {
  199. StopTimer();
  200. if (scrollViewScript == null) {
  201. scrollViewScript = GameObject.FindGameObjectWithTag("AnswerLine").transform.parent.parent.GetComponent<ScrollViewScript>();
  202. }
  203. scrollViewScript.SetAllQuestionsLocked(true);
  204. scrollViewScript.RemoveEverythingFromAnswerline();
  205. NextPlayer();
  206. }
  207. private void NextPlayer() {
  208. Database.Instance.IncreasePlayerRound(GameId, currentPlayer);
  209. if (GameMode.Equals("Online")) {
  210. statsScript.IncreaseRoundValue();
  211. }
  212. for (int i = 0; i < players.Count; i++) {
  213. if (players[i].Key.Equals(currentPlayer, StringComparison.InvariantCultureIgnoreCase)) {
  214. if (i + 1 < players.Count) {
  215. currentPlayer = players[i + 1].Key;
  216. break;
  217. } else {
  218. currentPlayer = players[0].Key;
  219. if (GameMode.Equals("Local")) {
  220. statsScript.IncreaseRoundValue();
  221. }
  222. break;
  223. }
  224. }
  225. }
  226. GenericDialog dialog = GenericDialog.Instance();
  227. dialog.SetTitle(LocalizationManager.Instance.GetText("NEXT_PLAYER_DIALOG_TITLE"));
  228. string message = LocalizationManager.Instance.GetText("NEXT_PLAYER_DIALOG_MESSAGE");
  229. dialog.SetMessage(String.Format(message, currentPlayer));
  230. dialog.SetOnAccept(LocalizationManager.Instance.GetText("OK"), () => {
  231. scrollViewScript.RemoveEverythingFromAnswerline();
  232. List<NewQuestionData> questions = new List<NewQuestionData>();
  233. if (GameMode.Equals("Online")) {
  234. questions = Database.Instance.GetPlayerQuestions(GameId, Database.Instance.GetSignedInUser().Value, gameMode);
  235. } else {
  236. questions = Database.Instance.GetPlayerQuestions(GameId, currentPlayer, gameMode);
  237. }
  238. scrollViewScript.SetQuestionsInAnswerLine(questions);
  239. statsScript.SetQuestionsLost(Database.Instance.GetQuestionsLost(GameId, currentPlayer, GameMode));
  240. statsScript.MakeBold(currentPlayer);
  241. Database.Instance.SetCurrentPlayer(GameId, currentPlayer, GameMode);
  242. InformationPanelScript ips = GameObject.Find("InformationPanel").GetComponent<InformationPanelScript>();
  243. if (GameMode.Equals("Online")) {
  244. OnlineDatabase.Instance.SendNextPlayerMessage(GameId, currentPlayer);
  245. }
  246. dialog.Hide();
  247. });
  248. dialog.SetOnDecline("", () => dialog.Hide());
  249. dialog.Show();
  250. }
  251. public static string GetCurrentPlayer() {
  252. return currentPlayer;
  253. }
  254. public void QuitApplication() {
  255. Application.Quit();
  256. }
  257. }