GameInfoScript.cs 2.5 KB

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