forked from cvan/tanx-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
239 lines (194 loc) · 9.13 KB
/
client.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
pc.script.create('client', function (context) {
// If `?gamepad=2` is passed we will use `navigator.getGamepads[1]`.
// Otherwise, we will assume `navigator.getGamepads[0]`, if connected.
var uri = new pc.URI(window.location.href);
var query = uri.getQuery();
var gamepadNum = query.gamepad;
var Client = function (entity) {
this.entity = entity;
this.id = null;
this.movement = [ 0, 0 ];
context.keyboard = new pc.input.Keyboard(document.body);
document.body.style.cursor = 'none';
};
Client.prototype = {
initialize: function () {
this.link = context.root.findByName('camera').script.link;
this.tanks = context.root.getChildren()[0].script.tanks;
this.bullets = context.root.getChildren()[0].script.bullets;
this.pickables = context.root.getChildren()[0].script.pickables;
this.teams = context.root.getChildren()[0].script.teams;
this.minimap = context.root.getChildren()[0].script.minimap;
var self = this;
// Get query-string parameters (à la `URLSearchParams`).
var uri = new pc.URI(window.location.href);
var query = uri.getQuery();
var socketPort = 'wsport' in query ? query.wsport : 80;
var socketUrl = (window.location.protocol + '//' +
window.location.hostname + ':' + socketPort + '/socket');
var socket = this.socket = new Socket({ url: socketUrl });
this.connected = false;
socket.on('connect', function() {
var qs_player = /[\?&]player=([\w\-]+)/i.exec(window.location.search);
socket.send('register.game', qs_player && qs_player[1]);
});
socket.on('error', function(err) {
console.error('WS error:', err);
});
socket.on('init', function(data) {
self.id = data.id;
self.connected = true;
self.minimap.state(true);
});
socket.on('tank.new', function(data) {
self.tanks.new(data);
});
socket.on('tank.delete', function(data) {
self.tanks.delete(data);
});
socket.on('update', function(data) {
// bullets add
if (data.bullets) {
for(var i = 0; i < data.bullets.length; i++)
self.bullets.new(data.bullets[i]);
}
// bullets delete
if (data.bulletsDelete) {
for(var i = 0; i < data.bulletsDelete.length; i++)
self.bullets.delete(data.bulletsDelete[i]);
}
// pickables add
if (data.pickable) {
for(var i = 0; i < data.pickable.length; i++)
self.pickables.new(data.pickable[i]);
}
// pickable delete
if (data.pickableDelete) {
for(var i = 0; i < data.pickableDelete.length; i++)
self.pickables.delete(data.pickableDelete[i]);
}
// tanks update
if (data.tanks)
self.tanks.updateData(data.tanks);
// tanks respawn
if (data.tanksRespawn) {
for(var i = 0; i < data.tanksRespawn.length; i++)
self.tanks.respawn(data.tanksRespawn[i]);
}
// teams score
if (data.teams) {
for(var i = 0; i < data.teams.length; i++) {
self.teams.teamScore(i, data.teams[i]);
}
}
// winner
if (data.winner) {
self.shoot(false);
self.teams.teamWin(data.winner);
}
});
context.mouse.on('mousedown', this.onMouseDown, this);
context.mouse.on('mouseup', this.onMouseUp, this);
this.gamepadConnected = false;
this.gamepadActive = false;
window.addEventListener('gamepadconnected', function () {
this.gamepadConnected = true;
}.bind(this));
window.addEventListener('gamepaddisconnected', function () {
this.gamepadConnected = false;
}.bind(this));
// Chrome doesn't have the gamepad events, and we can't
// feature detect them in Firefox unfortunately.
if ('chrome' in window) {
// This is a lie, but it lets us begin polling.
this.gamepadConnected = true;
}
},
update: function (dt) {
if (! this.connected)
return;
// collect keyboard input
var movement = [
context.keyboard.isPressed(pc.input.KEY_D) - context.keyboard.isPressed(pc.input.KEY_A),
context.keyboard.isPressed(pc.input.KEY_S) - context.keyboard.isPressed(pc.input.KEY_W)
];
movement[0] += context.keyboard.isPressed(pc.input.KEY_RIGHT) - context.keyboard.isPressed(pc.input.KEY_LEFT);
movement[1] += context.keyboard.isPressed(pc.input.KEY_DOWN) - context.keyboard.isPressed(pc.input.KEY_UP);
// Gamepad controls.
// AUTHORS: Potch and cvan
if (context.gamepads.gamepadsSupported && this.gamepadConnected) {
var gamepadIdx = gamepadNum - 1;
if (!context.gamepads.poll()[gamepadIdx]) {
// If it was active at one point, reset things.
if (self.gamepadActive && self.link && self.link.mouse) {
self.link.mouse.move = true;
this.gamepadActive = false;
}
} else {
// Gamepad movement axes.
var x = context.gamepads.getAxis(gamepadIdx, pc.PAD_L_STICK_X);
var y = context.gamepads.getAxis(gamepadIdx, pc.PAD_L_STICK_Y);
if ((x * x + y * y) > .25) {
movement[0] += x;
movement[1] += y;
}
// Gamepad firing axes.
var gpx = context.gamepads.getAxis(gamepadIdx, pc.PAD_R_STICK_X);
var gpy = context.gamepads.getAxis(gamepadIdx, pc.PAD_R_STICK_Y);
if (x || y || gpx || gpy) {
this.gamepadActive = true;
if (this.link && this.link.mouse) {
this.link.mouse.move = false;
// TODO: Figure out how to hide cursor without destroying
// (so we can show the cursor again if gamepad is disconnected).
var target = context.root.findByName('target');
if (target) {
target.destroy();
}
}
}
// Gamepad shooting.
if (gpx * gpx + gpy * gpy > .25) {
this.shoot(true);
if (this.link) {
this.link.mPos = [
gpx / 2 * (context.graphicsDevice.width / 2),
gpy / 2 * (context.graphicsDevice.height / 2)
];
this.link.angle = Math.floor(Math.atan2(gpx, gpy) / (Math.PI / 180) + 45);
if (this.link.link) {
this.link.link.targeting(this.link.angle);
}
}
} else {
this.shoot(false);
}
}
}
// rotate vector
var t = movement[0] * Math.sin(Math.PI * 0.75) - movement[1] * Math.cos(Math.PI * 0.75);
movement[1] = movement[1] * Math.sin(Math.PI * 0.75) + movement[0] * Math.cos(Math.PI * 0.75);
movement[0] = t;
// check if it is changed
if (movement[0] !== this.movement[0] || movement[1] != this.movement[1]) {
this.movement = movement;
this.socket.send('move', this.movement);
}
},
onMouseDown: function() {
this.shoot(true);
},
onMouseUp: function() {
this.shoot(false);
},
shoot: function(state) {
if (! this.connected)
return;
if (this.shootingState !== state) {
this.shootingState = state;
this.socket.send('shoot', this.shootingState);
}
}
};
return Client;
});