5
5
using System . Collections . Generic ;
6
6
using System ;
7
7
using Colyseus ;
8
+ using Colyseus . Schema ;
8
9
9
10
using GameDevWare . Serialization ;
10
11
@@ -18,10 +19,9 @@ public class ColyseusClient : MonoBehaviour {
18
19
public string roomName = "demo" ;
19
20
20
21
protected Client client ;
21
- protected Room room ;
22
+ protected Room < State > room ;
22
23
23
- // map of players
24
- protected Dictionary < string , GameObject > players = new Dictionary < string , GameObject > ( ) ;
24
+ protected IndexedDictionary < Player , GameObject > players = new IndexedDictionary < Player , GameObject > ( ) ;
25
25
26
26
// Use this for initialization
27
27
IEnumerator Start ( ) {
@@ -68,7 +68,7 @@ void ConnectToServer ()
68
68
69
69
void JoinRoom ( )
70
70
{
71
- room = client . Join ( roomName , new Dictionary < string , object > ( )
71
+ room = client . Join < State > ( roomName , new Dictionary < string , object > ( )
72
72
{
73
73
{ "create" , true }
74
74
} ) ;
@@ -85,10 +85,9 @@ void JoinRoom ()
85
85
Debug . Log ( "Joined room successfully." ) ;
86
86
m_SessionIdText . text = "sessionId: " + room . SessionId ;
87
87
88
- room . Listen ( "players/:id" , OnPlayerChange , true ) ;
89
- room . Listen ( "players/:id/:axis" , OnPlayerMove ) ;
90
- room . Listen ( "messages/:number" , OnMessageAdded ) ;
91
- room . Listen ( OnChangeFallback ) ;
88
+ room . State . players . OnAdd += OnPlayerAdd ;
89
+ room . State . players . OnRemove += OnPlayerRemove ;
90
+ room . State . players . OnChange += OnPlayerMove ;
92
91
93
92
PlayerPrefs . SetString ( "sessionId" , room . SessionId ) ;
94
93
PlayerPrefs . Save ( ) ;
@@ -107,7 +106,7 @@ void ReJoinRoom ()
107
106
return ;
108
107
}
109
108
110
- room = client . ReJoin ( roomName , sessionId ) ;
109
+ room = client . ReJoin < State > ( roomName , sessionId ) ;
111
110
112
111
room . OnReadyToConnect += ( sender , e ) => {
113
112
Debug . Log ( "Ready to connect to room!" ) ;
@@ -119,10 +118,7 @@ void ReJoinRoom ()
119
118
m_SessionIdText . text = "sessionId: " + room . SessionId ;
120
119
121
120
// only register listeners after OnJoin.
122
- room . Listen ( "players/:id" , OnPlayerChange , true ) ;
123
- room . Listen ( "players/:id/:axis" , OnPlayerMove ) ;
124
- room . Listen ( "messages/:number" , OnMessageAdded ) ;
125
- room . Listen ( OnChangeFallback ) ;
121
+ // TODO: ????
126
122
} ;
127
123
128
124
room . OnStateChange += OnStateChangeHandler ;
@@ -134,7 +130,7 @@ void LeaveRoom()
134
130
room . Leave ( false ) ;
135
131
136
132
// Destroy player entities
137
- foreach ( KeyValuePair < string , GameObject > entry in players )
133
+ foreach ( KeyValuePair < Player , GameObject > entry in players )
138
134
{
139
135
Destroy ( entry . Value ) ;
140
136
}
@@ -160,71 +156,42 @@ void OnMessage (object sender, MessageEventArgs e)
160
156
Debug . Log ( message ) ;
161
157
}
162
158
163
- void OnStateChangeHandler ( object sender , StateChangeEventArgs e )
159
+ void OnStateChangeHandler ( object sender , StateChangeEventArgs < State > e )
164
160
{
165
161
// Setup room first state
166
162
Debug . Log ( "State has been updated!" ) ;
167
163
}
168
164
169
- void OnPlayerChange ( DataChange change )
165
+ void OnPlayerAdd ( object sender , KeyValueEventArgs < Player , string > item )
170
166
{
171
- Debug . Log ( "OnPlayerChange" ) ;
172
- Debug . Log ( change . operation ) ;
173
- Debug . Log ( change . path [ "id" ] ) ;
174
- // Debug.Log (change.value);
167
+ GameObject cube = GameObject . CreatePrimitive ( PrimitiveType . Cube ) ;
175
168
176
- if ( change . operation == "add" ) {
177
- IndexedDictionary < string , object > value = ( IndexedDictionary < string , object > ) change . value ;
169
+ Debug . Log ( "Player add! x => " + item . Value . x + ", y => " + item . Value . y ) ;
178
170
179
- GameObject cube = GameObject . CreatePrimitive ( PrimitiveType . Cube ) ;
171
+ cube . transform . position = new Vector3 ( item . Value . x , item . Value . y , 0 ) ;
180
172
181
- cube . transform . position = new Vector3 ( Convert . ToSingle ( value [ "x" ] ) , Convert . ToSingle ( value [ "y" ] ) , 0 ) ;
182
-
183
- // add "player" to map of players by id.
184
- players . Add ( change . path [ "id" ] , cube ) ;
185
-
186
- } else if ( change . operation == "remove" ) {
187
- // remove player from scene
188
- GameObject cube ;
189
- players . TryGetValue ( change . path [ "id" ] , out cube ) ;
190
- Destroy ( cube ) ;
191
-
192
- players . Remove ( change . path [ "id" ] ) ;
193
- }
173
+ // add "player" to map of players
174
+ players . Add ( item . Value , cube ) ;
194
175
}
195
176
196
- void OnPlayerMove ( DataChange change )
177
+ void OnPlayerRemove ( object sender , KeyValueEventArgs < Player , string > item )
197
178
{
198
- // Debug.Log ("OnPlayerMove");
199
- // Debug.Log ("playerId: " + change.path["id"] + ", Axis: " + change.path["axis"]);
200
- // Debug.Log (change.value);
201
-
202
179
GameObject cube ;
203
- players . TryGetValue ( change . path [ "id" ] , out cube ) ;
180
+ players . TryGetValue ( item . Value , out cube ) ;
181
+ Destroy ( cube ) ;
204
182
205
- cube . transform . Translate ( new Vector3 ( Convert . ToSingle ( change . value ) , 0 , 0 ) ) ;
183
+ players . Remove ( item . Value ) ;
206
184
}
207
185
208
- void OnPlayerRemoved ( DataChange change )
209
- {
210
- // Debug.Log ("OnPlayerRemoved");
211
- // Debug.Log (change.path);
212
- // Debug.Log (change.value);
213
- }
214
186
215
- void OnMessageAdded ( DataChange change )
187
+ void OnPlayerMove ( object sender , KeyValueEventArgs < Player , string > item )
216
188
{
217
- // Debug.Log ("OnMessageAdded");
218
- // Debug.Log (change.path["number"]);
219
- // Debug.Log (change.value);
220
- }
189
+ GameObject cube ;
190
+ players . TryGetValue ( item . Value , out cube ) ;
221
191
222
- void OnChangeFallback ( PatchObject change )
223
- {
224
- // Debug.Log ("OnChangeFallback");
225
- // Debug.Log (change.operation);
226
- // Debug.Log (change.path);
227
- // Debug.Log (change.value);
192
+ Debug . Log ( item . Value . x ) ;
193
+
194
+ cube . transform . Translate ( new Vector3 ( item . Value . x , item . Value . y , 0 ) ) ;
228
195
}
229
196
230
197
void OnApplicationQuit ( )
0 commit comments