GameManagerScript.cs 12 KB

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