-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_core.js
185 lines (142 loc) · 4.71 KB
/
game_core.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
let frame_time = 60/1000; // run the local game at 16ms / 60hz
if ('undefined' != typeof(global)) {
frametime = 45/1000;
}
class GameCore {
constructor(gameInstance) {
this.instance = gameInstance; // store instance, if any
this.server = this.instance !== undefined; // store a flag if were are the server
this.world = {
width: 5000,
height: 5000
}
this.players = {};
this._pdt = 0.0001; // The physics update delta time
this._pdte = new Date().getTime(); // physics update last delta time
this.localTime = 0.016; // the local timer
this._dt = new Date().getTime(); // The local timer delta
this._dte = new Date().getTime(); // The local last frame time
// Start a physic loop, this is separate to the rendering as this happens at a
// fixed frequency
this.createPhysicsSimulation();
// Start a fast paced timer for measuring time easier
this.createTimer();
// Client specific initialization
if (!this.server) {
// !!! create a mouse and keyboard handler
// Create the default configuration settings
this.clientCreateConfiguration():
// A list of recent server updates we interpolate across
// This is the buffer that is the driving factor for our networking
this.serverUpdates = [];
// Connect to the socket.io server
this.clientConnectToServer();
// We start pinging the server to determine latency
this.clientCreatePingTimer();
} else {
this.serverTime = 0;
this.lastState = {};
}
}
update(t) {
this.dt = this.lastFrameTime ? ((t - this.lastFrameTime)/1000.0).fixed() : 0.016;
this.lastFrameTime = t;
if (!this.server) {
this.clientUpdate();
} else {
this.serverUpdate();
}
this.updateId = window.requestAnimationFrame(this.update.bind(this))
}
// Make sure things run smoothly and notifies clients of changes on the server side
serverUpdate() {
// Update the state of our local clock to match the timer
this.serverTime = this.localTime;
this.lastState = {
positions: Object.values(this.players).forEach((p) => p.pos), // player positions
inputSeqs: Object.values(this.players).forEach((p) => p.lastInputSeq), // player last input sequences
t: this.serverTime
};
Object.values(this.players).forEach((player) => {
player.instance.emit('onserverupdate', this.lastState);
})
}
createTimer() {
setInterval(() => {
this._dt = new Date().getTime() - this._dte;
this._dte = new Date().getTime();
this.localTime += this._dt/1000.0;
}, 4);
}
createPhysicsSimulation() {
setInterval(() => {
this._pdt = (new Date().getTime() - this._pdte)/1000.0;
this._pdte = new Date().getTime();
this.updatePhysics();
}, 15);
}
updatePhysics() {
if (this.server) {
this.serverUpdatePhysics();
} else {
this.clientUpdatePhysics();
}
}
// Updated at 15ms, simulates the world state
serverUpdatePhysics() {
Object.values(this.players).forEach((player) => {
this.processInput(player);
})
}
clientUpdatePhysics() {
}
processInput(player) {
const ic = player.inputs.length;
if (ic) {
for (let i = 0; i < ic; i++) {
if (player.inputs[i].seq <= player.lastInputSeq) {
continue;
}
const input = player.inputs[i].inputs;
const c = input.length;
}
}
}
serverHandleInput(client, input, inputTime, inputSeq) {
const playerClient = this.players[client.userId];
playerClient.inputs.push({inputs: input, time: inputTime, seq: inputSeq})
}
clientHandleInput() {
const input = [];
this.clientHasInput = false;
document.addEventListener('mousemove', (e) => {
});
}
clientCreatePingTimer() {
setInterval(() => {
this.lastPingTime = new Date().getTime();
this.socket.send('p.' + (this.lastPingTime))
}, 1000);
}
clientOnConnected(data) {
this.players[data.id] = {
id: data.id,
state: 'connected',
online: 'true',
infoColor: '#cc0000'
}
}
clientOnPing(data) {
this.netPing = new Date().getTime() - parseFloat(data);
this.netLatency = this.netPing/2;
}
clientConnectToServer() {
this.socket = io();
// Handle when we connect to the server, showing state and storing id's
this.socket.on('onconnected', this.clientOnConnected.bind(this));
// Sent each tick of the server simulation. This is our authoritative update
this.socket.on('onserverupdate', this.clientOnServerUpdateReceived.bind(this));
// On message from the server, we parse the commands and send it to the handlers
this.socket.on('message', this.clientOnNetMessage.bind(this));
}
}