-
Notifications
You must be signed in to change notification settings - Fork 1
/
socketClient.js
executable file
·204 lines (176 loc) · 4.95 KB
/
socketClient.js
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* Socket client main loop
*
*
*/
var socketHost = location.href
, socket = io.connect(socketHost)
, clientLoopMilliseconds = 50
, socketObjects = []
, playerObjects = []
, tickedSocketObjects = []
, numberOfSavedTicks = 6
, enemyZ = 100
, hostObject = {}
, host = 0;
/**
* Socket listeners (These relay messages back to server.js)
*
*
*/
// Assigns unique clientid to browser
socket.on('assignid', function (id) {
clientid = id;
});
// Updates ALL socketObjects with each server loop iteration
socket.on('updateobjects', function (objects) {
if (objects.length > 0 && clientid != host) {
socketObjects = objects;
// Push to a timestamped array of socketObject snapshots
if (tickedSocketObjects.length <= numberOfSavedTicks) {
tickedSocketObjects.push(socketObjects);
}
else {
tickedSocketObjects.push(socketObjects);
tickedSocketObjects.splice(0,1);
}
}
});
// Updates player objects fo all clients
socket.on('updateplayerobjects', function (objects) {
if (objects.length > 0) {
playerObjects = objects;
}
});
// Updates Host object directly
socket.on('updatehostobject', function (serverObject) {
if (clientid == host) {
// !!! Game Engine Logic !!! //
var hostObject = me.game.editEntityByGUID(serverObject.GUID, serverObject);
}
});
// Adds object passed from slave to host client
socket.on('addhostobject', function(serverObject) {
if (clientid == host) {
// Is the object already in the game?
var foundHostObj = false;
if (me.game.getEntityByGUID(serverObject.GUID) !== null) {
foundHostObj = true;
}
// If the object has not been added to non-host client
if (foundHostObj == false && serverObject.settings.entityName) {
addObjectToClient(serverObject);
}
}
});
/**
* Helper functions
*
*
*/
// Passing input from browser to socket.io server
var socketResponse = function(type, data) {
var socket = io.connect(socketHost);
socket.emit(type, data);
};
// Updates some socketObjects keys
var updateSocketObjectKeys = function(object, i) {
for (key in object) {
if (socketObjects[i][key]) {
socketObjects[i][key] = object[key];
}
}
return true;
};
// Updates some Entity keys
var updateHostEntityKeys = function(serverObject) {
for (key in serverObject) {
if (hostObject) {
hostObject[key] = serverObject[key];
}
}
return true;
};
// Find socket object index
var checkGUID = function(thisEntity) {
for(var i = 0; i < socketObjects.length; i++) {
if (socketObjects[i].GUID == thisEntity.GUID) {
return i;
}
}
return false;
};
var addObjectToClient = function(serverObject) {
var hostedEntity = new window[serverObject.settings.entityName](
serverObject.pos.x,
serverObject.pos.y,
{
image: serverObject.settings.image,
spritewidth: serverObject.settings.spritewidth,
spriteheight: serverObject.settings.spriteheight,
GUID: serverObject.GUID
}
);
me.game.add(hostedEntity, enemyZ++);
me.game.sort();
};
/**
* Main client loop for updating engine objects from array of 'socketObjects'
*
*
*/
var initClientLoop = function(){
// Useful for logging out objects for debug
// setInterval(function(){
// console.log(socketObjects);
// },2500);
setInterval(function(){
// Player Loop for all clients
var foundPlayerObj = [];
if (playerObjects.length > 0) {
for(var i = 0; i < playerObjects.length; i++) {
foundPlayerObj[i] = false;
if (me.game.getEntityByGUID(playerObjects[i].GUID) !== null) {
foundPlayerObj[i] = true;
}
if (foundPlayerObj[i] == false && playerObjects[i].settings.entityName && !playerObjects[i].dead) {
// !!! Game Engine Logic !!! //
addObjectToClient(playerObjects[i]);
}
}
}
// If Host, update all socketObjects to server
var foundGameObj = [];
if (socketObjects.length > 0 && clientid == host) {
socketResponse('updateobjects', socketObjects);
// Check if we should remove from host
for(var i = 0; i < socketObjects.length; i++) {
// Checking if we should remove object
if (socketObjects[i].dead == true) {
socketObjects.splice(i,1);
}
}
}
// If Slave, read objects from Servexr and add to game if not added yet
if (socketObjects && clientid != host && me.state.ready) {
for(var i = 0; i < socketObjects.length; i++) {
foundGameObj[i] = false;
// !!! Game Engine Logic !!! //
if (me.game.getEntityByGUID(socketObjects[i].GUID) !== null) {
foundGameObj[i] = true;
}
// If the object has not been added to non-host client
if (foundGameObj[i] == false && socketObjects[i].settings.entityName && !socketObjects[i].dead) {
addObjectToClient(socketObjects[i]);
}
}
}
},
clientLoopMilliseconds); // Set milliseconds to update from socketObjects
};
/**
* Kick things off
*
*
*/
initClientLoop();