Updated bkcore.hexgl.RaceData

Added import/export methods.
Added support for tick rates to lighten replay data.
Added sample replay file to replays/* (for testing only, normal replays
will be stored in localStorage).
Moved race data tick from main gameplay loop to mode-specific loop.
This commit is contained in:
Thibaut Despoulain
2012-10-22 22:10:00 +02:00
parent 6705467fe5
commit 4a090470ca
4 changed files with 64 additions and 16 deletions
+2 -1
View File
@@ -53,6 +53,8 @@ bkcore.hexgl.Gameplay = function(opts)
this.modes.timeattack = function()
{
self.raceData.tick(this.timer.time.elapsed);
self.hud.updateTime(self.timer.getElapsedTime());
var cp = self.checkPoint();
@@ -172,7 +174,6 @@ bkcore.hexgl.Gameplay.prototype.update = function()
else if(this.step == 4)
{
this.modes[this.mode].call(this);
this.raceData.tick(this.timer.time.elapsed);
}
else if(this.step == 100 && this.timer.time.elapsed >= 2000)
{
+3
View File
@@ -189,6 +189,9 @@ bkcore.hexgl.HexGL.prototype.displayScore = function(f, l)
{
dr != undefined && (dr.innerHTML = "New local record!");
localStorage['score-'+t+'-'+d] = f;
// Export race data
localStorage['race-'+t+'-'+d] = JSON.Stringify(this.gameplay.raceData.export());
}
else
{
+58 -15
View File
@@ -14,28 +14,34 @@ bkcore.hexgl.RaceData = function(track, mode, shipControls)
this.mode = mode;
this.shipControls = shipControls;
this.rate = 2; // 1 / rate
this.rateState = 1;
this.data = [];
this.last = 0;
this.last = -1;
this.seek = 0;
this._p = new THREE.Vector3();
this._q = new THREE.Quaternion();
}
bkcore.hexgl.RaceData.prototype.load = function(data)
{
this.data = data;
this.last = data.length - 1;
}
bkcore.hexgl.RaceData.prototype.tick = function(time)
{
this.data.push(new bkcore.hexgl.RaceTick(
time,
this.shipControls.getPosition(),
this.shipControls.getQuaternion()
));
++this.last;
if(this.rateState == 1)
{
this.data.push(new bkcore.hexgl.RaceTick(
time,
this.shipControls.getPosition(),
this.shipControls.getQuaternion()
));
++this.last;
}
else if(this.rateState == this.rate)
{
this.rateState = 0;
}
this.rate++;
}
bkcore.hexgl.RaceData.prototype.applyInterpolated = function(time)
@@ -45,9 +51,14 @@ bkcore.hexgl.RaceData.prototype.applyInterpolated = function(time)
var prev = this.data[this.seek];
if(this.seek < 0)
{
console.warn('Bad race data.');
return;
}
// no interpolation
if(this.seek == this.last && time >= prev.time
|| this.seek == 0)
if(this.seek == this.last || this.seek == 0)
this.shipControls.teleport(prev.position, prev.quaternion);
// interpolation
@@ -64,6 +75,38 @@ bkcore.hexgl.RaceData.prototype.reset = function()
this.seek = 0;
}
bkcore.hexgl.RaceData.prototype.export = function()
{
var exp = [];
for(var i = 0; i <= this.last; i++) exp.push(
[this.data[i].time,
this.data[i].position.x,
this.data[i].position.y,
this.data[i].position.z,
this.data[i].quaternion.x,
this.data[i].quaternion.y,
this.data[i].quaternion.z,
this.data[i].quaternion.w]
);
return exp;
}
bkcore.hexgl.RaceData.prototype.import = function(imp)
{
this.data = [];
for(var i = 0; i <= this.last; i++)
{
this.data.push(new bkcore.hexgl.RaceTick(
imp[i][0],
new THREE.Vector3(imp[i][1], imp[i][2], imp[i][3]),
new THREE.Quaternion(imp[i][4], imp[i][5], imp[i][6], imp[i][7])
));
}
return exp;
}
bkcore.hexgl.RaceTick = function(time, position, quaternion)
{
this.time = time;
File diff suppressed because one or more lines are too long