Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mamayaya1
GitHub Repository: mamayaya1/game
Path: blob/main/projects/HexGL/bkcore/hexgl/RaceData.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.RaceData = function(track, mode, shipControls)
12
{
13
this.track = track;
14
this.mode = mode;
15
this.shipControls = shipControls;
16
17
this.rate = 2; // 1 / rate
18
this.rateState = 1;
19
20
this.data = [];
21
this.last = -1;
22
this.seek = 0;
23
24
this._p = new THREE.Vector3();
25
this._pp = new THREE.Vector3();
26
this._np = new THREE.Vector3();
27
this._q = new THREE.Quaternion();
28
this._pq = new THREE.Quaternion();
29
this._nq = new THREE.Quaternion();
30
}
31
32
bkcore.hexgl.RaceData.prototype.tick = function(time)
33
{
34
if(this.rateState == 1)
35
{
36
var p = this.shipControls.getPosition();
37
var q = this.shipControls.getQuaternion();
38
this.data.push([
39
time,
40
p.x, p.y, p.z,
41
q.x, q.y, q.z, q.w
42
]);
43
++this.last;
44
}
45
else if(this.rateState == this.rate)
46
{
47
this.rateState = 0;
48
}
49
50
this.rate++;
51
}
52
53
bkcore.hexgl.RaceData.prototype.applyInterpolated = function(time)
54
{
55
while(this.seek < this.last && this.data[this.seek+1][0] < time)
56
++this.seek;
57
58
var prev = this.data[this.seek];
59
this._pp.set(prev[1], prev[2], prev[3]);
60
this._pq.set(prev[4], prev[5], prev[6], prev[7]);
61
62
if(this.seek < 0)
63
{
64
console.warn('Bad race data.');
65
return;
66
}
67
68
// no interpolation
69
if(this.seek == this.last || this.seek == 0)
70
this.shipControls.teleport(this._pp, this._pq);
71
72
// interpolation
73
var next = this.data[this.seek+1];
74
this._np.set(next[1], next[2], next[3]);
75
this._nq.set(next[4], next[5], next[6], next[7]);
76
77
var t = (time-prev[0]) / (next[0]-prev[0]);
78
this._p.copy(this._pp).lerpSelf(this._np, t);
79
this._q.copy(this._pq).slerpSelf(this._nq, t);
80
81
this.shipControls.teleport(this._p, this._q);
82
}
83
84
bkcore.hexgl.RaceData.prototype.reset = function()
85
{
86
this.seek = 0;
87
}
88
89
bkcore.hexgl.RaceData.prototype.export = function()
90
{
91
return this.data;
92
}
93
94
bkcore.hexgl.RaceData.prototype.import = function(imp)
95
{
96
this.data = imp;
97
this.last = this.data.length-1;
98
console.log(this.data);
99
}
100