XStateMachine.js 7.2 KB

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