GameManagerScript.cs 11 KB

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