GameInfoScript.cs 3.1 KB

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