Fixed clock sync and camera tracking.

This commit is contained in:
genxium
2022-10-10 14:33:04 +08:00
parent e224aaf680
commit 5f9aaddc9c
10 changed files with 263 additions and 170 deletions

View File

@@ -6,7 +6,10 @@ cc.Class({
type: cc.Node,
default: null
},
speed: {
type: cc.Float,
default: 500
},
},
onLoad () {
@@ -26,6 +29,9 @@ cc.Class({
if (!selfPlayerRichInfo) return;
const selfPlayerNode = selfPlayerRichInfo.node;
if (!selfPlayerNode) return;
self.mainCamera.node.setPosition(selfPlayerNode.position);
const pDiff = selfPlayerNode.position.sub(self.mainCamera.node.position);
pDiff.normalizeSelf();
const newCamPos = self.mainCamera.node.position.add(pDiff.mul(dt*self.speed));
self.mainCamera.node.setPosition(newCamPos);
}
});

View File

@@ -111,6 +111,10 @@ cc.Class({
type: cc.Integer,
default: 4 // implies (renderFrameIdLagTolerance >> inputScaleFrames) count of inputFrameIds
},
teleportEps1D: {
type: cc.Float,
default: 1e-3
},
},
_inputFrameIdDebuggable(inputFrameId) {
@@ -415,8 +419,10 @@ cc.Class({
self.inputScaleFrames = parsedBattleColliderInfo.inputScaleFrames;
self.inputFrameUpsyncDelayTolerance = parsedBattleColliderInfo.inputFrameUpsyncDelayTolerance;
self.battleDurationNanos = parsedBattleColliderInfo.battleDurationNanos;
self.rollbackEstimatedDt = parsedBattleColliderInfo.rollbackEstimatedDt;
self.rollbackEstimatedDtMillis = 1000.0 * self.rollbackEstimatedDt;
self.rollbackEstimatedDtMillis = parsedBattleColliderInfo.rollbackEstimatedDtMillis;
self.rollbackEstimatedDtNanos = parsedBattleColliderInfo.rollbackEstimatedDtNanos;
self.rollbackEstimatedDtToleranceMillis = self.rollbackEstimatedDtMillis / 1000.0;
self.maxChasingRenderFramesPerUpdate = parsedBattleColliderInfo.maxChasingRenderFramesPerUpdate;
@@ -799,6 +805,12 @@ cc.Class({
// Inside "self.rollbackAndChase", the "self.latestCollisionSys" is ALWAYS ROLLED BACK to "self.recentRenderCache.get(self.renderFrameId)" before being applied dynamics from corresponding inputFrameDownsync, REGARDLESS OF whether or not "self.chaserRenderFrameId == self.renderFrameId" now.
const rdf = self.rollbackAndChase(self.renderFrameId, self.renderFrameId + 1, self.latestCollisionSys, self.latestCollisionSysMap);
/*
const nonTrivialChaseEnded = (prevChaserRenderFrameId < nextChaserRenderFrameId && nextChaserRenderFrameId == self.renderFrameId);
if (nonTrivialChaseEnded) {
console.debug("Non-trivial chase ended, prevChaserRenderFrameId=" + prevChaserRenderFrameId + ", nextChaserRenderFrameId=" + nextChaserRenderFrameId);
}
*/
self.applyRoomDownsyncFrameDynamics(rdf);
let t3 = performance.now();
} catch (err) {
@@ -806,7 +818,7 @@ cc.Class({
} finally {
// Update countdown
if (null != self.countdownNanos) {
self.countdownNanos -= (performance.now() - self.lastRenderFrameIdTriggeredAt) * 1000000;
self.countdownNanos = self.battleDurationNanos - self.renderFrameId*self.rollbackEstimatedDtNanos;
if (self.countdownNanos <= 0) {
self.onBattleStopped(self.playerRichInfoDict);
return;
@@ -975,8 +987,15 @@ cc.Class({
self.playerRichInfoDict.forEach((playerRichInfo, playerId) => {
const immediatePlayerInfo = rdf.players[playerId];
playerRichInfo.node.setPosition(immediatePlayerInfo.x, immediatePlayerInfo.y);
playerRichInfo.scriptIns.scheduleNewDirection(immediatePlayerInfo.dir, true);
const dx = (immediatePlayerInfo.x-playerRichInfo.node.x);
const dy = (immediatePlayerInfo.y-playerRichInfo.node.y);
const selfJiggling = (playerId == self.selfPlayerInfo.playerId && (0 != dx && self.teleportEps1D >= Math.abs(dx) && 0 != dy && self.teleportEps1D >= Math.abs(dy)));
if (!selfJiggling) {
playerRichInfo.node.setPosition(immediatePlayerInfo.x, immediatePlayerInfo.y);
} else {
console.log("selfJiggling: dx = ", dx, ", dy = ", dy);
}
playerRichInfo.scriptIns.scheduleNewDirection(immediatePlayerInfo.dir, false);
playerRichInfo.scriptIns.updateSpeed(immediatePlayerInfo.speed);
});
},

View File

@@ -1,24 +1,20 @@
window.DIRECTION_DECODER = [
[0, 0, null],
[0, +1, null],
[0, -1, null],
[+2, 0, null],
[-2, 0, null],
[+2, +1, null],
[-2, -1, null],
[+2, -1, null],
[-2, +1, null],
[+2, 0, null],
[-2, 0, null],
[0, +1, null],
[0, -1, null],
// The 3rd value matches low-precision constants in backend.
[0, 0, 0.0],
[0, +1, 1.0],
[0, -1, 1.0],
[+2, 0, 0.5],
[-2, 0, 0.5],
[+2, +1, 0.4472],
[-2, -1, 0.4472],
[+2, -1, 0.4472],
[-2, +1, 0.4472],
[+2, 0, 0.5],
[-2, 0, 0.5],
[0, +1, 1.0],
[0, -1, 1.0],
];
for (let k in window.DIRECTION_DECODER) {
const length = Math.sqrt(window.DIRECTION_DECODER[k][0]*window.DIRECTION_DECODER[k][0] + window.DIRECTION_DECODER[k][1]*window.DIRECTION_DECODER[k][1]);
window.DIRECTION_DECODER[k][2] = (0 == length ? 0 : (1.0/length));
}
cc.Class({
extends: cc.Component,
properties: {

View File

@@ -1198,6 +1198,8 @@ $root.treasurehunterx = (function() {
* @property {number|null} [maxChasingRenderFramesPerUpdate] BattleColliderInfo maxChasingRenderFramesPerUpdate
* @property {number|null} [playerBattleState] BattleColliderInfo playerBattleState
* @property {number|null} [rollbackEstimatedDt] BattleColliderInfo rollbackEstimatedDt
* @property {number|null} [rollbackEstimatedDtMillis] BattleColliderInfo rollbackEstimatedDtMillis
* @property {number|Long|null} [rollbackEstimatedDtNanos] BattleColliderInfo rollbackEstimatedDtNanos
*/
/**
@@ -1369,6 +1371,22 @@ $root.treasurehunterx = (function() {
*/
BattleColliderInfo.prototype.rollbackEstimatedDt = 0;
/**
* BattleColliderInfo rollbackEstimatedDtMillis.
* @member {number} rollbackEstimatedDtMillis
* @memberof treasurehunterx.BattleColliderInfo
* @instance
*/
BattleColliderInfo.prototype.rollbackEstimatedDtMillis = 0;
/**
* BattleColliderInfo rollbackEstimatedDtNanos.
* @member {number|Long} rollbackEstimatedDtNanos
* @memberof treasurehunterx.BattleColliderInfo
* @instance
*/
BattleColliderInfo.prototype.rollbackEstimatedDtNanos = $util.Long ? $util.Long.fromBits(0,0,false) : 0;
/**
* Creates a new BattleColliderInfo instance using the specified properties.
* @function create
@@ -1437,6 +1455,10 @@ $root.treasurehunterx = (function() {
writer.uint32(/* id 18, wireType 0 =*/144).int32(message.playerBattleState);
if (message.rollbackEstimatedDt != null && Object.hasOwnProperty.call(message, "rollbackEstimatedDt"))
writer.uint32(/* id 19, wireType 1 =*/153).double(message.rollbackEstimatedDt);
if (message.rollbackEstimatedDtMillis != null && Object.hasOwnProperty.call(message, "rollbackEstimatedDtMillis"))
writer.uint32(/* id 20, wireType 1 =*/161).double(message.rollbackEstimatedDtMillis);
if (message.rollbackEstimatedDtNanos != null && Object.hasOwnProperty.call(message, "rollbackEstimatedDtNanos"))
writer.uint32(/* id 21, wireType 0 =*/168).int64(message.rollbackEstimatedDtNanos);
return writer;
};
@@ -1585,6 +1607,14 @@ $root.treasurehunterx = (function() {
message.rollbackEstimatedDt = reader.double();
break;
}
case 20: {
message.rollbackEstimatedDtMillis = reader.double();
break;
}
case 21: {
message.rollbackEstimatedDtNanos = reader.int64();
break;
}
default:
reader.skipType(tag & 7);
break;
@@ -1691,6 +1721,12 @@ $root.treasurehunterx = (function() {
if (message.rollbackEstimatedDt != null && message.hasOwnProperty("rollbackEstimatedDt"))
if (typeof message.rollbackEstimatedDt !== "number")
return "rollbackEstimatedDt: number expected";
if (message.rollbackEstimatedDtMillis != null && message.hasOwnProperty("rollbackEstimatedDtMillis"))
if (typeof message.rollbackEstimatedDtMillis !== "number")
return "rollbackEstimatedDtMillis: number expected";
if (message.rollbackEstimatedDtNanos != null && message.hasOwnProperty("rollbackEstimatedDtNanos"))
if (!$util.isInteger(message.rollbackEstimatedDtNanos) && !(message.rollbackEstimatedDtNanos && $util.isInteger(message.rollbackEstimatedDtNanos.low) && $util.isInteger(message.rollbackEstimatedDtNanos.high)))
return "rollbackEstimatedDtNanos: integer|Long expected";
return null;
};
@@ -1767,6 +1803,17 @@ $root.treasurehunterx = (function() {
message.playerBattleState = object.playerBattleState | 0;
if (object.rollbackEstimatedDt != null)
message.rollbackEstimatedDt = Number(object.rollbackEstimatedDt);
if (object.rollbackEstimatedDtMillis != null)
message.rollbackEstimatedDtMillis = Number(object.rollbackEstimatedDtMillis);
if (object.rollbackEstimatedDtNanos != null)
if ($util.Long)
(message.rollbackEstimatedDtNanos = $util.Long.fromValue(object.rollbackEstimatedDtNanos)).unsigned = false;
else if (typeof object.rollbackEstimatedDtNanos === "string")
message.rollbackEstimatedDtNanos = parseInt(object.rollbackEstimatedDtNanos, 10);
else if (typeof object.rollbackEstimatedDtNanos === "number")
message.rollbackEstimatedDtNanos = object.rollbackEstimatedDtNanos;
else if (typeof object.rollbackEstimatedDtNanos === "object")
message.rollbackEstimatedDtNanos = new $util.LongBits(object.rollbackEstimatedDtNanos.low >>> 0, object.rollbackEstimatedDtNanos.high >>> 0).toNumber();
return message;
};
@@ -1809,6 +1856,12 @@ $root.treasurehunterx = (function() {
object.maxChasingRenderFramesPerUpdate = 0;
object.playerBattleState = 0;
object.rollbackEstimatedDt = 0;
object.rollbackEstimatedDtMillis = 0;
if ($util.Long) {
var long = new $util.Long(0, 0, false);
object.rollbackEstimatedDtNanos = options.longs === String ? long.toString() : options.longs === Number ? long.toNumber() : long;
} else
object.rollbackEstimatedDtNanos = options.longs === String ? "0" : 0;
}
if (message.stageName != null && message.hasOwnProperty("stageName"))
object.stageName = message.stageName;
@@ -1858,6 +1911,13 @@ $root.treasurehunterx = (function() {
object.playerBattleState = message.playerBattleState;
if (message.rollbackEstimatedDt != null && message.hasOwnProperty("rollbackEstimatedDt"))
object.rollbackEstimatedDt = options.json && !isFinite(message.rollbackEstimatedDt) ? String(message.rollbackEstimatedDt) : message.rollbackEstimatedDt;
if (message.rollbackEstimatedDtMillis != null && message.hasOwnProperty("rollbackEstimatedDtMillis"))
object.rollbackEstimatedDtMillis = options.json && !isFinite(message.rollbackEstimatedDtMillis) ? String(message.rollbackEstimatedDtMillis) : message.rollbackEstimatedDtMillis;
if (message.rollbackEstimatedDtNanos != null && message.hasOwnProperty("rollbackEstimatedDtNanos"))
if (typeof message.rollbackEstimatedDtNanos === "number")
object.rollbackEstimatedDtNanos = options.longs === String ? String(message.rollbackEstimatedDtNanos) : message.rollbackEstimatedDtNanos;
else
object.rollbackEstimatedDtNanos = options.longs === String ? $util.Long.prototype.toString.call(message.rollbackEstimatedDtNanos) : options.longs === Number ? new $util.LongBits(message.rollbackEstimatedDtNanos.low >>> 0, message.rollbackEstimatedDtNanos.high >>> 0).toNumber() : message.rollbackEstimatedDtNanos;
return object;
};