GameInfoScript.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. // Start is called before the first frame update
  20. void Start() {
  21. cancelButton.onClick.AddListener(Hide);
  22. acceptButton.onClick.AddListener(OpenGame);
  23. }
  24. // Update is called once per frame
  25. void Update() {
  26. }
  27. internal void SetOnlineData(OnlineGameScript onlineGameScript) {
  28. String currentPlayer = Database.Instance.GetSignedInUser().Value;
  29. gameId = onlineGameScript.GetId();
  30. questionTimer = onlineGameScript.AnswerTimer;
  31. winNumber = onlineGameScript.WinNumber;
  32. answerTimeValueText.text = onlineGameScript.AnswerTimer.ToString();
  33. questionsToWinValueText.text = onlineGameScript.WinNumber.ToString();
  34. DaysToStartRoundValueText.text = onlineGameScript.RoundTimeLimit.ToString();
  35. foreach (KeyValuePair<string, string> player in onlineGameScript.PlayerInfos) {
  36. GameObject slp = Instantiate(playerInfoPrefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  37. OnlinePlayerInfoScript opi = slp.GetComponent<OnlinePlayerInfoScript>();
  38. opi.GameId = gameId;
  39. opi.SetPlayerName(player.Key);
  40. if (currentPlayer.Equals(player.Key, StringComparison.InvariantCultureIgnoreCase) && player.Value.Equals("WAITING")) {
  41. opi.ShowDecitionButtons();
  42. } else {
  43. opi.SetPlayerStatus(player.Value);
  44. }
  45. opi.transform.SetParent(playersPanel, false);
  46. }
  47. acceptButton.enabled = false;
  48. acceptButton.interactable = false;
  49. if (onlineGameScript.GameStatus.Equals("ACTIVE")) {
  50. acceptButton.enabled = true;
  51. acceptButton.interactable = true;
  52. }
  53. }
  54. private void OpenGame() {
  55. PlayerPrefs.SetString("GameMode", "Online");
  56. PlayerPrefs.SetInt("GameId", gameId);
  57. PlayerPrefs.SetInt("QuestionTimer", questionTimer);
  58. PlayerPrefs.SetInt("NeededForWin", winNumber);
  59. SceneManager.LoadSceneAsync("narKampen");
  60. }
  61. public void Show() {
  62. CanvasGroup cg = this.GetComponent<CanvasGroup>();
  63. cg.alpha = 1;
  64. cg.interactable = true;
  65. cg.blocksRaycasts = true;
  66. }
  67. public void Hide() {
  68. CanvasGroup cg = this.GetComponent<CanvasGroup>();
  69. cg.alpha = 0;
  70. cg.interactable = false;
  71. cg.blocksRaycasts = false;
  72. foreach (OnlinePlayerInfoScript opis in playersPanel.GetComponentsInChildren<OnlinePlayerInfoScript>()) {
  73. Destroy(opis);
  74. Destroy(opis.gameObject);
  75. }
  76. GameObject.Find("Main Panel").GetComponent<MainMenu>().UpdateOnlineGamesLists();
  77. }
  78. }