-
Notifications
You must be signed in to change notification settings - Fork 0
/
playerArea.js
297 lines (245 loc) · 9.24 KB
/
playerArea.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
"use strict";
/* -*- js-indent-level: 8 -*- */
/* jshint -W097, -W040 */
/*
* @author Tapani Jamsa
*/
/* global window, THREE, console,
Player, Ammo, keyboard */
function PlayerArea(position, rotation, id, racketGeometry) {
// Pointers to globals
this.p2pCtrl = p2pCtrl;
this.sceneCtrl = sceneCtrl;
this.touchController = touchController;
this.keyboard = keyboard;
// PLAYER INFO
if (this.p2pCtrl.netRole && p2pCtrl.playerArray[id]) {
// online
this.player = p2pCtrl.playerArray[id];
} else {
// offline
var randomColor = this.sceneCtrl.getRandomColor();
this.player = new Player(id, id, randomColor);
}
// PLAYER AREA
this.id = id;
this.group = new THREE.Object3D(); //create an empty container
this.group.rotation.y = rotation;
this.group.position = position;
this.rotation = rotation;
this.bottomTopWidth = this.sceneCtrl.playerAreaWidth;
this.bottomTopHeight = 10;
this.bottomTopGeometry = new THREE.CubeGeometry(this.bottomTopWidth, this.bottomTopHeight, this.bottomTopHeight, 1, 1, 1);
this.leftWidth = 120;
this.leftHeight = 10;
this.leftGeometry = new THREE.CubeGeometry(this.leftWidth, this.bottomTopHeight, this.bottomTopHeight, 1, 1, 1);
this.material = new THREE.MeshLambertMaterial({
color: this.player.color
});
// BORDER BOTTOM
// Create mesh
this.borderBottom = new THREE.Mesh(this.bottomTopGeometry, this.material);
this.borderBottom.position.set(41.73959, 0, 55);
this.borderBottom.rotation.y = 180 * (Math.PI / 180);
this.borderBottom.name = "bottom";
this.borderBottom.type = "box";
// Create a physics model
this.createPhysicsModel(this.bottomTopWidth, this.bottomTopHeight, this.borderBottom, false);
// BORDER LEFT
this.borderLeft = new THREE.Mesh(this.leftGeometry, this.material);
this.borderLeft.position.set(96.05561, 0, 0);
this.borderLeft.rotation.y = 270 * (Math.PI / 180);
this.borderLeft.name = "left";
this.borderLeft.type = "box";
this.borderLeft.parentNode = this;
// Create a physics model
this.createPhysicsModel(this.leftWidth, this.leftHeight, this.borderLeft, false);
// set it as ghost object
this.borderLeft.collider.setCollisionFlags(4);
// BORDER TOP
this.borderTop = new THREE.Mesh(this.bottomTopGeometry, this.material);
this.borderTop.position.set(41.73959, 0, -55);
this.borderTop.rotation.y = 360 * (Math.PI / 180);
this.borderTop.name = "top";
this.borderTop.type = "box";
// Create a physics model
this.createPhysicsModel(this.bottomTopWidth, this.bottomTopHeight, this.borderTop, false);
// PLAYER RACKET
this.racketWidth = 30;
this.racketHeight = 20;
this.racketMesh = new THREE.Mesh(racketGeometry, this.material);
this.racketMesh.position.set(76, 0, 4);
this.racketMesh.rotation.y = 180 * (Math.PI / 180);
this.racketMesh.name = "racket";
this.racketMesh.type = "box";
// Create a physics model
this.racketSpeed = 80;
this.racketTopStop = this.borderTop.position.z + (this.racketWidth / 2);
this.racketBottomStop = this.borderBottom.position.z - (this.racketWidth / 2);
this.createPhysicsModel(this.racketWidth, this.racketHeight, this.racketMesh, true);
// GROUP
this.groupMeshes = [];
this.groupMeshes.push(this.borderBottom);
this.groupMeshes.push(this.borderLeft);
this.groupMeshes.push(this.borderTop);
// CAMERA POSITION (for the server. client camera.lookAt in pong.js)
if (this.p2pCtrl.netRole && this.p2pCtrl.playerArray[id] && this.p2pCtrl.serverID == this.player.id) {
var worldPos = new THREE.Vector3();
worldPos.getPositionFromMatrix(this.borderLeft.matrixWorld);
this.sceneCtrl.camera.position.x = worldPos.x;
this.sceneCtrl.camera.position.z = worldPos.z;
this.sceneCtrl.camera.lookAt(position);
}
}
PlayerArea.prototype.createPhysicsModel = function(width, height, mesh, racket) {
var mass = width * height * height;
var localInertia = new Ammo.btVector3(0, 0, 0);
var w = width / 2;
var h = height / 2;
var shape = null;
if (racket) {
shape = new Ammo.btConvexHullShape();
for (var i = 0; i < mesh.geometry.vertices.length; i++) {
var point = mesh.geometry.vertices[i];
shape.addPoint(new Ammo.btVector3(point.x, point.y, point.z));
}
} else {
shape = new Ammo.btBoxShape(new Ammo.btVector3(w, h, h));
}
shape.calculateLocalInertia(mass, localInertia);
// Local to world pos
this.group.add(mesh);
this.group.updateMatrixWorld();
var worldPos = new THREE.Vector3();
worldPos.getPositionFromMatrix(mesh.matrixWorld);
var startTransform = new Ammo.btTransform();
startTransform.setIdentity();
startTransform.setOrigin(new Ammo.btVector3(worldPos.x, 0, worldPos.z));
// Set rotation
var worldRot = mesh.rotation.y + this.group.rotation.y;
var quat = new Ammo.btQuaternion();
quat.setEuler(worldRot, 0, 0); //or quat.setEulerZYX depending on the ordering you want
startTransform.setRotation(quat);
// Create a collision object
var collisionObject = new Ammo.btCollisionObject();
collisionObject.setWorldTransform(startTransform);
collisionObject.setCollisionShape(shape);
this.sceneCtrl.btWorld.addCollisionObject(collisionObject);
collisionObject.mesh = mesh;
collisionObject.setRestitution(1);
collisionObject.setFriction(0);
mesh.collider = collisionObject;
};
// ONLINE game mode
PlayerArea.prototype.serverUpdate = function(delta, clientKeyboard, clientTouch, playerID, i) {
var lastPosition = this.racketMesh.position.clone();
function checkPressed(scope, keyname, serverID, areaPlayerID) {
if (keyname === null)
return false;
if (serverID == areaPlayerID) {
return scope.keyboard.pressed(keyname);
} else if (playerID == areaPlayerID) {
return clientKeyboard.indexOf(keyname) != -1;
}
return false;
}
function checkTouch(scope, serverID, areaPlayerID) {
if (serverID == areaPlayerID) {
return scope.touchController.deltaPosition.x * scope.touchController.swipeSpeed;
} else if (playerID == areaPlayerID) {
return clientTouch;
}
return false;
}
// Racket controls
var left_key = 'left',
right_key = 'right';
var racketForward = new THREE.Vector3();
var rotation = this.racketMesh.rotation.y + (90 * (Math.PI / 180));
// Angle to vector3
racketForward.x = Math.cos(rotation * -1);
racketForward.z = Math.sin(rotation * -1);
racketForward.normalize();
// Racket's speed
racketForward.multiplyScalar(this.racketSpeed * delta);
if (checkPressed(this, left_key, this.p2pCtrl.serverID, this.player.id)) {
this.racketMesh.position.add(racketForward);
} else if (checkPressed(this, right_key, this.p2pCtrl.serverID, this.player.id)) {
this.racketMesh.position.sub(racketForward);
}
// Touch / Mouse
else {
var touchSpeedModifier = checkTouch(this, this.p2pCtrl.serverID, this.player.id);
if (touchSpeedModifier !== 0) {
racketForward.multiplyScalar(touchSpeedModifier);
this.racketMesh.position.add(racketForward);
}
}
// Local to world position
var worldPos = new THREE.Vector3();
worldPos.getPositionFromMatrix(this.racketMesh.matrixWorld);
var transform = this.racketMesh.collider.getWorldTransform();
transform.setOrigin(new Ammo.btVector3(worldPos.x, 0, worldPos.z));
this.racketMesh.collider.setWorldTransform(transform);
if (this.racketMesh.position.z < this.racketTopStop || this.racketMesh.position.z > this.racketBottomStop) {
this.racketMesh.position = lastPosition;
}
};
// ONLINE game mode
PlayerArea.prototype.clientUpdate = function(msg) {
var newpos = msg.racketspos[this.id];
if (newpos !== undefined)
this.racketMesh.position = newpos;
else
console.log("undefined position for racket");
};
// OFFLINE game mode
PlayerArea.prototype.offlineUpdate = function(delta) {
var lastPosition = this.racketMesh.position.clone();
// Racket controls
if (this.keyboard.pressed("left") || this.keyboard.pressed("right") || this.keyboard.pressed("a") || this.keyboard.pressed("d") || (this.touchController.swiping && delta.x !== 0)) {
var racketForward = new THREE.Vector3();
var rotation = this.racketMesh.rotation.y + (90 * (Math.PI / 180));
// Angle to vector3
racketForward.x = Math.cos(rotation * -1);
racketForward.z = Math.sin(rotation * -1);
racketForward.normalize();
// Racket's speed
if (this.touchController.swiping) {
// Touch / Mouse
racketForward.multiplyScalar(this.racketSpeed * this.touchController.deltaPosition.x * this.touchController.swipeSpeed * delta);
} else {
// Keyboard
racketForward.multiplyScalar(this.racketSpeed * delta);
}
// Keyboard
if (this.player.id == 1) {
if (this.keyboard.pressed("left")) {
this.racketMesh.position.add(racketForward);
}
if (this.keyboard.pressed("right")) {
this.racketMesh.position.sub(racketForward);
}
// Touch
if (this.touchController.swiping) {
this.racketMesh.position.add(racketForward);
}
} else {
if (this.keyboard.pressed("a")) {
this.racketMesh.position.add(racketForward);
}
if (this.keyboard.pressed("d")) {
this.racketMesh.position.sub(racketForward);
}
}
// Local to world position
var worldPos = new THREE.Vector3();
worldPos.getPositionFromMatrix(this.racketMesh.matrixWorld);
var transform = this.racketMesh.collider.getWorldTransform();
transform.setOrigin(new Ammo.btVector3(worldPos.x, 0, worldPos.z));
this.racketMesh.collider.setWorldTransform(transform);
if (this.racketMesh.position.z < this.racketTopStop || this.racketMesh.position.z > this.racketBottomStop) {
this.racketMesh.position = lastPosition;
}
}
};