GameManagerScript.cs 9.6 KB

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