LobbyUIManager.cs 3.53 KB
Newer Older
1
2
3
4
5
using UnityEngine;
using System.Collections;

using UnityEngine.Networking;
using UnityEngine.Networking.Match;
6
using UnityEngine.SceneManagement;
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using UnityEngine.UI;

public class LobbyUIManager : MonoBehaviour {

	private static LobbyUIManager _instance;

	private LobbyManager _lobbyManager;

	[SerializeField]
	private InputField _portField;

	[SerializeField]
	private InputField _ipField;

	[SerializeField]
22
	private OfflineDisplay _offline;
23
24

	[SerializeField]
25
	private OnlineDisplay _online;
26
27
28
29
30
31
32

	[SerializeField]
	private LobbyPlayerUI _playerInfo;

	[SerializeField]
	private RectTransform _lobby;

33
34
35
36
37
38
39
40
41
	[SerializeField]
	private Button _hostButton;

	[SerializeField]
	private Button _joinButton;

	[SerializeField]
	private Image _connectingDisplay;

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
	private int _playerCount = 0;

	private LobbyManager LobbyMngr {
		get { return _lobbyManager ?? (_lobbyManager = LobbyManager.GetInstance ()); }
	}

	public int PlayerCount { get { return _playerCount; } }

	private void Awake () {
		if (null != _instance && _instance != this) {
			Destroy (gameObject);
			return;
		}

		_instance = this;
	}

	void Start () {
		_portField.text = "" + LobbyMngr.networkPort;
		LobbyMngr.onConnected += OnConnected;
		LobbyMngr.onDisconnected += OnDisconnected;
63
64

		SetOnlineOffline (false);
65
66
67
68
		PlayerConnectedEvent.handler += OnPlayerConnectedEvent;
	}

	void OnDestroy () {
69
70
		LobbyMngr.onConnected -= OnConnected;
		LobbyMngr.onDisconnected -= OnDisconnected;
71
72
73
		PlayerConnectedEvent.handler -= OnPlayerConnectedEvent;
	}

74
75
76
77
78
	private void SetOnlineOffline (bool online) {
		_offline.gameObject.SetActive (!online);
		_online.gameObject.SetActive (online);
	}

79
	private void OnConnected () {
80
81
82
83
84
85
86
87
88
89
		if (null != _connectingDisplay)
			_connectingDisplay.gameObject.SetActive (false);

		SetOnlineOffline (true);
	}

	public void OnReturnToLobby () {
		_offline = OfflineDisplay.GetInstance ();
		_online = OnlineDisplay.GetInstance ();
		OnConnected ();
90
91
92
	}

	private void OnDisconnected () {
93
94
		Destroy (LobbyManager.GetInstance ().gameObject);
		SceneManager.LoadScene ("Lobby");
95
96
97
	}

	public void StartHost () {
98
99
		_hostButton.interactable = false;
		_joinButton.interactable = false;
100
		LobbyMngr.networkPort = int.Parse (_portField.text);
101
102
103
104
105
106
107
108
109
		if (null == LobbyMngr.StartHost ()) {
			// Error occured when starting server!

			// Display error perhaps...
			OnDisconnected ();
			return;
		}

		_connectingDisplay.gameObject.SetActive (true);
110
111
112
113
114
115
116
	}

	public void StartDedicated () {
		LobbyMngr.networkPort = int.Parse (_portField.text);
		LobbyMngr.StartServer ();
	}

117
118
119
120
	public void OnConnectError (NetworkMessage message) {
		OnDisconnected ();
	}

121
	public void ConnectToServer () {
122
123
		_hostButton.interactable = false;
		_joinButton.interactable = false;
124
125
		//LobbyMngr.networkPort = int.Parse (_portField.text);
		LobbyMngr.networkAddress = _ipField.text;
126
127
128
		NetworkClient client = LobbyMngr.StartClient ();
		client.RegisterHandler (MsgType.Error | MsgType.Disconnect, OnConnectError);
		_connectingDisplay.gameObject.SetActive (true);
129
130
131
132
133
134
135
136
137
	}

	public void Disconnect () {
		LobbyMngr.StopHost ();
	}

	public void OnPlayerConnectedEvent (PlayerConnectedEvent e) {
		LobbyPlayerUI obj = Instantiate (_playerInfo);
		obj.transform.SetParent (_lobby, false);
138
139
140
		Vector3 pos = obj.transform.localPosition;
		pos.y -= 50 * PlayerCount;
		obj.transform.localPosition = pos;
141
142
143
144
145
146
147
148
149
		++_playerCount;

		obj.SetLobbyPlayer (e.Player);
	}

	public static LobbyUIManager GetInstance () {
		return _instance;
	}
}