using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class GameManagerScript : MonoBehaviour { private int playerCount; private int questionTimer; private string gameMode; 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; } 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"); if (GameMode.Equals("Local")) { db = Database.Instance; db.SetLocalOrOnline(GameMode); db.SetLastPlayedDate(GameId); } else if (GameMode.Equals("Online")) { odb = OnlineDatabase.Instance; odb.SetLastPlayedDate(GameId); } statsScript = GameObject.Find("StatsPanel").GetComponent(); scrollViewScript = GameObject.FindGameObjectWithTag("AnswerLine").transform.parent.parent.GetComponent(); 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() { 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); } NewQuestionData newQuestionData = NewQuestionCard.transform.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.transform.SetParent(AnswerLine.transform); firstAnswerLineQuestion.transform.SetSiblingIndex(0); firstAnswerLineQuestion.transform.localScale = new Vector3(1,1,1); CreateLeftSpacer(); CreateRightSpacer(); NewQuestionCard.GetComponent().GenerateNewQuestion(); // Destroy(NewQuestionCard.gameObject); } private void CreateLeftSpacer() { GameObject newSpacerLeft = Instantiate(DropZonePrefab); newSpacerLeft.GetComponent().preferredWidth = 30f; 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() { GameObject newSpacerRight = Instantiate(DropZonePrefab); newSpacerRight.GetComponent().preferredWidth = 30f; 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.001f) { 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(); 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(); List questions = Database.Instance.GetPlayerQuestions(GameId, currentPlayer, GetGameMode()); 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(); ips.SetCurrentPlayer(currentPlayer); //ResetNewQuestionPosition(); 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; } }