lvjincheng 3 лет назад
Родитель
Сommit
0adcd2a8a7
1 измененных файлов с 116 добавлено и 2 удалено
  1. 116 2
      Assets/BowArrow/Scenes/GameChallengeScene/Wolf.cs

+ 116 - 2
Assets/BowArrow/Scenes/GameChallengeScene/Wolf.cs

@@ -15,13 +15,16 @@ public class Wolf : TargetAnimal
 
     void Awake()
     {
+        wolfSet.Add(this);
         state = new State();
         ap = GetComponent<AnimationPlayer>();
         agent = GetComponent<NavMeshAgent>();
+        this.onlineHandler.InitOnAwake(this);
     }
 
     void Start()
     {
+        if (onlineHandler.isMirror) return;
         initAniListener();
     }
     static int _avoidancePriority = 0;
@@ -38,6 +41,12 @@ public class Wolf : TargetAnimal
 
     void Update()
     {
+        this.onlineHandler.Update();
+        if (this.onlineHandler.isMirror) {
+            UpdateOutline();
+            return;
+        }
+        //寻路过程监测
         if (HasCloseToDestination()) {
             OnReachDestination();
         }
@@ -47,13 +56,16 @@ public class Wolf : TargetAnimal
     }
 
     void OnDestroy() {
+        wolfSet.Remove(this);
         WolfActGrid.ins.areaMatrix.releaseOccupy(this);
     }
 
     //可选皮肤材质
     [SerializeField] Material[] materials;
     [SerializeField] Material[] outlineMaterials;
+    [System.NonSerialized] public int colorType = -1;
     public void ChangeColorByType(int type) {
+        colorType = type;
         if (skinnedMeshRenderer == null) skinnedMeshRenderer = GetComponentInChildren<SkinnedMeshRenderer>();
         baseMaterial = materials[type - 1];
         outlineMaterial = outlineMaterials[type - 1];
@@ -85,6 +97,14 @@ public class Wolf : TargetAnimal
     {
         arrow.Head().position = hitPoint + arrow.transform.forward * 0.1f;
         arrow.Hit();
+        if (onlineHandler.isMirror) {
+            onlineHandler.onHitData = onlineHandler.uid.ToString() + "," + partName;
+            return;
+        }
+        OnHitLogic(arrow, partName);
+    }
+
+    public void OnHitLogic(Arrow arrow, string partName) {
         int hurtValue = 0;
         if (partName == "Leg" || partName == "Tail") {
             hurtValue = 2;
@@ -106,12 +126,20 @@ public class Wolf : TargetAnimal
 
     void Die(Arrow arrow) {
         if (state.dead) return;
-        arrow.onDoNextShoot += delegate() { Destroy(this.gameObject); };
+        if (arrow != null) {
+            arrow.onDoNextShoot += delegate() {
+                Destroy(this.gameObject);
+            };
+        } else {
+            //需要借住关卡的GameMode来清除
+            onlineHandler.onDoNextShootWillDestroy = true;
+        }
         state.ResetActionState();
         state.dead = true;
         this.agent.enabled = false;
         onDie?.Invoke(this);
         AudioMgr.ins.PlayAnimalEffect("wolf_die", AudioMgr.GetAudioSource(this.gameObject));
+        this.onlineHandler.deadID++;
     }
 
     void Hurt() {
@@ -129,6 +157,7 @@ public class Wolf : TargetAnimal
             } 
         }
         AudioMgr.ins.PlayAnimalEffect("wolf_injured", AudioMgr.GetAudioSource(this.gameObject));
+        this.onlineHandler.injuredID++;
     }
 
     void SetAgentStopped(bool value) { //要捕捉错误,不然会报错
@@ -469,7 +498,7 @@ public class Wolf : TargetAnimal
     }
 
     //动画播放
-    int curAnimIndex = 0;
+    [System.NonSerialized] public int curAnimIndex = -1;
     void playAniStay() {
         ap.play(curAnimIndex = 7, WrapMode.Loop);
     }
@@ -587,4 +616,89 @@ public class Wolf : TargetAnimal
     //委托
     public System.Action<Wolf> onDie;
     public System.Action<Wolf, int> onAttack;
+
+    #region 联机附加部分
+        public static HashSet<Wolf> wolfSet = new HashSet<Wolf>();
+        public OnlineHandler onlineHandler = new OnlineHandler();
+        public class OnlineHandler {
+            Wolf animal;
+            public int uid;
+            public bool isMirror;
+            public WolfSyncData outputSyncData;
+            public void InitOnAwake(Wolf animal) {
+                this.animal = animal;
+                if (isMirror) {
+                    GameObject.Destroy(this.animal.agent);
+                }
+            }
+            public void Update() {
+                if (isMirror) {
+                    if (inputSyncData == null) return;
+                    if (!hasUpdateBySyncData) {
+                        this.animal.transform.position = syncPosition;
+                        this.animal.transform.rotation = syncRotation;
+                        hasUpdateBySyncData = true;
+                    } else {
+                        this.animal.transform.position = Vector3.Lerp(this.animal.transform.position, syncPosition, Time.deltaTime * 15);
+                        this.animal.transform.rotation = Quaternion.Lerp(this.animal.transform.rotation, syncRotation, Time.deltaTime * 15);     
+                    }
+                    if (this.animal.colorType != inputSyncData.ct) {
+                        this.animal.ChangeColorByType(inputSyncData.ct);
+                    }
+                    if (inputSyncData.ai != this.animal.curAnimIndex) {
+                        this.animal.curAnimIndex = inputSyncData.ai;
+                        if (this.animal.isAniStay()) {
+                            this.animal.playAniStay();
+                        } else if (this.animal.isAniMoveSlowly()) {
+                            this.animal.playAniMoveSlowly();
+                        } else if (this.animal.isAniMoveQuickly()) {
+                            this.animal.playAniMoveQuickly();
+                        } else if (this.animal.isAniAmbush()) {
+                            this.animal.playAniAmbush();
+                        } else if (this.animal.curAnimIndex >= 0) {
+                            this.animal.ap.play(this.animal.curAnimIndex, WrapMode.Once);
+                        }
+                    }
+                    if (inputSyncData.ii != injuredID) {
+                        injuredID = inputSyncData.ii;
+                        AudioMgr.ins.PlayAnimalEffect("wolf_injured", AudioMgr.GetAudioSource(this.animal.gameObject));
+                    }
+                    if (inputSyncData.di != deadID) {
+                        deadID = inputSyncData.di;
+                        AudioMgr.ins.PlayAnimalEffect("wolf_die", AudioMgr.GetAudioSource(this.animal.gameObject));
+                    }
+                    return;
+                }
+                if (GlobalData.pkMatchType == PKMatchType.OnlinePK) {
+                    if (outputSyncData == null) outputSyncData = new WolfSyncData();
+                    outputSyncData.SetData(this.animal);
+                }
+            }
+            private WolfSyncData _inputSyncData;
+            public WolfSyncData inputSyncData {
+                get {
+                    return _inputSyncData;
+                }
+                set {
+                    _inputSyncData = value;
+                    uid = value.id;
+                    syncRotation.x = value.rx;
+                    syncRotation.y = value.ry;
+                    syncRotation.z = value.rz;
+                    syncRotation.w = value.rw;
+                    syncPosition.x = value.px;
+                    syncPosition.y = value.py;
+                    syncPosition.z = value.pz;
+                }
+            }
+            private Quaternion syncRotation;
+            private Vector3 syncPosition;
+            private bool hasUpdateBySyncData = false;
+            public bool isInvalid = false;  //外部运算用的
+            public string onHitData = null;
+            public bool onDoNextShootWillDestroy = false;
+            public int injuredID = 0;
+            public int deadID = 0;
+        }
+    #endregion
 }