-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathBoltInit.cs
More file actions
188 lines (159 loc) · 4.04 KB
/
Copy pathBoltInit.cs
File metadata and controls
188 lines (159 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using UnityEngine;
using System;
using UdpKit;
using UnityEngine.SceneManagement;
using UdpKit.Platform.Photon;
using Photon.Bolt;
using Photon.Bolt.Matchmaking;
using Photon.Bolt.Utils;
namespace Bolt.Samples
{
public class BoltInit : GlobalEventListener
{
private enum State
{
SelectMode,
SelectMap,
SelectRoom,
StartServer,
StartClient,
Started,
}
private Rect labelRoom = new Rect(0, 0, 140, 75);
private GUIStyle labelRoomStyle;
private State currentState;
private string map;
void Awake()
{
Application.targetFrameRate = 60;
labelRoomStyle = new GUIStyle()
{
fontSize = 20,
fontStyle = FontStyle.Bold,
normal =
{
textColor = Color.white
}
};
}
void OnGUI()
{
Rect tex = new Rect(10, 10, 140, 75);
Rect area = new Rect(10, 90, Screen.width - 20, Screen.height - 100);
GUI.Box(tex, Resources.Load("BoltLogo") as Texture2D);
if (BoltNetwork.IsRunning)
{
tex = new Rect(160, 10, 140, 75);
GUILayout.BeginArea(tex);
if (GUILayout.Button("Shutdown", GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true)))
{
BoltNetwork.Shutdown();
}
GUILayout.EndArea();
}
GUILayout.BeginArea(area);
switch (currentState)
{
case State.SelectMode: State_SelectMode(); break;
case State.SelectMap: State_SelectMap(); break;
case State.SelectRoom: State_SelectRoom(); break;
case State.StartClient: State_StartClient(); break;
case State.StartServer: State_StartServer(); break;
}
GUILayout.EndArea();
}
void State_SelectRoom()
{
GUI.Label(labelRoom, "Looking for rooms:", labelRoomStyle);
if (BoltNetwork.SessionList.Count > 0)
{
GUILayout.BeginVertical();
GUILayout.Space(30);
foreach (var session in BoltNetwork.SessionList)
{
var photonSession = session.Value as PhotonSession;
if (photonSession.Source == UdpSessionSource.Photon)
{
var matchName = photonSession.HostName;
var label = string.Format("Join: {0} | {1}/{2}", matchName, photonSession.ConnectionsCurrent, photonSession.ConnectionsMax);
if (ExpandButton(label))
{
BoltMatchmaking.JoinSession(photonSession);
currentState = State.Started;
}
}
}
GUILayout.EndVertical();
}
}
void State_SelectMode()
{
if (ExpandButton("Server"))
{
currentState = State.SelectMap;
}
if (ExpandButton("Client"))
{
currentState = State.StartClient;
}
}
void State_SelectMap()
{
GUILayout.BeginVertical();
foreach (string value in BoltScenes.AllScenes)
{
if (SceneManager.GetActiveScene().name != value)
{
if (ExpandButton(value))
{
map = value;
currentState = State.StartServer;
}
}
}
GUILayout.EndVertical();
}
void State_StartServer()
{
BoltLauncher.StartServer();
currentState = State.Started;
}
void State_StartClient()
{
BoltLauncher.StartClient();
currentState = State.SelectRoom;
}
public override void BoltStartBegin()
{
// Register any Protocol Token that are you using
BoltNetwork.RegisterTokenClass<PhotonRoomProperties>();
}
public override void BoltStartDone()
{
if (BoltNetwork.IsServer)
{
var id = Guid.NewGuid().ToString().Split('-')[0];
var matchName = string.Format("{0} - {1}", id, map);
BoltMatchmaking.CreateSession(
sessionID: matchName,
sceneToLoad: map
);
}
}
public override void BoltShutdownBegin(AddCallback registerDoneCallback, UdpConnectionDisconnectReason disconnectReason)
{
registerDoneCallback(() =>
{
currentState = State.SelectMode;
});
}
bool ExpandButton(string text)
{
return GUILayout.Button(text, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
}
public override void SessionListUpdated(Map<Guid, UdpSession> sessionList)
{
BoltLog.Info("New session list");
}
}
}