-
Notifications
You must be signed in to change notification settings - Fork 0
/
pong.js
216 lines (173 loc) · 6.32 KB
/
pong.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
/* -*- js-indent-level: 8 -*-
/*
* Pong
* @author Tapani Jamsa
* @author Erno Kuusela
* @author Toni Alatalo
* Date: 2013
*/
// "use strict";
/* jshint -W097, -W040 */
/* global THREE, THREEx, Ammo, window, Director, DirectorScreens */
// MAIN
var sceneCtrl, p2pCtrl, orbitControls, touchController, stats, gui, gameDirector;
var keyboard = new THREEx.KeyboardState();
var clock = new THREE.Clock();
init();
update();
function init() {
// TOUCH
touchController = new TouchInputController();
// DIRECTOR
gameDirector = new Director();
// SCENE
sceneCtrl = new SceneController();
// P2P
p2pCtrl = new P2P();
// DAT GUI
container = document.getElementById('ThreeJS');
gui = new dat.GUI();
gui.add(sceneCtrl, 'playerAmount').min(2).max(100).step(1).listen();
gui.close();
gui.domElement.style.position = 'absolute';
gui.domElement.style.right = '0px';
// gui.domElement.style.zIndex = 100;
container.appendChild(gui.domElement);
// RENDERER
if (Detector.webgl)
renderer = new THREE.WebGLRenderer({
antialias: true
});
else
renderer = new THREE.CanvasRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
container.appendChild(renderer.domElement);
// EVENTS
// Custom resize function because THREEx.windowResize doesn't support orthographic camera
$(window).on('resize', function() {
// notify the renderer of the size change
renderer.setSize(window.innerWidth, window.innerHeight);
sceneCtrl.updateCamera();
});
THREEx.FullScreen.bindKey({
charCode: 'm'.charCodeAt(0)
});
// CONTROLS
orbitControls = new THREE.OrbitControls(sceneCtrl.camera, renderer.domElement);
orbitControls.userZoom = false;
// STATS
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.bottom = '0px';
stats.domElement.style.zIndex = 100;
container.appendChild(stats.domElement);
gui.add(sceneCtrl.ball, 'speed').min(0.1).max(400).step(0.1).listen();
}
function refreshPlayersInfo() {
$("#playersInfo").empty();
for (var i = 0; i < sceneCtrl.playerAmount; i++) {
$("#playersInfo").append("<font color = " + sceneCtrl.playerAreas[i].player.color + ">Player" + (i + 1) + "</font>");
switch (sceneCtrl.playerAreas[i].player.balls) {
case 0:
$("#playersInfo").append("<img width='¨12' height='12' src='images/ballIconRed.png'/> ");
$("#playersInfo").append("<img width='12' height='12' src='images/ballIconRed.png'/> ");
$("#playersInfo").append("<img width='12' height='12' src='images/ballIconRed.png'/> <br>");
break;
case 1:
$("#playersInfo").append("<img width='¨12' height='12' src='images/ballIcon.png'/> ");
$("#playersInfo").append("<img width='12' height='12' src='images/ballIconRed.png'/> ");
$("#playersInfo").append("<img width='12' height='12' src='images/ballIconRed.png'/> <br>");
break;
case 2:
$("#playersInfo").append("<img width='¨12' height='12' src='images/ballIcon.png'/> ");
$("#playersInfo").append("<img width='12' height='12' src='images/ballIcon.png'/> ");
$("#playersInfo").append("<img width='12' height='12' src='images/ballIconRed.png'/> <br>");
break;
case 3:
$("#playersInfo").append("<img width='¨12' height='12' src='images/ballIcon.png'/> ");
$("#playersInfo").append("<img width='12' height='12' src='images/ballIcon.png'/> ");
$("#playersInfo").append("<img width='12' height='12' src='images/ballIcon.png'/> <br>");
break;
}
}
}
// UPDATE FUNCTIONS
function update() {
var delta = clock.getDelta(); // seconds
orbitControls.update();
stats.update();
if (p2pCtrl.netRole === 'client') {
clientUpdate();
} else if (p2pCtrl.netRole === 'server') {
serverUpdate(delta);
} else if (p2pCtrl.netRole === null) {
offlineUpdate(delta);
}
requestAnimationFrame(update);
render();
}
// Update the scene
function clientUpdate() {
sceneCtrl.updateScene();
}
function serverUpdate(delta) {
var racketPositions = [];
for (var i = 0; i < sceneCtrl.playerAreas.length; i++) {
sceneCtrl.playerAreas[i].serverUpdate(delta, p2pCtrl.clientKeysPressed ? p2pCtrl.clientKeysPressed : [], p2pCtrl.clientTouch ? p2pCtrl.clientTouch : 0, p2pCtrl.clientID, i);
racketPositions.push(sceneCtrl.playerAreas[i].racketMesh.position);
}
sceneCtrl.updateScene();
sceneCtrl.btWorldUpdate(delta);
p2pCtrl.serverNetUpdate(racketPositions, sceneCtrl.ball.sphereMesh.position, delta, sceneCtrl.playerAmount);
}
function offlineUpdate(delta) {
for (var i = 0; i < sceneCtrl.playerAreas.length; i++) {
sceneCtrl.playerAreas[i].offlineUpdate(delta);
}
sceneCtrl.updateScene();
sceneCtrl.btWorldUpdate(delta);
}
// Callback from the server
function updateClient(msg) {
// called in client mode (when we're just showing what server tells us).
if (msg.dt === undefined || msg.ballpos === undefined || msg.racketspos === undefined)
throw "update msg: missing properties";
sceneCtrl.ball.sphereMesh.position = msg.ballpos;
for (var i = 0; i < sceneCtrl.playerAreas.length; i++) {
sceneCtrl.playerAreas[i].clientUpdate(msg);
// Camera position. Server camera.lookAt in playerArea.js
if (sceneCtrl.clientPlayerAmount !== sceneCtrl.playerAreas.length && sceneCtrl.playerAreas[i].player.id === p2pCtrl.ThisPeerID) {
var worldPos = new THREE.Vector3();
worldPos.getPositionFromMatrix(sceneCtrl.playerAreas[i].borderLeft.matrixWorld);
sceneCtrl.camera.position.x = worldPos.x;
sceneCtrl.camera.position.z = worldPos.z;
sceneCtrl.camera.lookAt(sceneCtrl.playerAreas[i].group.position);
sceneCtrl.clientPlayerAmount = sceneCtrl.playerAreas.length; // Helps to prevent unnecessary camera position modification
}
}
var inputs = {
keyboard: readKeyboard(),
touch: touchController.deltaPosition.x * touchController.swipeSpeed,
};
return inputs;
}
function render() {
renderer.render(sceneCtrl.scene, sceneCtrl.camera);
}
function showHelp() {
gameDirector.setScreen(DirectorScreens.controls);
}
function readKeyboard() {
var pressed = [];
function checkPressed(keyname) {
if (keyboard.pressed(keyname))
pressed.push(keyname);
}
checkPressed("left");
checkPressed("right");
checkPressed("a");
checkPressed("d");
// checkPressed("o");
// checkPressed("p");
return pressed;
}