You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.5 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5. using Networking;
  6. using Networking.Server;
  7. public class LobbyUIController : MonoBehaviour
  8. {
  9. [SerializeField]
  10. private ClientList Clients;
  11. [SerializeField]
  12. private string SceneToStart;
  13. [Header("UI Elements")]
  14. [SerializeField]
  15. private TextMeshProUGUI IPText;
  16. [SerializeField]
  17. private Transform Content;
  18. [SerializeField]
  19. private GameObject ClientTag;
  20. [SerializeField]
  21. private ServerObject Server;
  22. // Start is called before the first frame update
  23. void Start()
  24. {
  25. IPText.text = "Lobby - " + Networking.Utility.GetIP(Networking.Utility.ADDRESSFAM.IPv4);
  26. }
  27. private void OnEnable()
  28. {
  29. Clients.OnClientsChange += DisplayClients;
  30. DisplayClients(Clients.ConnectedClients);
  31. if (Server.server != null)
  32. Server.RegisterHandler(LoginProtocols.StartGame, RecieveStartMsg);
  33. else
  34. Debug.LogWarning("Server not started yet");
  35. }
  36. private void OnDisable()
  37. {
  38. Clients.OnClientsChange -= DisplayClients;
  39. if (Server.server != null)
  40. Server.UnregisterHandler(LoginProtocols.StartGame);
  41. }
  42. private void DisplayClients(List<ClientData> data)
  43. {
  44. //Debug.Log("Updating Connected Clients");
  45. foreach (Transform child in Content)
  46. if (child.gameObject != ClientTag)
  47. Destroy(child.gameObject);
  48. for (int i = 0; i < Clients.ConnectedClients.Count; i++)
  49. {
  50. ClientData client = Clients.ConnectedClients[i];
  51. GameObject clientObject = Instantiate(ClientTag, Content);
  52. TextMeshProUGUI clientText = clientObject.GetComponent<TextMeshProUGUI>();
  53. clientText.text = client.Name + " the " + client.characterAnimal;
  54. clientText.color = client.Color;
  55. clientObject.SetActive(true);
  56. }
  57. }
  58. public void OnClick_Back()
  59. {
  60. UnityEngine.SceneManagement.SceneManager.LoadScene("MainMenu Server");
  61. }
  62. public void OnClick_StartGame()
  63. {
  64. #if UNITY_EDITOR
  65. UnityEngine.SceneManagement.SceneManager.LoadScene(SceneToStart);
  66. #else
  67. if (Clients.ConnectedClients.Count >= 2 && Clients.ConnectedClients.Count <= 4){
  68. UnityEngine.SceneManagement.SceneManager.LoadScene(SceneToStart);
  69. }
  70. #endif
  71. }
  72. public void RecieveStartMsg(UnityEngine.Networking.NetworkMessage msg)
  73. {
  74. OnClick_StartGame();
  75. }
  76. }