GameManagerScript.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. if (GameMode.Equals("Online")) {
  118. OnlineDatabase.Instance.SendGameOverMessage(GameId, players, currentPlayer);
  119. }
  120. dialog.Hide();
  121. });
  122. dialog.SetOnDecline("", () => dialog.Hide());
  123. dialog.Show();
  124. }
  125. }
  126. internal void RemoveUnlockedQuestions()
  127. {
  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. {
  141. GameObject newSpacerLeft = Instantiate(DropZonePrefab);
  142. newSpacerLeft.GetComponent<LayoutElement>().preferredWidth = width;
  143. newSpacerLeft.name = "LeftDropZone";
  144. newSpacerLeft.GetComponent<NewDropZoneScript>().LeftmostSpacer = true;
  145. newSpacerLeft.transform.SetParent(AnswerLine.transform);
  146. newSpacerLeft.transform.SetAsFirstSibling();
  147. newSpacerLeft.transform.localScale = new Vector3(1,1,1);
  148. }
  149. private void CreateRightSpacer(float width)
  150. {
  151. GameObject newSpacerRight = Instantiate(DropZonePrefab);
  152. newSpacerRight.GetComponent<LayoutElement>().preferredWidth = width;
  153. newSpacerRight.name = "RightDropZone";
  154. newSpacerRight.GetComponent<NewDropZoneScript>().RightmostSpacer = true;
  155. newSpacerRight.transform.SetParent(AnswerLine.transform);
  156. newSpacerRight.transform.SetAsLastSibling();
  157. newSpacerRight.transform.localScale = new Vector3(1,1,1);
  158. }
  159. private IEnumerator MoveCardToAnswerLine()
  160. {
  161. newQuestionCardMoving = true;
  162. Vector3 alPosition = AnswerLine.transform.position;
  163. float t = 0f;
  164. while (alPosition != NewQuestionCard.transform.position)
  165. {
  166. t += Time.deltaTime / 10.0f;
  167. float step = movementSpeed * Time.deltaTime;
  168. NewQuestionCard.transform.position = Vector3.Lerp(NewQuestionCard.transform.position, AnswerLine.transform.position, t);
  169. if (Vector3.Distance(NewQuestionCard.transform.position, AnswerLine.transform.position) < 0.01f)
  170. {
  171. NewQuestionCard.transform.position = AnswerLine.transform.position;
  172. }
  173. yield return null;
  174. }
  175. newQuestionCardMoving = false;
  176. }
  177. private List<KeyValuePair<string,int>> GetPlayersForGame() {
  178. players = db.GetPlayersForGame(GameId, GameMode);
  179. return players;
  180. }
  181. public void UpdateQuestiosLost(int questionsLost, string playerName) {
  182. db.SetQuestionsLost(GameId, playerName, questionsLost);
  183. }
  184. public void UpdateQuestionsInAnswerLine(string playerName, int count) {
  185. }
  186. public List<KeyValuePair<string, int>> GetPlayers() {
  187. if (players == null) {
  188. players = GetPlayersForGame();
  189. }
  190. return players;
  191. }
  192. public void NextRound() {
  193. StopTimer();
  194. if (scrollViewScript == null) {
  195. scrollViewScript = GameObject.FindGameObjectWithTag("AnswerLine").transform.parent.parent.GetComponent<ScrollViewScript>();
  196. }
  197. scrollViewScript.SetAllQuestionsLocked(true);
  198. scrollViewScript.RemoveEverythingFromAnswerline();
  199. NewQuestionCard.GetComponent<NewQuestionCardController>().GenerateNewQuestion(); // Borde inte behövas
  200. NextPlayer();
  201. }
  202. private void NextPlayer() {
  203. for (int i = 0; i < players.Count; i++) {
  204. if (players[i].Key.Equals(currentPlayer, StringComparison.InvariantCultureIgnoreCase)) {
  205. if (i + 1 < players.Count) {
  206. currentPlayer = players[i + 1].Key;
  207. break;
  208. } else {
  209. currentPlayer = players[0].Key;
  210. statsScript.IncreaseRoundValue();
  211. break;
  212. }
  213. }
  214. }
  215. GenericDialog dialog = GenericDialog.Instance();
  216. dialog.SetTitle(LocalizationManager.Instance.GetText("NEXT_PLAYER_DIALOG_TITLE"));
  217. string message = LocalizationManager.Instance.GetText("NEXT_PLAYER_DIALOG_MESSAGE");
  218. dialog.SetMessage(String.Format(message, currentPlayer));
  219. dialog.SetOnAccept(LocalizationManager.Instance.GetText("OK"), () => {
  220. scrollViewScript.RemoveEverythingFromAnswerline();
  221. string gameMode = GetGameMode();
  222. List<NewQuestionData> questions = new List<NewQuestionData>();
  223. if (gameMode.Equals("Online")) {
  224. questions = Database.Instance.GetPlayerQuestions(GameId, Database.Instance.GetSignedInUser().Value, gameMode);
  225. } else {
  226. questions = Database.Instance.GetPlayerQuestions(GameId, currentPlayer, gameMode);
  227. }
  228. scrollViewScript.SetQuestionsInAnswerLine(questions);
  229. statsScript.SetQuestionsLost(Database.Instance.GetQuestionsLost(GameId, currentPlayer, GetGameMode()));
  230. statsScript.MakeBold(currentPlayer);
  231. Database.Instance.SetCurrentPlayer(GameId, currentPlayer, GetGameMode());
  232. InformationPanelScript ips = GameObject.Find("InformationPanel").GetComponent<InformationPanelScript>();
  233. if (GameMode.Equals("Online")) {
  234. OnlineDatabase.Instance.SendNextPlayerMessage(GameId, currentPlayer);
  235. }
  236. dialog.Hide();
  237. });
  238. dialog.SetOnDecline("", () => dialog.Hide());
  239. dialog.Show();
  240. }
  241. public string GetGameMode() {
  242. if (gameMode == null) {
  243. gameMode = PlayerPrefs.GetString("GameMode");
  244. }
  245. return gameMode;
  246. }
  247. public static string GetCurrentPlayer() {
  248. return currentPlayer;
  249. }
  250. public void QuitApplication() {
  251. Application.Quit();
  252. }
  253. }