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.
130 lines
4.4 KiB
130 lines
4.4 KiB
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class MainMenuController : MonoBehaviour {
|
|
public GameObject mainMenu;
|
|
public GameObject serverMenu;
|
|
public GameObject lobbyMenu;
|
|
|
|
public InputField username;
|
|
public InputField ip;
|
|
public InputField port;
|
|
|
|
void Start() {
|
|
for(int i = 5; i < 0; i++){
|
|
Debug.Log(i);
|
|
}
|
|
// Simulate Lobbies in a Server
|
|
for (int i = 0; i < 2; i++) {
|
|
var lobby = Instantiate(serverMenu.transform.GetChild(2).GetChild(0).GetChild(0), serverMenu.transform.GetChild(2).GetChild(0));
|
|
lobby.GetComponentInChildren<Text>().text = "LBY" + Random.Range(0, 10) + Random.Range(0, 10) + Random.Range(0, 10);
|
|
lobby.gameObject.SetActive(true);
|
|
}
|
|
|
|
mainMenu.SetActive(true);
|
|
serverMenu.SetActive(false);
|
|
lobbyMenu.SetActive(false);
|
|
}
|
|
|
|
void Update() {}
|
|
|
|
public void ConnectServer() {
|
|
var conn = Client.GetConnection();
|
|
if (conn == null) {
|
|
if (ip.text != "" && port.text != "")
|
|
Client.Connect(username.text, ip.text + ":" + port.text);
|
|
else
|
|
Client.Connect(username.text);
|
|
conn = Client.GetConnection();
|
|
}
|
|
|
|
foreach (Game.Game g in conn.getGames()) {
|
|
Debug.Log(g.ToString());
|
|
}
|
|
|
|
if (conn != null) {
|
|
mainMenu.SetActive(false);
|
|
serverMenu.SetActive(true);
|
|
lobbyMenu.SetActive(false);
|
|
ReloadMenu();
|
|
}
|
|
}
|
|
|
|
public void LeaveServer() {
|
|
Client.CloseConnection();
|
|
mainMenu.SetActive(true);
|
|
serverMenu.SetActive(false);
|
|
lobbyMenu.SetActive(false);
|
|
ReloadMenu();
|
|
}
|
|
|
|
public void ConnectLobby(Text codeText) {
|
|
var code = codeText.text;
|
|
Debug.Log(code);
|
|
var conn = Client.GetConnection();
|
|
if (conn != null) {
|
|
var lobby = conn.GetLobby();
|
|
if (lobby == null) {
|
|
conn.JoinLobby(code);
|
|
mainMenu.SetActive(false);
|
|
serverMenu.SetActive(false);
|
|
lobbyMenu.SetActive(true);
|
|
lobbyMenu.transform.GetChild(1).gameObject.GetComponent<Text>().text = "Code: " + code;
|
|
ReloadMenu();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void CreateLobby() {
|
|
var conn = Client.GetConnection();
|
|
if (conn != null) {
|
|
var lobby = conn.GetLobby();
|
|
if (lobby == null) {
|
|
lobby = conn.CreateLobby();
|
|
mainMenu.SetActive(false);
|
|
serverMenu.SetActive(false);
|
|
lobbyMenu.SetActive(true);
|
|
lobbyMenu.transform.GetChild(1).gameObject.GetComponent<Text>().text = "Code: " + lobby;
|
|
ReloadMenu();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void LeaveLobby() {
|
|
var conn = Client.GetConnection();
|
|
if (conn == null) {
|
|
var lobby = conn.GetLobby();
|
|
if (lobby != null) {
|
|
//conn.LeaveLobby();
|
|
mainMenu.SetActive(false);
|
|
serverMenu.SetActive(true);
|
|
lobbyMenu.SetActive(false);
|
|
ReloadMenu();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ReloadMenu() {
|
|
if(serverMenu.activeSelf) {
|
|
for (int i = serverMenu.transform.GetChild(2).GetChild(0).childCount-1; i >= 1 ; i--) {
|
|
DestroyImmediate(serverMenu.transform.GetChild(2).GetChild(0).GetChild(i).gameObject);
|
|
}
|
|
// Simulate Lobbies in a Server
|
|
for (int i = 0; i < Random.Range(2, 15); i++) {
|
|
var lobby = Instantiate(serverMenu.transform.GetChild(2).GetChild(0).GetChild(0), serverMenu.transform.GetChild(2).GetChild(0));
|
|
lobby.GetComponentInChildren<Text>().text = "LBY" + Random.Range(0, 10) + Random.Range(0, 10) + Random.Range(0, 10);
|
|
lobby.gameObject.SetActive(true);
|
|
}
|
|
serverMenu.GetComponentInChildren<Scrollable>().Reload();
|
|
}
|
|
if (lobbyMenu.activeSelf) {
|
|
for (int i = lobbyMenu.transform.GetChild(2).GetChild(0).childCount-1; i >= 1; i--) {
|
|
DestroyImmediate(lobbyMenu.transform.GetChild(2).GetChild(0).GetChild(i).gameObject);
|
|
}
|
|
// Add Lobby Games to list when function exists
|
|
lobbyMenu.GetComponentInChildren<Scrollable>().Reload();
|
|
}
|
|
}
|
|
}
|
|
|