xushiting 3 jaren geleden
bovenliggende
commit
1bfa023f75
1 gewijzigde bestanden met toevoegingen van 200 en 152 verwijderingen
  1. 200 152
      src/XAvatar.js

+ 200 - 152
src/XAvatar.js

@@ -11,85 +11,88 @@ const castRayOffsetY = .01;
 const castRayTeleportationOffset = 10;
 
 export default class XAvatar {
-    constructor({id: e, avatarType: t, priority: r, avatarManager: n, assets: o, status: a}) {
-        E(this, "id", "-1");
-        E(this, "priority", 0);
-        E(this, "isRender", !1);
-        E(this, "distLevel", 0);
-        E(this, "isInLoadingList", !1);
-        E(this, "isHide", !1);
-        E(this, "component");
-        E(this, "controller");
-        E(this, "stateMachine");
-        E(this, "bbComponent");
-        E(this, "_avatarType");
-        E(this, "clothesList", []);
-        E(this, "isSelected", !1);
-        E(this, "pendingLod", !1);
-        E(this, "_previousReceivedPosition", new BABYLON.Vector3(0,1e4,0));
-        E(this, "_avatarPosition");
-        E(this, "_avatarRotation");
-        E(this, "_avatarScale");
-        E(this, "rootNode");
-        E(this, "distToCam", 1e11);
-        E(this, "enableNickname", !0);
-        E(this, "distance", 1e11);
-        E(this, "isCulling", !1);
-        E(this, "reslevel", 0);
-        E(this, "isInLoadingQueue", !1);
-        E(this, "_isRayCastEnable");
-        E(this, "_scene");
-        E(this, "_avatarManager");
-        E(this, "_transparent", 0);
-        E(this, "hide", ()=>(this.isHide = !0,
+    constructor({id: e, avatarType: i, priority: o, avatarManager: s, assets: c, status: d}) {
+        this.id = "-1",
+        this.priority = 0,
+        this.isRender = !1,
+        this.distLevel = 0,
+        this.isInLoadingList = !1,
+        this.isHide = !1,
+        this.clothesList = [],
+        this.isSelected = !1,
+        this.pendingLod = !1,
+        this._previousReceivedPosition = new BABYLON.Vector3(0,1e4,0),
+        this.rootNode = void 0,
+        this.distToCam = 1e11,
+        this.enableNickname = !0,
+        this.distance = 1e11,
+        this.isCulling = !1,
+        this.reslevel = 0,
+        this.isInLoadingQueue = !1,
+        this._scene = void 0,
+        this._transparent = 0,
+        this.hide = ()=>(this.isHide = !0,
         this._hide(),
-        !this.isRender));
-        E(this, "_show", ()=>{
-            var e;
+        !this.isRender),
+        this._show = ()=>{
+            var _;
             this.isHide || (this.setIsPickable(!0),
+            this.bbComponent._attachmentObservers.forEach((b,k)=>{
+                k.setEnabled(!0)
+            }
+            ),
             this.priority == 0 && (this.rootNode.setEnabled(!0),
             this.isRender = !0,
             this.avatarManager._updateBillboardStatus(this, BillboardStatus.SHOW),
-            (e = this.controller) == null || e.playAnimation(this.controller.onPlay, this.controller.loop)))
+            this.component.accessories.forEach(b=>{
+                b.rootComponent.setEnabled(!0)
+            }
+            ),
+            (_ = this.controller) == null || _.playAnimation(this.controller.onPlay, this.controller.loop)),
+            this.component.accessories.forEach(b=>{
+                b.rootComponent.setEnabled(!0)
+            }
+            ))
         }
-        );
-        E(this, "show", ()=>(this.isHide = !1,
+        ,
+        this.show = ()=>(this.isHide = !1,
         this._show(),
-        !!this.isRender));
-        E(this, "setAnimations", e=>{
-            this.controller.animations = e
+        !!this.isRender),
+        this.setAnimations = _=>{
+            this.controller.animations = _
         }
-        );
-        E(this, "attachToAvatar", (e,t=!1,r={
+        ,
+        this.attachToAvatar = (_,b=!1,k={
             x: 0,
             y: 0,
             z: 0
-        },n=!1,o,a)=>this.bbComponent.attachToAvatar(this, e, t, r, n, o, a));
-        E(this, "detachFromAvatar", (e,t=!1)=>this.bbComponent.detachFromAvatar(this, e, t));
-        E(this, "getBbox", (e={})=>this.bbComponent.getBbox(this, e));
+        },j=!1,$,_e)=>this.bbComponent.attachToAvatar(this, _, b, k, j, _e),
+        this.detachFromAvatar = (_,b=!1)=>this.bbComponent.detachFromAvatar(this, _, b),
+        this.getBbox = (_={})=>this.bbComponent.getBbox(this, _),
         this.id = e,
-        this._avatarManager = n,
+        this._avatarManager = s,
         this._scene = this.avatarManager.scene,
-        this.clothesList = o,
-        this._avatarType = t,
-        this.priority = r || 0,
+        this.clothesList = c,
+        this._avatarType = i,
+        this.priority = o || 0,
         this.controller = new XAnimationController(this),
         this.component = new XAvatarComopnent,
         this.stateMachine = new XStateMachine(this._scene),
         this.bbComponent = new XAvatarBillboardComponent(this._scene),
         this.rootNode = new BABYLON.TransformNode(e,this._avatarManager.scene),
-        this._avatarScale = a.avatarScale == null ? 1 : a.avatarScale,
-        this._avatarRotation = a.avatarRotation == null ? {
+        this._avatarScale = d.avatarScale == null ? 1 : d.avatarScale,
+        this._avatarRotation = d.avatarRotation == null ? {
             pitch: 0,
             yaw: 0,
             roll: 0
-        } : a.avatarRotation,
-        this._avatarPosition = a.avatarPosition == null ? {
+        } : d.avatarRotation,
+        this._avatarPosition = d.avatarPosition == null ? {
             x: 0,
             y: 0,
             z: 0
-        } : a.avatarPosition,
-        this.setPosition(this._avatarPosition),
+        } : d.avatarPosition,
+        this._isRayCastEnable = avatarSetting.isRayCastEnable,
+        this.setPosition(this._avatarPosition, !0),
         this.setRotation(this._avatarRotation),
         this.setScale(this.scale),
         this._isRayCastEnable = avatarSetting.isRayCastEnable,
@@ -98,13 +101,14 @@ export default class XAvatar {
         }
         )
     }
+
     tick() {
         this.cullingTick()
     }
     cullingTick() {
         var e;
-        this.isCulling && ((e = this.rootNode) == null || e.getChildMeshes().forEach(t=>{
-            this.distToCam < 50 ? t.visibility = 0 : t.visibility = this._transparent
+        this.isCulling && ((e = this.rootNode) == null || e.getChildMeshes().forEach(i=>{
+            this.distToCam < 50 ? i.visibility = 0 : i.visibility = this._transparent
         }
         ))
     }
@@ -194,13 +198,25 @@ export default class XAvatar {
     }
     _hide() {
         !this.isHide || (this.setIsPickable(!1),
+        this.bbComponent._attachmentObservers.forEach((e,i)=>{
+            i.setEnabled(!1)
+        }
+        ),
         this.priority == 0 ? (this.rootNode.setEnabled(!1),
         this.isRender = !1,
-        this.bbComponent.updateBillboardStatus(this, BillboardStatus.HIDE)) : this.isRender && (this.avatarManager.currentLODUsers[this.distLevel]--,
-        this.removeAvatarFromScene()))
+        this.bbComponent.updateBillboardStatus(this, BillboardStatus.HIDE),
+        this.component.accessories.forEach(e=>{
+            e.rootComponent.setEnabled(!1)
+        }
+        )) : this.isRender && (this.avatarManager.currentLODUsers[this.distLevel]--,
+        this.removeAvatarFromScene()),
+        this.component.accessories.forEach(e=>{
+            e.rootComponent.setEnabled(!1)
+        }
+        ))
     }
-    rotate(e, t, r) {
-        return this.stateMachine.roll(this, e, t, r)
+    rotate(e, i, o) {
+        return this.stateMachine.roll(this, e, i, o)
     }
     set isRayCastEnable(e) {
         this._isRayCastEnable = e
@@ -215,42 +231,42 @@ export default class XAvatar {
         const e = avatarLoader.avaliableAnimation.get(this.avatarType);
         return e || []
     }
-    setPosition(e, t=!1) {
+    setPosition(e, i=!1) {
         if (this._avatarPosition = e,
         this.rootNode) {
-            const r = ue4Position2Xverse(this._avatarPosition);
-            let n = !1;
-            this.avatarManager.getMainAvatar() && (this.id != this.avatarManager.getMainAvatar().id || (Math.abs(r.y - this._previousReceivedPosition.y) > castRayOffsetY && (n = !0),
-            r.subtract(this._previousReceivedPosition).length() > castRayTeleportationOffset && (n = !0))),
-            this._isRayCastEnable ? n || t ? this._castRay(e).then(o=>{
-                this.rootNode.position = r,
-                this.rootNode.position.y -= o
+            const o = ue4Position2Xverse(this._avatarPosition);
+            let s = !1;
+            this.avatarManager.getMainAvatar() && (this.id != this.avatarManager.getMainAvatar().id || (Math.abs(o.y - this._previousReceivedPosition.y) > castRayOffsetY && (s = !0),
+            o.subtract(this._previousReceivedPosition).length() > castRayTeleportationOffset && (s = !0))),
+            this._isRayCastEnable ? s || i ? this._castRay(e).then(c=>{
+                this.rootNode.position = o,
+                this.rootNode.position.y -= c
             }
-            ).catch(o=>{
-                Promise.reject(o)
+            ).catch(c=>{
+                Promise.reject(c)
             }
-            ) : (this.rootNode.position.x = r.x,
-            this.rootNode.position.z = r.z) : this.rootNode.position = r,
-            this._previousReceivedPosition = r.clone()
+            ) : (this.rootNode.position.x = o.x,
+            this.rootNode.position.z = o.z) : this.rootNode.position = o,
+            this._previousReceivedPosition = o.clone()
         }
         return Promise.resolve(e)
     }
     setRotation(e) {
         if (this._avatarRotation = e,
         this.rootNode) {
-            const t = {
+            const i = {
                 pitch: e.pitch,
                 yaw: e.yaw + 180,
                 roll: e.roll
             }
-              , r = ue4Rotation2Xverse(t);
-            this.rootNode.rotation = r
+              , o = ue4Rotation2Xverse(i);
+            this.rootNode.rotation = o
         }
     }
     setAvatarVisible(e) {
         this.rootNode && (this.rootNode.setEnabled(e),
-        this.rootNode.getChildMeshes().forEach(t=>{
-            t.setEnabled(e)
+        this.rootNode.getChildMeshes().forEach(i=>{
+            i.setEnabled(e)
         }
         ))
     }
@@ -260,11 +276,15 @@ export default class XAvatar {
         this.bbComponent.bbox && this.getBbox()
     }
     _removeAvatarFromScene() {
-        var e, t;
+        var e, i;
         this.isRender = !1,
         (e = this.controller) == null || e.detachAnimation(),
         this.component.dispose(this),
-        (t = this.avatarManager.sceneManager) == null || t.lightComponent.removeShadow(this)
+        (i = this.avatarManager.sceneManager) == null || i.lightComponent.removeShadow(this),
+        this.component.accessories.forEach(o=>{
+            o.rootComponent.setEnabled(!1)
+        }
+        )
     }
     removeAvatarFromScene() {
         this._removeAvatarFromScene(),
@@ -273,30 +293,52 @@ export default class XAvatar {
     _disposeBillBoard() {
         this.bbComponent.disposeBillBoard(this)
     }
-    addComponent(e, t, r, n) {
-        return this.component.changeClothesComp(this, e, t, r, n)
+    addComponent(e, i, o, s) {
+        return i === "pendant" ? this.component.attachPendant(this, e) : this.component.changeClothesComp(this, e, i, o, s)
+    }
+    removeComponent(e, i) {
+        if (e === "pendant")
+            i ? this.component.detachPendant(i) : this.component.accessories.forEach((o,s)=>{
+                this.component.detachPendant(s)
+            }
+            );
+        else {
+            const o = this.component.resourceIdList.find(s=>s.type == e);
+            o && (this.detachDecoration(o),
+            this.clothesList = this.clothesList.filter(s=>s.type != e))
+        }
+    }
+    getComponentByType(e, i) {
+        if (e === "pendant")
+            if (i) {
+                const o = this.component.accessories.get(i);
+                return o || []
+            } else
+                return Array.from(this.component.accessories).map(o=>o[1]);
+        else
+            return this.component.resourceIdList.find(o=>o.type == e)
     }
     _castRay(e) {
-        return new Promise((t,r)=>{
-            var d;
-            const n = ue4Position2Xverse(e)
-              , o = new BABYLON.Vector3(0,-1,0)
-              , a = 1.5 * this.scale
-              , s = 100 * a
-              , l = a
-              , u = new BABYLON.Vector3(n.x,n.y + l,+n.z)
-              , c = new BABYLON.Ray(u,o,s)
-              , h = (d = this.avatarManager.sceneManager) == null ? void 0 : d.getGround(e);
-            if (!h || h.length <= 0)
-                return logger.warn(`\u89D2\u8272 id= ${this.id} \u627E\u4E0D\u5230\u5730\u9762\uFF0C\u5F53\u524D\u9AD8\u5EA6\u4E3A\u4E0B\u53D1\u9AD8\u5EA6`),
-                t(0);
-            let f = c.intersectsMeshes(h);
-            if (f.length > 0)
-                return t(f[0].distance - l);
-            if (o.y = 1,
-            f = c.intersectsMeshes(h),
-            f.length > 0)
-                return t(-(f[0].distance - l))
+        return new Promise((i,o)=>{
+            var et;
+            const s = ue4Position2Xverse(e)
+              , c = new BABYLON.Vector3(0,-1,0)
+              , d = 1.5 * this.scale
+              , _ = 100 * d
+              , b = d
+              , k = new BABYLON.Vector3(s.x,s.y + b,+s.z)
+              , j = new BABYLON.Ray(k,c,_)
+              , $ = (et = this.avatarManager.sceneManager) == null ? void 0 : et.getGround(e);
+            if (!$ || $.length <= 0)
+                return log$F.warn(`\u89D2\u8272 id= ${this.id} \u627E\u4E0D\u5230\u5730\u9762\uFF0C\u5F53\u524D\u9AD8\u5EA6\u4E3A\u4E0B\u53D1\u9AD8\u5EA6`),
+                i(0);
+            let _e = j.intersectsMeshes($);
+            if (_e.length > 0)
+                return i(_e[0].distance - b);
+            if (c.y = 1,
+            _e = j.intersectsMeshes($),
+            _e.length > 0)
+                return i(-(_e[0].distance - b))
         }
         )
     }
@@ -312,17 +354,23 @@ export default class XAvatar {
     scaleBbox(e) {
         this.bbComponent.bbox && this.bbComponent.bbox.scale(e)
     }
-    rotateTo(e, t, r) {
-        return this.stateMachine.rotateTo(this, e, t, r)
+    rotateTo(e, i, o) {
+        return this.stateMachine.rotateTo(this, e, i, o)
     }
-    faceTo(e, t) {
-        return this.stateMachine.lookAt(this, e, t)
+    faceTo(e, i) {
+        return this.stateMachine.lookAt(this, e, i)
     }
     removeObserver() {
         this.stateMachine.disposeObsever()
     }
-    move(e, t, r, n, o) {
-        return this.stateMachine.moveTo(this, e, t, r, n, o)
+    moveHermite(e, i, o, s, c, d) {
+        return this.stateMachine.moveToHermite(this, e, i, o, s, c, d)
+    }
+    moveCardinal(e, i, o, s, c, d, _=!1) {
+        return this.stateMachine.moveToCardinal(this, e, i, o, s, c, d, _)
+    }
+    move(e, i, o, s, c, d=!1) {
+        return this.stateMachine.moveTo(this, e, i, o, s, c, d)
     }
     initNameboard(e=1) {
         return this.bbComponent.initNameboard(this, e)
@@ -330,59 +378,59 @@ export default class XAvatar {
     initBubble(e=1) {
         return this.bbComponent.initBubble(this, e)
     }
-    say(e, {id: t, isUser: r, background: n, font: o="Arial", fontsize: a=38, fontcolor: s="#ffffff", fontstyle: l="bold", linesize: u=22, linelimit: c, offsets: h={
+    say(e, {id: i, isUser: o, background: s, font: c="Arial", fontsize: d=38, fontcolor: _="#ffffff", fontstyle: b="bold", linesize: k=22, linelimit: j, offsets: $={
         x: 0,
         y: 0,
         z: 40
-    }, scale: f=this._avatarScale, compensationZ: d=11.2, reregistAnyway: _=!0}) {
+    }, scale: _e=this._avatarScale, compensationZ: et=11.2, reregistAnyway: tt=!0}) {
         return this.bbComponent.say(this, e, {
-            id: t,
-            isUser: r,
-            background: n,
-            font: o,
-            fontsize: a,
-            fontcolor: s,
-            fontstyle: l,
-            linesize: u,
-            linelimit: c,
-            offsets: h,
-            scale: f,
-            compensationZ: d,
-            reregistAnyway: _
+            id: i,
+            isUser: o,
+            background: s,
+            font: c,
+            fontsize: d,
+            fontcolor: _,
+            fontstyle: b,
+            linesize: k,
+            linelimit: j,
+            offsets: $,
+            scale: _e,
+            compensationZ: et,
+            reregistAnyway: tt
         })
     }
     silent() {
         return this.bbComponent.silent()
     }
-    setNickName(e, {id: t, isUser: r, background: n, font: o="Arial", fontsize: a=40, fontcolor: s="#ffffff", fontstyle: l="bold", linesize: u=22, linelimit: c, offsets: h={
+    setNickName(e, {id: i, isUser: o, background: s, font: c="Arial", fontsize: d=40, fontcolor: _="#ffffff", fontstyle: b="bold", linesize: k=22, linelimit: j, offsets: $={
         x: 0,
         y: 0,
         z: 15
-    }, scale: f=this._avatarScale, compensationZ: d=0, reregistAnyway: _=!1}) {
+    }, scale: _e=this._avatarScale, compensationZ: et=0, reregistAnyway: tt=!1}) {
         return this.bbComponent.setNickName(this, e, {
-            id: t,
-            isUser: r,
-            background: n,
-            font: o,
-            fontsize: a,
-            fontcolor: s,
-            fontstyle: l,
-            linesize: u,
-            linelimit: c,
-            offsets: h,
-            scale: f,
-            compensationZ: d,
-            reregistAnyway: _
+            id: i,
+            isUser: o,
+            background: s,
+            font: c,
+            fontsize: d,
+            fontcolor: _,
+            fontstyle: b,
+            linesize: k,
+            linelimit: j,
+            offsets: $,
+            scale: _e,
+            compensationZ: et,
+            reregistAnyway: tt
         })
     }
-    generateButtons(e=null, t=this._avatarScale, r=85) {
-        return this.bbComponent.generateButtons(this, e, t, r)
+    generateButtons(e=null, i=this._avatarScale, o=85) {
+        return this.bbComponent.generateButtons(this, e, i, o)
     }
     clearButtons() {
         return this.bbComponent.clearButtons()
     }
-    attachExtraProp(e, t, r, n) {
-        return this.component.addDecoComp(this, e, t, r, n)
+    attachExtraProp(e, i, o, s) {
+        return this.component.addDecoComp(this, e, i, o, s)
     }
     showExtra(e) {
         return this.component.showExtra(e)
@@ -394,24 +442,24 @@ export default class XAvatar {
         return this.component.disposeExtra()
     }
     getSkeletonPositionByName(e) {
-        var t;
+        var i;
         if (this.skeleton) {
-            const r = this.skeleton.bones.find(n=>n.name.replace("Clone of ", "") == e);
-            if (r && r.getTransformNode() && ((t = r.getTransformNode()) == null ? void 0 : t.position)) {
-                const n = r.getTransformNode().position;
+            const o = this.skeleton.bones.find(s=>s.name.replace("Clone of ", "") == e);
+            if (o && o.getTransformNode() && ((i = o.getTransformNode()) == null ? void 0 : i.position)) {
+                const s = o.getTransformNode().position;
                 return xversePosition2Ue4({
-                    x: n.x,
-                    y: n.y,
-                    z: n.z
+                    x: s.x,
+                    y: s.y,
+                    z: s.z
                 })
             }
         }
     }
-    shootTo(e, t, r=2, n=10, o={
+    shootTo(e, i, o=2, s=10, c={
         x: 0,
         y: 0,
         z: 150
     }) {
-        return this.stateMachine.sendObjectTo(this, e, t, r, n, o)
+        return this.stateMachine.sendObjectTo(this, e, i, o, s, c)
     }
 }