GameInfoScript.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.SceneManagement;
  6. using UnityEngine.UI;
  7. public class GameInfoScript : MonoBehaviour {
  8. public Button cancelButton;
  9. public Button acceptButton;
  10. public GameObject playerInfoPrefab;
  11. public Transform playersPanel;
  12. public Transform gameSettingsPanel;
  13. private int gameId;
  14. private int questionTimer;
  15. private int winNumber;
  16. [SerializeField] Text answerTimeValueText;
  17. [SerializeField] Text questionsToWinValueText;
  18. [SerializeField] Text DaysToStartRoundValueText;
  19. [SerializeField] Text GameIdText;
  20. // Start is called before the first frame update
  21. void Start() {
  22. cancelButton.onClick.AddListener(Hide);
  23. acceptButton.onClick.AddListener(OpenGame);
  24. }
  25. // Update is called once per frame
  26. void Update() {
  27. }
  28. internal void SetOnlineData(OnlineGameScript onlineGameScript) {
  29. String currentPlayer = Database.Instance.GetSignedInUser().Value;
  30. gameId = onlineGameScript.GetId();
  31. questionTimer = onlineGameScript.AnswerTimer;
  32. winNumber = onlineGameScript.WinNumber;
  33. answerTimeValueText.text = onlineGameScript.AnswerTimer.ToString();
  34. questionsToWinValueText.text = onlineGameScript.WinNumber.ToString();
  35. DaysToStartRoundValueText.text = onlineGameScript.RoundTimeLimit.ToString();
  36. foreach (KeyValuePair<string, string> player in onlineGameScript.PlayerInfos) {
  37. GameObject slp = Instantiate(playerInfoPrefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  38. OnlinePlayerInfoScript opi = slp.GetComponent<OnlinePlayerInfoScript>();
  39. opi.GameId = gameId;
  40. opi.SetPlayerName(player.Key);
  41. if (currentPlayer.Equals(player.Key, StringComparison.InvariantCultureIgnoreCase) && player.Value.Equals("WAITING")) {
  42. opi.ShowDecitionButtons();
  43. } else {
  44. opi.SetPlayerStatus(player.Value);
  45. }
  46. opi.transform.SetParent(playersPanel, false);
  47. }
  48. GameIdText.text = onlineGameScript.GetId().ToString();
  49. acceptButton.enabled = false;
  50. acceptButton.interactable = false;
  51. if (onlineGameScript.GameStatus.Equals("ACTIVE")) {
  52. acceptButton.enabled = true;
  53. acceptButton.interactable = true;
  54. }
  55. }
  56. private void OpenGame() {
  57. PlayerPrefs.SetString("GameMode", "Online");
  58. PlayerPrefs.SetInt("GameId", gameId);
  59. PlayerPrefs.SetInt("QuestionTimer", questionTimer);
  60. PlayerPrefs.SetInt("NeededForWin", winNumber);
  61. SceneManager.LoadSceneAsync("narKampen");
  62. }
  63. public void Show() {
  64. CanvasGroup cg = this.GetComponent<CanvasGroup>();
  65. cg.alpha = 1;
  66. cg.interactable = true;
  67. cg.blocksRaycasts = true;
  68. }
  69. public void Hide() {
  70. CanvasGroup cg = this.GetComponent<CanvasGroup>();
  71. cg.alpha = 0;
  72. cg.interactable = false;
  73. cg.blocksRaycasts = false;
  74. foreach (OnlinePlayerInfoScript opis in playersPanel.GetComponentsInChildren<OnlinePlayerInfoScript>()) {
  75. Destroy(opis);
  76. Destroy(opis.gameObject);
  77. }
  78. GameObject.Find("Main Panel").GetComponent<MainMenu>().UpdateOnlineGamesLists();
  79. }
  80. }