XStateMachine.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. const log$G = new Logger$1("XStateMachine");
  2. class XStateMachine {
  3. constructor(e) {
  4. E(this, "state");
  5. E(this, "isMoving");
  6. E(this, "isRotating");
  7. E(this, "_observer");
  8. E(this, "_movingObserver");
  9. E(this, "_scene");
  10. this._scene = e
  11. }
  12. rotateTo(e, t, r, n) {
  13. return new Promise((o,a)=>{
  14. var h;
  15. const s = e.avatarManager.scene;
  16. if (r && e.setRotation(r),
  17. t == r)
  18. return o();
  19. e.priority === 0 && log$G.info(`avatar ${e.id} starts to rotate from ${r} to ${t}`);
  20. let l = 0;
  21. const u = 1e3 / 25
  22. , c = calcDistance3DAngle(t, e.rotation) / u;
  23. this._movingObserver && s.onBeforeRenderObservable.remove(this._movingObserver),
  24. (h = e.controller) == null || h.playAnimation(n || "Walking", !0),
  25. this._movingObserver = s == null ? void 0 : s.onBeforeRenderObservable.add(()=>{
  26. var f;
  27. if (l < 1) {
  28. if (!e.rootNode)
  29. return e.setRotation(t),
  30. o();
  31. const d = Vector3.Lerp(e.rootNode.rotation, ue4Rotation2Xverse(t), l);
  32. e.setRotation(xverseRotation2Ue4(d)),
  33. l += u / (c * 1e3)
  34. } else
  35. return s.onBeforeRenderObservable.remove(this._movingObserver),
  36. (f = e.controller) == null || f.playAnimation("Idle", !0),
  37. o()
  38. }
  39. )
  40. }
  41. )
  42. }
  43. _filterPathPoint(e) {
  44. let t = 0;
  45. const r = 1e-4;
  46. if (e.length <= 1)
  47. return [];
  48. for (; t < e.length - 1; )
  49. calcDistance3D(e[t], e[t + 1]) < r ? e.splice(t, 1) : t++;
  50. return e
  51. }
  52. moveTo(e, t, r, n, o, a) {
  53. return new Promise((s,l)=>{
  54. var m;
  55. const u = e.avatarManager.scene;
  56. e.priority === 0 && log$G.info(`avatar ${e.id} starts to move from ${t} to ${r}`);
  57. let c = 0;
  58. a ? a = a.concat(r) : a = [r],
  59. a = this._filterPathPoint(a);
  60. let h = t
  61. , f = a.shift();
  62. if (!f)
  63. return l("[Engine input path error]");
  64. let d = calcDistance3D(h, f) / n;
  65. const _ = 1e3 / 25;
  66. e.rootNode.lookAt(ue4Position2Xverse(f));
  67. const g = xverseRotation2Ue4({
  68. x: e.rootNode.rotation.x,
  69. y: e.rootNode.rotation.y,
  70. z: e.rootNode.rotation.z
  71. });
  72. g && (g.roll = 0,
  73. g.pitch = 0,
  74. e.setRotation(g)),
  75. this._movingObserver && u.onBeforeRenderObservable.remove(this._movingObserver),
  76. (m = e.controller) == null || m.playAnimation(o, !0),
  77. this._movingObserver = u == null ? void 0 : u.onBeforeRenderObservable.add(()=>{
  78. var v;
  79. if (c < 1) {
  80. const y = Vector3.Lerp(ue4Position2Xverse(h), ue4Position2Xverse(f), c);
  81. if (e.setPosition(xversePosition2Ue4(y)),
  82. !e.rootNode)
  83. return e.setPosition(r),
  84. s();
  85. c += _ / (d * 1e3)
  86. } else if (h = f,
  87. f = a.shift(),
  88. f) {
  89. d = calcDistance3D(h, f) / n,
  90. e.rootNode.lookAt(ue4Position2Xverse(f));
  91. const y = xverseRotation2Ue4({
  92. x: e.rootNode.rotation.x,
  93. y: e.rootNode.rotation.y,
  94. z: e.rootNode.rotation.z
  95. });
  96. y && (y.roll = 0,
  97. y.pitch = 0,
  98. e.setRotation(y)),
  99. c = 0
  100. } else
  101. return u.onBeforeRenderObservable.remove(this._movingObserver),
  102. (v = e.controller) == null || v.playAnimation("Idle", !0),
  103. s()
  104. }
  105. )
  106. }
  107. )
  108. }
  109. lookAt(e, t, r) {
  110. return new Promise(n=>{
  111. var _, g;
  112. const o = ue4Position2Xverse(t)
  113. , s = e.rootNode.position.subtract(o).length()
  114. , l = new Vector3(s * Math.sin(e.rootNode.rotation.y),0,s * Math.cos(e.rootNode.rotation.y))
  115. , u = (_ = e.rootNode) == null ? void 0 : _.position.add(l);
  116. let c = 0;
  117. const h = r || 1 / 100
  118. , f = (g = e.rootNode) == null ? void 0 : g.getScene()
  119. , d = f == null ? void 0 : f.onBeforeRenderObservable.add(()=>{
  120. var y, b;
  121. const m = (y = e.controller) == null ? void 0 : y.animations.find(T=>T.name == "Idle");
  122. (m == null ? void 0 : m.isPlaying) != !0 && (m == null || m.play());
  123. const v = Vector3.Lerp(u, o, c);
  124. c < 1 ? ((b = e.rootNode) == null || b.lookAt(v),
  125. c += h) : (d && f.onBeforeRenderObservable.remove(d),
  126. n())
  127. }
  128. )
  129. }
  130. )
  131. }
  132. sendObjectTo(e, t, r, n=2, o=10, a={
  133. x: 0,
  134. y: 0,
  135. z: 150
  136. }) {
  137. return new Promise((s,l)=>{
  138. var u;
  139. if (!r.loaded)
  140. l("Gift has not inited!");
  141. else {
  142. const c = (u = e.rootNode) == null ? void 0 : u.getScene();
  143. let h = 0;
  144. const f = 1 / (n * 25)
  145. , d = f
  146. , _ = o / 100
  147. , g = 8 * _ * f * f;
  148. let m = .5 * g / f
  149. , v = ue4Position2Xverse(e.position);
  150. const y = ue4Position2Xverse(a)
  151. , b = ue4Position2Xverse(e.position)
  152. , T = c == null ? void 0 : c.onBeforeRenderObservable.add(()=>{
  153. (!t || !e.position || !t.position) && (T && c.onBeforeRenderObservable.remove(T),
  154. l("Invalid receiver when shoot gift!")),
  155. r.loaded || (T && c.onBeforeRenderObservable.remove(T),
  156. s());
  157. const C = ue4Position2Xverse(t.position)
  158. , A = new Vector3((C.x - b.x) * f,m,(C.z - b.z) * f);
  159. m = m - g,
  160. h < 1 ? (v = v.add(A),
  161. r.setPositionVector(v.add(y)),
  162. h += d) : (s(),
  163. T && c.onBeforeRenderObservable.remove(T))
  164. }
  165. )
  166. }
  167. }
  168. )
  169. }
  170. roll(e, t, r, n) {
  171. var o, a;
  172. this._observer && ((o = this._scene) == null || o.onBeforeRenderObservable.remove(this._observer)),
  173. t && (r = r != null ? r : 1,
  174. n = n != null ? n : 1,
  175. this._observer = (a = this._scene) == null ? void 0 : a.onBeforeRenderObservable.add(()=>{
  176. e.rootNode.rotation.y += r * .1 * n,
  177. e.rootNode.rotation.y %= Math.PI * 2
  178. }
  179. ))
  180. }
  181. disposeObsever() {
  182. this._movingObserver && this._scene.onBeforeRenderObservable.remove(this._movingObserver)
  183. }
  184. }