GameInfoScript.cs 2.6 KB

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