|
@@ -2,45 +2,39 @@ import Logger from "./Logger.js"
|
|
|
|
|
|
const logger = new Logger('XStateMachine')
|
|
|
export default class XStateMachine {
|
|
|
- constructor(e) {
|
|
|
- E(this, "state");
|
|
|
- E(this, "isMoving");
|
|
|
- E(this, "isRotating");
|
|
|
- E(this, "_observer");
|
|
|
- E(this, "_movingObserver");
|
|
|
- E(this, "_scene");
|
|
|
- this._scene = e
|
|
|
+ constructor(scene) {
|
|
|
+ this.state
|
|
|
+ this.isMoving
|
|
|
+ this.isRotating
|
|
|
+ this._observer
|
|
|
+ this._movingObserver
|
|
|
+ this._scene = scene
|
|
|
}
|
|
|
- rotateTo(e, t, r, n) {
|
|
|
- return new Promise((o,a)=>{
|
|
|
- var h;
|
|
|
- const s = e.avatarManager.scene;
|
|
|
- if (r && e.setRotation(r),
|
|
|
- t == r)
|
|
|
- return o();
|
|
|
- e.priority === 0 && logger.info(`avatar ${e.id} starts to rotate from ${r} to ${t}`);
|
|
|
- let l = 0;
|
|
|
+ rotateTo(avatar, t, r, aniName) {
|
|
|
+ return new Promise((resolve, reject)=>{
|
|
|
+ const scene = avatar.avatarManager.scene;
|
|
|
+ if (r && avatar.setRotation(r), t == r) return resolve();
|
|
|
+
|
|
|
+ avatar.priority === 0 && logger.info(`avatar ${avatar.id} starts to rotate from ${r} to ${t}`);
|
|
|
+
|
|
|
+ let lerpRatio = 0;
|
|
|
const u = 1e3 / 25
|
|
|
- , c = calcDistance3DAngle(t, e.rotation) / u;
|
|
|
- this._movingObserver && s.onBeforeRenderObservable.remove(this._movingObserver),
|
|
|
- (h = e.controller) == null || h.playAnimation(n || "Walking", !0),
|
|
|
- this._movingObserver = s == null ? void 0 : s.onBeforeRenderObservable.add(()=>{
|
|
|
- var f;
|
|
|
- if (l < 1) {
|
|
|
- if (!e.rootNode)
|
|
|
- return e.setRotation(t),
|
|
|
- o();
|
|
|
- const d = BABYLON.Vector3.Lerp(e.rootNode.rotation, ue4Rotation2Xverse(t), l);
|
|
|
- e.setRotation(xverseRotation2Ue4(d)),
|
|
|
- l += u / (c * 1e3)
|
|
|
+ , c = calcDistance3DAngle(t, avatar.rotation) / u;
|
|
|
+
|
|
|
+ this._movingObserver && scene.onBeforeRenderObservable.remove(this._movingObserver),
|
|
|
+ avatar.controller && avatar.controller.playAnimation(aniName || "Walking", !0),
|
|
|
+ this._movingObserver = scene && scene.onBeforeRenderObservable.add(()=>{
|
|
|
+ if (lerpRatio < 1) {
|
|
|
+ if (!avatar.rootNode) return avatar.setRotation(t), resolve();
|
|
|
+ const d = BABYLON.Vector3.Lerp(avatar.rootNode.rotation, ue4Rotation2Xverse(t), lerpRatio);
|
|
|
+ avatar.setRotation(xverseRotation2Ue4(d)),
|
|
|
+ lerpRatio += u / (c * 1e3)
|
|
|
} else
|
|
|
- return s.onBeforeRenderObservable.remove(this._movingObserver),
|
|
|
- (f = e.controller) == null || f.playAnimation("Idle", !0),
|
|
|
- o()
|
|
|
- }
|
|
|
- )
|
|
|
- }
|
|
|
- )
|
|
|
+ return scene.onBeforeRenderObservable.remove(this._movingObserver),
|
|
|
+ avatar.controller && avatar.controller.playAnimation("Idle", !0),
|
|
|
+ resolve()
|
|
|
+ })
|
|
|
+ })
|
|
|
}
|
|
|
_filterPathPoint(e) {
|
|
|
let t = 0;
|
|
@@ -51,58 +45,61 @@ export default class XStateMachine {
|
|
|
calcDistance3D(e[t], e[t + 1]) < r ? e.splice(t, 1) : t++;
|
|
|
return e
|
|
|
}
|
|
|
- moveTo(e, t, r, n, o, a) {
|
|
|
- return new Promise((s,l)=>{
|
|
|
- var m;
|
|
|
- const u = e.avatarManager.scene;
|
|
|
- e.priority === 0 && logger.info(`avatar ${e.id} starts to move from ${t} to ${r}`);
|
|
|
- let c = 0;
|
|
|
+ moveTo(avatar, t, r, n, aniName, a) {
|
|
|
+ return new Promise((resolve, reject)=>{
|
|
|
+ const scene = avatar.avatarManager.scene;
|
|
|
+ avatar.priority === 0 && logger.info(`avatar ${avatar.id} starts to move from ${t} to ${r}`);
|
|
|
+
|
|
|
+ let lerpRatio = 0;
|
|
|
a ? a = a.concat(r) : a = [r],
|
|
|
a = this._filterPathPoint(a);
|
|
|
let h = t
|
|
|
, f = a.shift();
|
|
|
- if (!f)
|
|
|
- return l("[Engine input path error]");
|
|
|
+ if (!f) return reject("[Engine input path error]");
|
|
|
let d = calcDistance3D(h, f) / n;
|
|
|
const _ = 1e3 / 25;
|
|
|
- e.rootNode.lookAt(ue4Position2Xverse(f));
|
|
|
- const g = xverseRotation2Ue4({
|
|
|
- x: e.rootNode.rotation.x,
|
|
|
- y: e.rootNode.rotation.y,
|
|
|
- z: e.rootNode.rotation.z
|
|
|
+ avatar.rootNode.lookAt(ue4Position2Xverse(f));
|
|
|
+
|
|
|
+ const rotationUE4 = xverseRotation2Ue4({
|
|
|
+ x: avatar.rootNode.rotation.x,
|
|
|
+ y: avatar.rootNode.rotation.y,
|
|
|
+ z: avatar.rootNode.rotation.z
|
|
|
});
|
|
|
- g && (g.roll = 0,
|
|
|
- g.pitch = 0,
|
|
|
- e.setRotation(g)),
|
|
|
- this._movingObserver && u.onBeforeRenderObservable.remove(this._movingObserver),
|
|
|
- (m = e.controller) == null || m.playAnimation(o, !0),
|
|
|
- this._movingObserver = u == null ? void 0 : u.onBeforeRenderObservable.add(()=>{
|
|
|
- var v;
|
|
|
- if (c < 1) {
|
|
|
- const y = BABYLON.Vector3.Lerp(ue4Position2Xverse(h), ue4Position2Xverse(f), c);
|
|
|
- if (e.setPosition(xversePosition2Ue4(y)),
|
|
|
- !e.rootNode)
|
|
|
- return e.setPosition(r),
|
|
|
- s();
|
|
|
- c += _ / (d * 1e3)
|
|
|
- } else if (h = f,
|
|
|
- f = a.shift(),
|
|
|
- f) {
|
|
|
+ rotationUE4 && (
|
|
|
+ rotationUE4.roll = 0,
|
|
|
+ rotationUE4.pitch = 0,
|
|
|
+ avatar.setRotation(rotationUE4)
|
|
|
+ ),
|
|
|
+
|
|
|
+ this._movingObserver && scene.onBeforeRenderObservable.remove(this._movingObserver),
|
|
|
+ avatar.controller && avatar.controller.playAnimation(aniName, !0),
|
|
|
+ this._movingObserver = scene && scene.onBeforeRenderObservable.add(()=>{
|
|
|
+ if (lerpRatio < 1)
|
|
|
+ {
|
|
|
+ const y = BABYLON.Vector3.Lerp(ue4Position2Xverse(h), ue4Position2Xverse(f), lerpRatio);
|
|
|
+ if (avatar.setPosition(xversePosition2Ue4(y)), !avatar.rootNode)
|
|
|
+ return avatar.setPosition(r),
|
|
|
+ resolve();
|
|
|
+ lerpRatio += _ / (d * 1e3)
|
|
|
+ } else if (h = f, f = a.shift(), f)
|
|
|
+ {
|
|
|
d = calcDistance3D(h, f) / n,
|
|
|
- e.rootNode.lookAt(ue4Position2Xverse(f));
|
|
|
- const y = xverseRotation2Ue4({
|
|
|
- x: e.rootNode.rotation.x,
|
|
|
- y: e.rootNode.rotation.y,
|
|
|
- z: e.rootNode.rotation.z
|
|
|
+ avatar.rootNode.lookAt(ue4Position2Xverse(f));
|
|
|
+ const rotationUE4 = xverseRotation2Ue4({
|
|
|
+ x: avatar.rootNode.rotation.x,
|
|
|
+ y: avatar.rootNode.rotation.y,
|
|
|
+ z: avatar.rootNode.rotation.z
|
|
|
});
|
|
|
- y && (y.roll = 0,
|
|
|
- y.pitch = 0,
|
|
|
- e.setRotation(y)),
|
|
|
- c = 0
|
|
|
+ rotationUE4 && (
|
|
|
+ rotationUE4.roll = 0,
|
|
|
+ rotationUE4.pitch = 0,
|
|
|
+ avatar.setRotation(rotationUE4)
|
|
|
+ ),
|
|
|
+ lerpRatio = 0
|
|
|
} else
|
|
|
- return u.onBeforeRenderObservable.remove(this._movingObserver),
|
|
|
- (v = e.controller) == null || v.playAnimation("Idle", !0),
|
|
|
- s()
|
|
|
+ return scene.onBeforeRenderObservable.remove(this._movingObserver),
|
|
|
+ avatar.controller && avatar.controller.playAnimation("Idle", !0),
|
|
|
+ resolve()
|
|
|
}
|
|
|
)
|
|
|
}
|