GameManagerScript.cs 12 KB

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