Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mamayaya1
GitHub Repository: mamayaya1/game
Path: blob/main/projects/HexGL/bkcore/hexgl/ShipControls.js
4627 views
1
/*
2
* HexGL
3
* @author Thibaut 'BKcore' Despoulain <http://bkcore.com>
4
* @license This work is licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported License.
5
* To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/3.0/.
6
*/
7
8
var bkcore = bkcore || {};
9
bkcore.hexgl = bkcore.hexgl || {};
10
11
bkcore.hexgl.ShipControls = function(ctx)
12
{
13
var self = this;
14
var domElement = ctx.document;
15
16
this.active = true;
17
this.destroyed = false;
18
this.falling = false;
19
20
this.dom = domElement;
21
this.mesh = null;
22
23
this.epsilon = 0.00000001;
24
this.zero = new THREE.Vector3(0,0,0);
25
this.airResist = 0.02;
26
this.airDrift = 0.1;
27
this.thrust = 0.02;
28
this.airBrake = 0.02;
29
this.maxSpeed = 7.0;
30
this.boosterSpeed = this.maxSpeed * 0.2;
31
this.boosterDecay = 0.01;
32
this.angularSpeed = 0.005;
33
this.airAngularSpeed = 0.0065;
34
this.repulsionRatio = 0.5;
35
this.repulsionCap = 2.5;
36
this.repulsionLerp = 0.1;
37
this.collisionSpeedDecrease = 0.8;
38
this.collisionSpeedDecreaseCoef = 0.8;
39
this.maxShield = 1.0;
40
this.shieldDelay = 60;
41
this.shieldTiming = 0;
42
this.shieldDamage = 0.25;
43
this.driftLerp = 0.35;
44
this.angularLerp = 0.35;
45
46
this.movement = new THREE.Vector3(0,0,0);
47
this.rotation = new THREE.Vector3(0,0,0);
48
this.roll = 0.0;
49
this.rollAxis = new THREE.Vector3();
50
this.drift = 0.0;
51
this.speed = 0.0;
52
this.speedRatio = 0.0;
53
this.boost = 0.0;
54
this.shield = 1.0;
55
this.angular = 0.0;
56
57
this.currentVelocity = new THREE.Vector3();
58
59
this.quaternion = new THREE.Quaternion();
60
61
this.dummy = new THREE.Object3D();
62
this.dummy.useQuaternion = true;
63
64
this.collisionMap = null;
65
this.collisionPixelRatio = 1.0;
66
this.collisionDetection = false;
67
this.collisionPreviousPosition = new THREE.Vector3();
68
69
this.heightMap = null;
70
this.heightPixelRatio = 1.0;
71
this.heightBias = 0.0;
72
this.heightLerp = 0.4;
73
this.heightScale = 1.0;
74
75
this.rollAngle = 0.6;
76
this.rollLerp = 0.08;
77
this.rollDirection = new THREE.Vector3(0,0,1);
78
79
this.gradient = 0.0;
80
this.gradientTarget = 0.0;
81
this.gradientLerp = 0.05;
82
this.gradientScale = 4.0;
83
this.gradientVector = new THREE.Vector3(0,0,5);
84
this.gradientAxis = new THREE.Vector3(1,0,0);
85
86
this.tilt = 0.0;
87
this.tiltTarget = 0.0;
88
this.tiltLerp = 0.05;
89
this.tiltScale = 4.0;
90
this.tiltVector = new THREE.Vector3(5,0,0);
91
this.tiltAxis = new THREE.Vector3(0,0,1);
92
93
this.repulsionVLeft = new THREE.Vector3(1,0,0);
94
this.repulsionVRight = new THREE.Vector3(-1,0,0);
95
this.repulsionVFront = new THREE.Vector3(0,0,1);
96
this.repulsionVScale = 4.0;
97
this.repulsionAmount = 0.0;
98
this.repulsionForce = new THREE.Vector3();
99
100
this.fallVector = new THREE.Vector3(0,-20,0);
101
102
this.resetPos = null;
103
this.resetRot = null;
104
105
this.key = {
106
forward: false,
107
backward: false,
108
left: false,
109
right: false,
110
ltrigger: false,
111
rtrigger: false,
112
use: false
113
};
114
115
this.collision = {
116
front: false,
117
left: false,
118
right: false
119
};
120
121
this.touchController = null;
122
this.orientationController = null;
123
this.gamepadController = null
124
125
if(ctx.controlType == 1 && bkcore.controllers.TouchController.isCompatible())
126
{
127
this.touchController = new bkcore.controllers.TouchController(
128
domElement, ctx.width/2,
129
function(state, touch, event){
130
if(event.touches.length >= 4)
131
window.location.reload(false);
132
else if(event.touches.length == 3)
133
ctx.restart();
134
// touch was on the right-hand side of the screen
135
else if (touch.clientX > (ctx.width / 2)) {
136
if (event.type === 'touchend')
137
self.key.forward = false;
138
else
139
self.key.forward = true;
140
}
141
});
142
}
143
else if(ctx.controlType == 4 && bkcore.controllers.OrientationController.isCompatible())
144
{
145
this.orientationController = new bkcore.controllers.OrientationController(
146
domElement, true,
147
function(state, touch, event){
148
if(event.touches.length >= 4)
149
window.location.reload(false);
150
else if(event.touches.length == 3)
151
ctx.restart();
152
else if(event.touches.length < 1)
153
self.key.forward = false;
154
else
155
self.key.forward = true;
156
});
157
}
158
else if(ctx.controlType == 3 && bkcore.controllers.GamepadController.isCompatible())
159
{
160
this.gamepadController = new bkcore.controllers.GamepadController(
161
function(controller){
162
if (controller.select)
163
ctx.restart();
164
else
165
self.key.forward = controller.acceleration > 0;
166
self.key.ltrigger = controller.ltrigger > 0;
167
self.key.rtrigger = controller.rtrigger > 0;
168
self.key.left = controller.lstickx < -0.1;
169
self.key.right = controller.lstickx > 0.1;
170
});
171
}
172
else if(ctx.controlType == 2)
173
{
174
if(Leap == null)
175
throw new Error("Unable to reach LeapJS!");
176
177
var leapInfo = this.leapInfo = document.getElementById('leapinfo');
178
isServerConnected = false;
179
var lb = this.leapBridge = {
180
isConnected: true,
181
hasHands: false,
182
palmNormal: [0, 0, 0]
183
};
184
185
function updateInfo()
186
{
187
if(!isServerConnected)
188
{
189
leapInfo.innerHTML = 'Waiting for the Leap Motion Controller server...'
190
leapInfo.style.display = 'block';
191
}
192
else if(lb.isConnected && lb.hasHands)
193
{
194
leapInfo.style.display = 'none';
195
}
196
else if(!lb.isConnected)
197
{
198
leapInfo.innerHTML = 'Please connect your Leap Motion Controller.'
199
leapInfo.style.display = 'block';
200
}
201
else if(!lb.hasHands)
202
{
203
leapInfo.innerHTML = 'Put your hand over the Leap Motion Controller to play.'
204
leapInfo.style.display = 'block';
205
}
206
}
207
updateInfo();
208
209
var lc = this.leapController = new Leap.Controller({enableGestures: false});
210
lc.on('connect', function()
211
{
212
isServerConnected = true;
213
updateInfo();
214
});
215
lc.on('deviceConnected', function()
216
{
217
lb.isConnected = true;
218
updateInfo();
219
});
220
lc.on('deviceDisconnected', function()
221
{
222
lb.isConnected = false;
223
updateInfo();
224
});
225
lc.on('frame', function(frame)
226
{
227
if(!lb.isConnected) return;
228
hand = frame.hands[0];
229
if(typeof hand === 'undefined')
230
{
231
if(lb.hasHands)
232
{
233
lb.hasHands = false;
234
updateInfo();
235
}
236
lb.palmNormal = [0, 0, 0];
237
}
238
else
239
{
240
if(!lb.hasHands)
241
{
242
lb.hasHands = true;
243
updateInfo();
244
}
245
lb.palmNormal = hand.palmNormal;
246
}
247
});
248
lc.connect();
249
}
250
251
function onKeyDown(event)
252
{
253
switch(event.keyCode)
254
{
255
case 38: /*up*/ self.key.forward = true; break;
256
257
case 40: /*down*/self.key.backward = true; break;
258
259
case 37: /*left*/self.key.left = true; break;
260
261
case 39: /*right*/self.key.right = true; break;
262
263
case 81: /*Q*/self.key.ltrigger = true; break;
264
case 65: /*A*/self.key.ltrigger = true; break;
265
266
case 68: /*D*/self.key.rtrigger = true; break;
267
case 69: /*E*/self.key.rtrigger = true; break;
268
}
269
};
270
271
function onKeyUp(event)
272
{
273
switch(event.keyCode)
274
{
275
case 38: /*up*/ self.key.forward = false; break;
276
277
case 40: /*down*/self.key.backward = false; break;
278
279
case 37: /*left*/self.key.left = false; break;
280
281
case 39: /*right*/self.key.right = false; break;
282
283
case 81: /*Q*/self.key.ltrigger = false; break;
284
case 65: /*A*/self.key.ltrigger = false; break;
285
286
case 68: /*D*/self.key.rtrigger = false; break;
287
case 69: /*E*/self.key.rtrigger = false; break;
288
}
289
};
290
291
domElement.addEventListener('keydown', onKeyDown, false);
292
domElement.addEventListener('keyup', onKeyUp, false);
293
};
294
295
bkcore.hexgl.ShipControls.prototype.control = function(threeMesh)
296
{
297
this.mesh = threeMesh;
298
this.mesh.martixAutoUpdate = false;
299
this.dummy.position = this.mesh.position;
300
};
301
302
bkcore.hexgl.ShipControls.prototype.reset = function(position, rotation)
303
{
304
this.resetPos = position;
305
this.resetRot = rotation;
306
this.movement.set(0,0,0);
307
this.rotation.copy(rotation);
308
this.roll = 0.0;
309
this.drift = 0.0;
310
this.speed = 0.0;
311
this.speedRatio = 0.0;
312
this.boost = 0.0;
313
this.shield = this.maxShield;
314
this.destroyed = false;
315
316
this.dummy.position.copy(position);
317
this.quaternion.set(rotation.x, rotation.y, rotation.z, 1).normalize();
318
this.dummy.quaternion.set(0,0,0,1);
319
this.dummy.quaternion.multiplySelf(this.quaternion);
320
321
this.dummy.matrix.setPosition(this.dummy.position);
322
this.dummy.matrix.setRotationFromQuaternion(this.dummy.quaternion);
323
324
this.mesh.matrix.identity();
325
this.mesh.applyMatrix(this.dummy.matrix);
326
}
327
328
bkcore.hexgl.ShipControls.prototype.terminate = function()
329
{
330
this.destroy();
331
332
if(this.leapController != null)
333
{
334
this.leapController.disconnect();
335
this.leapInfo.style.display = 'none';
336
}
337
}
338
339
bkcore.hexgl.ShipControls.prototype.destroy = function()
340
{
341
bkcore.Audio.play('destroyed');
342
bkcore.Audio.stop('bg');
343
bkcore.Audio.stop('wind');
344
345
this.active = false;
346
this.destroyed = true;
347
this.collision.front = false;
348
this.collision.left = false;
349
this.collision.right = false;
350
}
351
352
bkcore.hexgl.ShipControls.prototype.fall = function()
353
{
354
this.active = false;
355
this.collision.front = false;
356
this.collision.left = false;
357
this.collision.right = false;
358
this.falling = true;
359
_this = this;
360
setTimeout(function(){
361
_this.destroyed = true;
362
}, 1500);
363
}
364
365
bkcore.hexgl.ShipControls.prototype.update = function(dt)
366
{
367
if(this.falling)
368
{
369
this.mesh.position.addSelf(this.fallVector);
370
return;
371
}
372
373
this.rotation.y = 0;
374
this.movement.set(0,0,0);
375
this.drift += (0.0 - this.drift) * this.driftLerp;
376
this.angular += (0.0 - this.angular) * this.angularLerp * 0.5;
377
378
var rollAmount = 0.0;
379
var angularAmount = 0.0;
380
var yawLeap = 0.0;
381
382
if(this.leapBridge != null && this.leapBridge.hasHands)
383
{
384
rollAmount -= this.leapBridge.palmNormal[0] * 3.5 * this.rollAngle;
385
yawLeap = -this.leapBridge.palmNormal[2] * 0.6;
386
}
387
388
if(this.active)
389
{
390
391
if(this.touchController != null)
392
{
393
angularAmount -= this.touchController.stickVector.x/100 * this.angularSpeed * dt;
394
rollAmount += this.touchController.stickVector.x/100 * this.rollAngle;
395
}
396
else if(this.orientationController != null)
397
{
398
angularAmount += this.orientationController.beta/45 * this.angularSpeed * dt;
399
rollAmount -= this.orientationController.beta/45 * this.rollAngle;
400
}
401
else if(this.gamepadController != null && this.gamepadController.updateAvailable())
402
{
403
angularAmount -= this.gamepadController.lstickx * this.angularSpeed * dt;
404
rollAmount += this.gamepadController.lstickx * this.rollAngle;
405
}
406
else if(this.leapBridge != null && this.leapBridge.hasHands)
407
{
408
angularAmount += this.leapBridge.palmNormal[0] * 2 * this.angularSpeed * dt;
409
this.speed += Math.max(0.0, (0.5 + this.leapBridge.palmNormal[2])) * 3 * this.thrust * dt;
410
}
411
else
412
{
413
if(this.key.left)
414
{
415
angularAmount += this.angularSpeed * dt;
416
rollAmount -= this.rollAngle;
417
}
418
if(this.key.right)
419
{
420
angularAmount -= this.angularSpeed * dt;
421
rollAmount += this.rollAngle;
422
}
423
}
424
425
if(this.key.forward)
426
this.speed += this.thrust * dt;
427
else
428
this.speed -= this.airResist * dt;
429
if(this.key.ltrigger)
430
{
431
if(this.key.left)
432
angularAmount += this.airAngularSpeed * dt;
433
else
434
angularAmount += this.airAngularSpeed * 0.5 * dt;
435
this.speed -= this.airBrake * dt;
436
this.drift += (this.airDrift - this.drift) * this.driftLerp;
437
this.movement.x += this.speed * this.drift * dt;
438
if(this.drift > 0.0)
439
this.movement.z -= this.speed * this.drift * dt;
440
rollAmount -= this.rollAngle * 0.7;
441
}
442
if(this.key.rtrigger)
443
{
444
if(this.key.right)
445
angularAmount -= this.airAngularSpeed * dt;
446
else
447
angularAmount -= this.airAngularSpeed * 0.5 * dt;
448
this.speed -= this.airBrake * dt;
449
this.drift += (-this.airDrift - this.drift) * this.driftLerp;
450
this.movement.x += this.speed * this.drift * dt;
451
if(this.drift < 0.0)
452
this.movement.z += this.speed * this.drift * dt;
453
rollAmount += this.rollAngle * 0.7;
454
}
455
}
456
457
this.angular += (angularAmount - this.angular) * this.angularLerp;
458
this.rotation.y = this.angular;
459
460
this.speed = Math.max(0.0, Math.min(this.speed, this.maxSpeed));
461
this.speedRatio = this.speed / this.maxSpeed;
462
this.movement.z += this.speed * dt;
463
464
if(this.repulsionForce.isZero())
465
{
466
this.repulsionForce.set(0,0,0);
467
}
468
else
469
{
470
if(this.repulsionForce.z != 0.0) this.movement.z = 0;
471
this.movement.addSelf(this.repulsionForce);
472
this.repulsionForce.lerpSelf(this.zero, dt > 1.5 ? this.repulsionLerp*2 : this.repulsionLerp);
473
}
474
475
this.collisionPreviousPosition.copy(this.dummy.position);
476
477
this.boosterCheck(dt);
478
479
//this.movement.multiplyScalar(dt);
480
//this.rotation.multiplyScalar(dt);
481
482
this.dummy.translateX(this.movement.x);
483
this.dummy.translateZ(this.movement.z);
484
485
486
this.heightCheck(dt);
487
this.dummy.translateY(this.movement.y);
488
489
this.currentVelocity.copy(this.dummy.position).subSelf(this.collisionPreviousPosition);
490
491
this.collisionCheck(dt);
492
493
this.quaternion.set(this.rotation.x, this.rotation.y, this.rotation.z, 1).normalize();
494
this.dummy.quaternion.multiplySelf(this.quaternion);
495
496
this.dummy.matrix.setPosition(this.dummy.position);
497
this.dummy.matrix.setRotationFromQuaternion(this.dummy.quaternion);
498
499
if(this.shield <= 0.0)
500
{
501
this.shield = 0.0;
502
this.destroy();
503
}
504
505
if(this.mesh != null)
506
{
507
this.mesh.matrix.identity();
508
509
// Gradient (Mesh only, no dummy physics impact)
510
var gradientDelta = (this.gradientTarget - (yawLeap + this.gradient)) * this.gradientLerp;
511
if(Math.abs(gradientDelta) > this.epsilon) this.gradient += gradientDelta;
512
if(Math.abs(this.gradient) > this.epsilon)
513
{
514
this.gradientAxis.set(1,0,0);
515
this.mesh.matrix.rotateByAxis(this.gradientAxis, this.gradient);
516
}
517
518
// Tilting (Idem)
519
var tiltDelta = (this.tiltTarget - this.tilt) * this.tiltLerp;
520
if(Math.abs(tiltDelta) > this.epsilon) this.tilt += tiltDelta;
521
if(Math.abs(this.tilt) > this.epsilon)
522
{
523
this.tiltAxis.set(0,0,1);
524
this.mesh.matrix.rotateByAxis(this.tiltAxis, this.tilt);
525
}
526
527
// Rolling (Idem)
528
var rollDelta = (rollAmount - this.roll) * this.rollLerp;
529
if(Math.abs(rollDelta) > this.epsilon) this.roll += rollDelta;
530
if(Math.abs(this.roll) > this.epsilon)
531
{
532
this.rollAxis.copy(this.rollDirection);
533
this.mesh.matrix.rotateByAxis(this.rollAxis, this.roll);
534
}
535
536
this.mesh.applyMatrix(this.dummy.matrix);
537
this.mesh.updateMatrixWorld(true);
538
}
539
540
//Update listener position
541
bkcore.Audio.setListenerPos(this.movement);
542
bkcore.Audio.setListenerVelocity(this.currentVelocity);
543
};
544
545
bkcore.hexgl.ShipControls.prototype.teleport = function(pos, quat)
546
{
547
this.quaternion.copy(quat);
548
this.dummy.quaternion.copy(this.quaternion);
549
550
this.dummy.position.copy(pos);
551
this.dummy.matrix.setPosition(this.dummy.position);
552
553
//console.log(pos.x, pos.y, pos.z);
554
555
this.dummy.matrix.setRotationFromQuaternion(this.dummy.quaternion);
556
557
if(this.mesh != null)
558
{
559
this.mesh.matrix.identity();
560
561
// Gradient (Mesh only, no dummy physics impact)
562
var gradientDelta = (this.gradientTarget - this.gradient) * this.gradientLerp;
563
if(Math.abs(gradientDelta) > this.epsilon) this.gradient += gradientDelta;
564
if(Math.abs(this.gradient) > this.epsilon)
565
{
566
this.gradientAxis.set(1,0,0);
567
this.mesh.matrix.rotateByAxis(this.gradientAxis, this.gradient);
568
}
569
570
// Tilting (Idem)
571
var tiltDelta = (this.tiltTarget - this.tilt) * this.tiltLerp;
572
if(Math.abs(tiltDelta) > this.epsilon) this.tilt += tiltDelta;
573
if(Math.abs(this.tilt) > this.epsilon)
574
{
575
this.tiltAxis.set(0,0,1);
576
this.mesh.matrix.rotateByAxis(this.tiltAxis, this.tilt);
577
}
578
579
this.mesh.applyMatrix(this.dummy.matrix);
580
this.mesh.updateMatrixWorld(true);
581
}
582
}
583
584
bkcore.hexgl.ShipControls.prototype.boosterCheck = function(dt)
585
{
586
if(!this.collisionMap || !this.collisionMap.loaded)
587
return false;
588
589
this.boost -= this.boosterDecay * dt;
590
if(this.boost < 0){
591
this.boost = 0.0;
592
bkcore.Audio.stop('boost');
593
}
594
595
var x = Math.round(this.collisionMap.pixels.width/2 + this.dummy.position.x * this.collisionPixelRatio);
596
var z = Math.round(this.collisionMap.pixels.height/2 + this.dummy.position.z * this.collisionPixelRatio);
597
var pos = new THREE.Vector3(x, 0, z);
598
599
var color = this.collisionMap.getPixel(x, z);
600
601
if(color.r == 255 && color.g < 127 && color.b < 127) {
602
bkcore.Audio.play('boost');
603
this.boost = this.boosterSpeed;
604
}
605
606
this.movement.z += this.boost * dt;
607
}
608
609
bkcore.hexgl.ShipControls.prototype.collisionCheck = function(dt)
610
{
611
if(!this.collisionDetection || !this.collisionMap || !this.collisionMap.loaded)
612
return false;
613
614
if(this.shieldDelay > 0)
615
this.shieldDelay -= dt;
616
617
this.collision.left = false;
618
this.collision.right = false;
619
this.collision.front = false;
620
621
var x = Math.round(this.collisionMap.pixels.width/2 + this.dummy.position.x * this.collisionPixelRatio);
622
var z = Math.round(this.collisionMap.pixels.height/2 + this.dummy.position.z * this.collisionPixelRatio);
623
var pos = new THREE.Vector3(x, 0, z);
624
625
//console.log({c: this.collisionMap.getPixel(414, 670), d: this.dummy.position, x: x, y: y, p: this.collisionMap.getPixel(x, y)})
626
627
var collision = this.collisionMap.getPixelBilinear(x, z);
628
629
if(collision.r < 255)
630
{
631
bkcore.Audio.play('crash');
632
633
// Shield
634
var sr = (this.getRealSpeed() / this.maxSpeed);
635
this.shield -= sr * sr * 0.8 * this.shieldDamage;
636
637
// Repulsion
638
this.repulsionVLeft.set(1,0,0);
639
this.repulsionVRight.set(-1,0,0);
640
this.dummy.matrix.rotateAxis(this.repulsionVLeft);
641
this.dummy.matrix.rotateAxis(this.repulsionVRight);
642
this.repulsionVLeft.multiplyScalar(this.repulsionVScale);
643
this.repulsionVRight.multiplyScalar(this.repulsionVScale);
644
645
var lPos = this.repulsionVLeft.addSelf(pos);
646
var rPos = this.repulsionVRight.addSelf(pos);
647
var lCol = this.collisionMap.getPixel(Math.round(lPos.x), Math.round(lPos.z)).r;
648
var rCol = this.collisionMap.getPixel(Math.round(rPos.x), Math.round(rPos.z)).r;
649
650
this.repulsionAmount = Math.max(0.8,
651
Math.min(this.repulsionCap,
652
this.speed * this.repulsionRatio
653
)
654
);
655
656
if(rCol > lCol)
657
{// Repulse right
658
this.repulsionForce.x += -this.repulsionAmount;
659
this.collision.left = true;
660
}
661
else if(rCol < lCol)
662
{// Repulse left
663
this.repulsionForce.x += this.repulsionAmount;
664
this.collision.right = true;
665
}
666
else
667
{
668
//console.log(collision.r+" -- "+fCol+" @ "+lCol+" / "+rCol);
669
this.repulsionForce.z += -this.repulsionAmount*4;
670
this.collision.front = true;
671
this.speed = 0;
672
}
673
674
// DIRTY GAMEOVER
675
if(rCol < 128 && lCol < 128)
676
{
677
var fCol = this.collisionMap.getPixel(Math.round(pos.x+2), Math.round(pos.z+2)).r;
678
if(fCol < 128)
679
{
680
console.log('GAMEOVER');
681
this.fall();
682
}
683
}
684
685
this.speed *= this.collisionSpeedDecrease;
686
this.speed *= (1-this.collisionSpeedDecreaseCoef*(1-collision.r/255));
687
this.boost = 0;
688
689
return true;
690
}
691
else
692
{
693
return false;
694
}
695
}
696
697
bkcore.hexgl.ShipControls.prototype.heightCheck = function(dt)
698
{
699
if(!this.heightMap || !this.heightMap.loaded)
700
return false;
701
702
var x = this.heightMap.pixels.width/2 + this.dummy.position.x * this.heightPixelRatio;
703
var z = this.heightMap.pixels.height/2 + this.dummy.position.z * this.heightPixelRatio;
704
var height = this.heightMap.getPixelFBilinear(x, z) / this.heightScale + this.heightBias;
705
706
var color = this.heightMap.getPixel(x, z);
707
708
if(height < 16777)
709
{
710
var delta = (height - this.dummy.position.y);
711
712
if(delta > 0)
713
{
714
this.movement.y += delta;
715
}
716
else
717
{
718
this.movement.y += delta * this.heightLerp;
719
}
720
}
721
722
// gradient
723
this.gradientVector.set(0,0,5);
724
this.dummy.matrix.rotateAxis(this.gradientVector);
725
this.gradientVector.addSelf(this.dummy.position);
726
727
x = this.heightMap.pixels.width/2 + this.gradientVector.x * this.heightPixelRatio;
728
z = this.heightMap.pixels.height/2 + this.gradientVector.z * this.heightPixelRatio;
729
730
var nheight = this.heightMap.getPixelFBilinear(x, z) / this.heightScale + this.heightBias;
731
732
if(nheight < 16777)
733
this.gradientTarget = -Math.atan2(nheight-height, 5.0)*this.gradientScale;
734
735
// tilt
736
this.tiltVector.set(5,0,0);
737
this.dummy.matrix.rotateAxis(this.tiltVector);
738
this.tiltVector.addSelf(this.dummy.position);
739
740
x = this.heightMap.pixels.width/2 + this.tiltVector.x * this.heightPixelRatio;
741
z = this.heightMap.pixels.height/2 + this.tiltVector.z * this.heightPixelRatio;
742
743
nheight = this.heightMap.getPixelFBilinear(x, z) / this.heightScale + this.heightBias;
744
745
if(nheight >= 16777) // If right project out of bounds, try left projection
746
{
747
this.tiltVector.subSelf(this.dummy.position).multiplyScalar(-1).addSelf(this.dummy.position);
748
749
x = this.heightMap.pixels.width/2 + this.tiltVector.x * this.heightPixelRatio;
750
z = this.heightMap.pixels.height/2 + this.tiltVector.z * this.heightPixelRatio;
751
752
nheight = this.heightMap.getPixelFBilinear(x, z) / this.heightScale + this.heightBias;
753
}
754
755
if(nheight < 16777)
756
this.tiltTarget = Math.atan2(nheight-height, 5.0)*this.tiltScale;
757
};
758
759
bkcore.hexgl.ShipControls.prototype.getRealSpeed = function(scale)
760
{
761
return Math.round(
762
(this.speed+this.boost)
763
* (scale == undefined ? 1 : scale)
764
);
765
};
766
767
bkcore.hexgl.ShipControls.prototype.getRealSpeedRatio = function()
768
{
769
return Math.min(
770
this.maxSpeed,
771
this.speed+this.boost
772
) / this.maxSpeed;
773
};
774
775
bkcore.hexgl.ShipControls.prototype.getSpeedRatio = function()
776
{
777
return (this.speed+this.boost)/ this.maxSpeed;
778
};
779
780
bkcore.hexgl.ShipControls.prototype.getBoostRatio = function()
781
{
782
return this.boost / this.boosterSpeed;
783
};
784
785
bkcore.hexgl.ShipControls.prototype.getShieldRatio = function()
786
{
787
return this.shield / this.maxShield;
788
};
789
790
bkcore.hexgl.ShipControls.prototype.getShield = function(scale)
791
{
792
return Math.round(
793
this.shield
794
* (scale == undefined ? 1 : scale)
795
);
796
};
797
798
bkcore.hexgl.ShipControls.prototype.getPosition = function()
799
{
800
return this.dummy.position;
801
}
802
803
bkcore.hexgl.ShipControls.prototype.getQuaternion = function()
804
{
805
return this.dummy.quaternion;
806
}
807
808