using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; public class GameManagerScript : MonoBehaviour { private int questionTimer; private string gameMode; private int neededForWin; public Database db; public OnlineDatabase odb; StatsScript statsScript; ScrollViewScript scrollViewScript; [SerializeField] GameObject NewQuestionCard; [SerializeField] GameObject AnswerLine; [SerializeField] GameObject AnswerLineQuestionCard; [SerializeField] GameObject DropZonePrefab; [SerializeField] GameObject TimerPanel; public string GameMode { get => gameMode; set => gameMode = value; } public int GameId { get; internal set; } public int QuestionTimer { get => questionTimer; set => questionTimer = value; } public int NeededForWin { get => neededForWin; set => neededForWin = value; } static string currentPlayer; private bool newQuestionCardMoving = false; [SerializeField] float movementSpeed = 1.0f; List> players; // Start is called before the first frame update void Start() { GameId = PlayerPrefs.GetInt("GameId"); GameMode = PlayerPrefs.GetString("GameMode"); QuestionTimer = PlayerPrefs.GetInt("QuestionTimer"); NeededForWin = PlayerPrefs.GetInt("NeededForWin"); if (GameMode.Equals("Local")) { db = Database.Instance; db.SetLocalOrOnline(GameMode); db.SetLastPlayedDate(GameId); } else if (GameMode.Equals("Online")) { odb = OnlineDatabase.Instance; odb.SetLastPlayedDate(GameId); } GameObject statsPanelObject = GameObject.Find("StatsPanel"); if (statsPanelObject != null) { statsScript = statsPanelObject.GetComponent(); } GameObject answerLineObject = GameObject.FindGameObjectWithTag("AnswerLine"); if (answerLineObject != null) { scrollViewScript = answerLineObject.transform.parent.parent.GetComponent(); } if (SceneManager.GetActiveScene().name.Equals("narKampen")) { currentPlayer = Database.Instance.GetCurrentPlayer(GameId, GetGameMode()); } } public void StartTimer() { TimerPanel.GetComponentInChildren().ResetTimer(); TimerPanel.GetComponentInChildren().StartTimer(); } public void StopTimer() { TimerScript timerScript = TimerPanel.GetComponentInChildren(); timerScript.StopTimer(); timerScript.ResetTimer(); } void Awake() { if (SceneManager.GetActiveScene().name.Equals("narKampen")) { GameId = PlayerPrefs.GetInt("GameId"); GameMode = PlayerPrefs.GetString("GameMode"); if (Database.Instance.GetRoundValue(GameId, GameMode) == 1) { StartCoroutine(startAnimation()); } } } private IEnumerator startAnimation() { NewQuestionCardController newQuestionCardController = NewQuestionCard.GetComponent(); newQuestionCardController.RotateCard(); while (!newQuestionCardController.getRotationDone()) { yield return new WaitForSeconds(0.1f); } //NewQuestionCard.transform.position = AnswerLine.transform.position; StartCoroutine(MoveCardToAnswerLine()); while (newQuestionCardMoving) { yield return new WaitForSeconds(0.1f); } RectTransform newQuestionRect = GameObject.Find("NewQuestion").GetComponent(); NewQuestionData newQuestionData = NewQuestionCard.transform.parent.parent.GetComponent().QuestionData; GameObject go = Instantiate(AnswerLineQuestionCard, Vector3.zero, Quaternion.identity); AnswerLineQuestionCard firstAnswerLineQuestion = go.GetComponent(); firstAnswerLineQuestion.SetAnswerText(newQuestionData.Answer); firstAnswerLineQuestion.SetQuestionText(newQuestionData.Question); firstAnswerLineQuestion.SetId(newQuestionData.Id); firstAnswerLineQuestion.SetQuestionSafe(); firstAnswerLineQuestion.GetComponent().preferredHeight = newQuestionRect.rect.height; firstAnswerLineQuestion.GetComponent().preferredWidth = newQuestionRect.rect.width; firstAnswerLineQuestion.transform.SetParent(AnswerLine.transform); firstAnswerLineQuestion.transform.SetSiblingIndex(0); firstAnswerLineQuestion.transform.localScale = new Vector3(1,1,1); CreateLeftSpacer(newQuestionRect.rect.width); CreateRightSpacer(newQuestionRect.rect.width); NewQuestionCard.GetComponent().GenerateNewQuestion(); // Destroy(NewQuestionCard.gameObject); StopTimer(); } internal void CheckWin(List answerLine) { if (answerLine.Count == NeededForWin) { GenericDialog dialog = GenericDialog.Instance(); dialog.SetTitle(LocalizationManager.Instance.GetText("WINNER_DIALOG_TITLE")); string message = LocalizationManager.Instance.GetText("WINNER_DIALOG_MESSAGE"); dialog.SetMessage(String.Format(message, NeededForWin, Database.Instance.GetQuestionsLost(GameId, currentPlayer, GameMode), Database.Instance.GetRoundValue(GameId, GameMode))); dialog.SetOnAccept(LocalizationManager.Instance.GetText("OK"), () => { Database.Instance.SetGameFinished(GameId, GameMode); scrollViewScript.SetAllQuestionsLocked(true); GameObject.Find("NewQuestion").GetComponent().BackClickable = false; if (GameMode.Equals("Online")) { OnlineDatabase.Instance.SendGameOverMessage(GameId, players, currentPlayer, Database.Instance.GetRoundValue(GameId, GameMode)); } dialog.Hide(); }); dialog.SetOnDecline("", () => dialog.Hide()); dialog.Show(); } } internal void RemoveUnlockedQuestions() { if (scrollViewScript == null) { scrollViewScript = GameObject.FindGameObjectWithTag("AnswerLine").transform.parent.parent.GetComponent(); } scrollViewScript.RemoveUnlockedQuestions(); } internal int GetUnlockedQuestionsCount() { if (scrollViewScript == null) { scrollViewScript = GameObject.FindGameObjectWithTag("AnswerLine").transform.parent.parent.GetComponent(); } return scrollViewScript.GetUnlockedQuestionCount(); } private void CreateLeftSpacer(float width) { GameObject newSpacerLeft = Instantiate(DropZonePrefab); newSpacerLeft.GetComponent().preferredWidth = width; newSpacerLeft.name = "LeftDropZone"; newSpacerLeft.GetComponent().LeftmostSpacer = true; newSpacerLeft.transform.SetParent(AnswerLine.transform); newSpacerLeft.transform.SetAsFirstSibling(); newSpacerLeft.transform.localScale = new Vector3(1,1,1); } private void CreateRightSpacer(float width) { GameObject newSpacerRight = Instantiate(DropZonePrefab); newSpacerRight.GetComponent().preferredWidth = width; newSpacerRight.name = "RightDropZone"; newSpacerRight.GetComponent().RightmostSpacer = true; newSpacerRight.transform.SetParent(AnswerLine.transform); newSpacerRight.transform.SetAsLastSibling(); newSpacerRight.transform.localScale = new Vector3(1,1,1); } private IEnumerator MoveCardToAnswerLine() { newQuestionCardMoving = true; Vector3 alPosition = AnswerLine.transform.position; float t = 0f; while (alPosition != NewQuestionCard.transform.position) { t += Time.deltaTime / 10.0f; float step = movementSpeed * Time.deltaTime; NewQuestionCard.transform.position = Vector3.Lerp(NewQuestionCard.transform.position, AnswerLine.transform.position, t); if (Vector3.Distance(NewQuestionCard.transform.position, AnswerLine.transform.position) < 0.01f) { NewQuestionCard.transform.position = AnswerLine.transform.position; } yield return null; } newQuestionCardMoving = false; } private List> GetPlayersForGame() { players = db.GetPlayersForGame(GameId, GameMode); return players; } public void UpdateQuestiosLost(int questionsLost, string playerName) { db.SetQuestionsLost(GameId, playerName, questionsLost); } public void UpdateQuestionsInAnswerLine(string playerName, int count) { } public List> GetPlayers() { if (players == null) { players = GetPlayersForGame(); } return players; } public void NextRound() { StopTimer(); if (scrollViewScript == null) { scrollViewScript = GameObject.FindGameObjectWithTag("AnswerLine").transform.parent.parent.GetComponent(); } scrollViewScript.SetAllQuestionsLocked(true); scrollViewScript.RemoveEverythingFromAnswerline(); NewQuestionCard.GetComponent().GenerateNewQuestion(); // Borde inte behövas NextPlayer(); } private void NextPlayer() { for (int i = 0; i < players.Count; i++) { if (players[i].Key.Equals(currentPlayer, StringComparison.InvariantCultureIgnoreCase)) { if (i + 1 < players.Count) { currentPlayer = players[i + 1].Key; break; } else { currentPlayer = players[0].Key; statsScript.IncreaseRoundValue(); break; } } } GenericDialog dialog = GenericDialog.Instance(); dialog.SetTitle(LocalizationManager.Instance.GetText("NEXT_PLAYER_DIALOG_TITLE")); string message = LocalizationManager.Instance.GetText("NEXT_PLAYER_DIALOG_MESSAGE"); dialog.SetMessage(String.Format(message, currentPlayer)); dialog.SetOnAccept(LocalizationManager.Instance.GetText("OK"), () => { scrollViewScript.RemoveEverythingFromAnswerline(); string gameMode = GetGameMode(); List questions = new List(); if (gameMode.Equals("Online")) { questions = Database.Instance.GetPlayerQuestions(GameId, Database.Instance.GetSignedInUser().Value, gameMode); } else { questions = Database.Instance.GetPlayerQuestions(GameId, currentPlayer, gameMode); } scrollViewScript.SetQuestionsInAnswerLine(questions); statsScript.SetQuestionsLost(Database.Instance.GetQuestionsLost(GameId, currentPlayer, GetGameMode())); statsScript.MakeBold(currentPlayer); Database.Instance.SetCurrentPlayer(GameId, currentPlayer, GetGameMode()); InformationPanelScript ips = GameObject.Find("InformationPanel").GetComponent(); if (GameMode.Equals("Online")) { OnlineDatabase.Instance.SendNextPlayerMessage(GameId, currentPlayer); } dialog.Hide(); }); dialog.SetOnDecline("", () => dialog.Hide()); dialog.Show(); } public string GetGameMode() { if (gameMode == null) { gameMode = PlayerPrefs.GetString("GameMode"); } return gameMode; } public static string GetCurrentPlayer() { return currentPlayer; } public void QuitApplication() { Application.Quit(); } }