Wolf.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5. using DG.Tweening;
  6. /* 动物组件-野狼 */
  7. public class Wolf : TargetAnimal
  8. {
  9. //动画播放器
  10. AnimationPlayer ap;
  11. //寻路代理
  12. NavMeshAgent agent;
  13. //血量
  14. [System.NonSerialized] public int hp = 1;
  15. void Awake()
  16. {
  17. wolfSet.Add(this);
  18. state = new State();
  19. ap = GetComponent<AnimationPlayer>();
  20. agent = GetComponent<NavMeshAgent>();
  21. this.onlineHandler.InitOnAwake(this);
  22. }
  23. void Start()
  24. {
  25. if (onlineHandler.isMirror) return;
  26. initAniListener();
  27. }
  28. static int _avoidancePriority = 0;
  29. static int avoidancePriority {
  30. get {
  31. if (_avoidancePriority < 50) {
  32. _avoidancePriority++;
  33. } else {
  34. _avoidancePriority = 1;
  35. }
  36. return _avoidancePriority;
  37. }
  38. }
  39. void Update()
  40. {
  41. this.onlineHandler.Update();
  42. if (this.onlineHandler.isMirror) {
  43. UpdateOutline();
  44. return;
  45. }
  46. //寻路过程监测
  47. if (HasCloseToDestination()) {
  48. OnReachDestination();
  49. }
  50. UpdateAutoStrategy();
  51. UpdateAction();
  52. UpdateOutline();
  53. }
  54. void OnDestroy() {
  55. wolfSet.Remove(this);
  56. WolfActGrid.ins.areaMatrix.releaseOccupy(this);
  57. }
  58. //可选皮肤材质
  59. [SerializeField] Material[] materials;
  60. [SerializeField] Material[] outlineMaterials;
  61. [System.NonSerialized] public int colorType = -1;
  62. public void ChangeColorByType(int type) {
  63. colorType = type;
  64. if (skinnedMeshRenderer == null) skinnedMeshRenderer = GetComponentInChildren<SkinnedMeshRenderer>();
  65. baseMaterial = materials[type - 1];
  66. outlineMaterial = outlineMaterials[type - 1];
  67. skinnedMeshRenderer.material = baseMaterial;
  68. }
  69. Material baseMaterial;
  70. Material outlineMaterial;
  71. SkinnedMeshRenderer skinnedMeshRenderer = null;
  72. bool hasOutLine = false;
  73. void UpdateOutline() {
  74. Camera camera = Camera.main;
  75. if (!camera) return;
  76. float distance = Vector3.Distance(this.transform.position, camera.transform.position);
  77. if (distance > 15) {
  78. if (!hasOutLine) {
  79. hasOutLine = true;
  80. skinnedMeshRenderer.material = outlineMaterial;
  81. }
  82. } else {
  83. if (hasOutLine) {
  84. hasOutLine = false;
  85. skinnedMeshRenderer.material = baseMaterial;
  86. }
  87. }
  88. }
  89. public override void OnHit(Arrow arrow, Vector3 hitPoint, string partName)
  90. {
  91. arrow.Head().position = hitPoint + arrow.transform.forward * 0.1f;
  92. arrow.Hit();
  93. if (onlineHandler.isMirror) {
  94. onlineHandler.onHitData = onlineHandler.uid.ToString() + "," + partName;
  95. return;
  96. }
  97. OnHitLogic(arrow, partName);
  98. }
  99. public void OnHitLogic(Arrow arrow, string partName) {
  100. int hurtValue = 0;
  101. if (partName == "Leg" || partName == "Tail") {
  102. hurtValue = 2;
  103. }
  104. else if (partName == "Body") {
  105. hurtValue = 3;
  106. }
  107. else if (partName == "Head") {
  108. hurtValue = 10;
  109. }
  110. hp -= hurtValue;
  111. GameEventCenter.ins.onTargetAnimalHurt?.Invoke(this, hurtValue);
  112. if (hp > 0) {
  113. Hurt();
  114. } else {
  115. Die(arrow);
  116. }
  117. }
  118. void Die(Arrow arrow) {
  119. if (state.dead) return;
  120. if (arrow != null) {
  121. arrow.onDoNextShoot += delegate() {
  122. Destroy(this.gameObject);
  123. };
  124. } else {
  125. //需要借住关卡的GameMode来清除
  126. onlineHandler.onDoNextShootWillDestroy = true;
  127. }
  128. state.ResetActionState();
  129. state.dead = true;
  130. this.agent.enabled = false;
  131. onDie?.Invoke(this);
  132. AudioMgr.ins.PlayAnimalEffect("wolf_die", AudioMgr.GetAudioSource(this.gameObject));
  133. this.onlineHandler.deadID++;
  134. }
  135. void Hurt() {
  136. if (!state.attacking) {
  137. if (Random.value < 0.2f) {
  138. CancelLockTarget();
  139. state.ResetActionState();
  140. state.lockingTarget = true;
  141. needAmbush = false;
  142. } else {
  143. //未锁定目标阶段时被击退,因为可能会再次满足z路径条件,因此需要重置Z路径记录
  144. if (!state.lockingTarget) ResetZPathRecord();
  145. CancelLockTarget(1);
  146. RunAwayFromHunter();
  147. }
  148. }
  149. AudioMgr.ins.PlayAnimalEffect("wolf_injured", AudioMgr.GetAudioSource(this.gameObject));
  150. this.onlineHandler.injuredID++;
  151. }
  152. void SetAgentStopped(bool value) { //要捕捉错误,不然会报错
  153. try { this.agent.isStopped = value; } catch (System.Exception) {}
  154. }
  155. //启动寻路
  156. void SetDestination(Vector3 pos) {
  157. state.ResetActionState();
  158. state.moving = true;
  159. SetAgentStopped(false);
  160. if (useRunbackPoint) {
  161. useRunbackPoint = false;
  162. WolfActGrid.ins.areaMatrix.releaseOccupy(this);
  163. } else {
  164. bool occupySuccess = WolfActGrid.ins.areaMatrix.occupyPos(pos, this);
  165. if (!occupySuccess) {
  166. pos = transform.position;
  167. }
  168. }
  169. this.agent.destination = pos;
  170. }
  171. //寻路结束
  172. void OnReachDestination() {
  173. if (!state.moving) return;
  174. SetAgentStopped(true);
  175. state.ResetActionState();
  176. }
  177. //是否已经接近目的地(寻路完成判断)
  178. bool HasCloseToDestination() {
  179. if (!state.moving) return true;
  180. if (Vector3.Distance(this.agent.nextPosition, this.agent.destination) < 0.25f) {
  181. return true;
  182. }
  183. if (state.movingTime > 0.1f && this.agent.velocity.magnitude < 0.05f) {
  184. return true;
  185. }
  186. return false;
  187. }
  188. int lastAutoType;
  189. float willStayTime;
  190. bool willAttack = false;
  191. bool hasRunAround = false;
  192. int needRunAroundCount = 0;
  193. bool hasRunToHunter = false;
  194. //取消锁定状态
  195. //cancelType==1时,表示触发逃跑
  196. void CancelLockTarget(int cancelType = 0) {
  197. if (!state.lockingTarget) return;
  198. state.lockingTarget = false;
  199. if (cancelType == 1) state.lockingTarget = true;
  200. RandomWillStayTime();
  201. willAttack = false;
  202. hasRunAround = false;
  203. if (cancelType == 1) hasRunAround = true;
  204. needRunAroundCount = 0;
  205. if (cancelType == 1) needRunAroundCount = 2;
  206. hasRunToHunter = false;
  207. if (cancelType == 1) hasRunToHunter = true;
  208. }
  209. //帧更新逻辑-自动策略
  210. void UpdateAutoStrategy() {
  211. if (state.dead) return;
  212. if (state.lockingTarget) {
  213. // if (state.attacking) {
  214. // this.transform.LookAt(this.hunterPosition);
  215. // }
  216. if (!hasRunToHunter) {
  217. hasRunToHunter = true;
  218. RunToAttackHunter(true);
  219. willAttack = true;
  220. return;
  221. }
  222. if (!state.moving && !state.attacking) {
  223. if (willAttack) {
  224. willAttack = false;
  225. Attack();
  226. needRunAroundCount = 3;
  227. return;
  228. }
  229. if (needRunAroundCount > 0) {
  230. needRunAroundCount--;
  231. useRunbackPoint = !hasRunAround;
  232. RunAroundHunter(!hasRunAround);
  233. hasRunAround = true;
  234. return;
  235. }
  236. if (hasRunAround) {
  237. hasRunAround = false;
  238. RunToAttackHunter(false);
  239. }
  240. willAttack = true;
  241. return;
  242. }
  243. return;
  244. }
  245. //以下为未锁定目标时的自动策略
  246. if (state.staying) {
  247. if (state.stayingTime > willStayTime) {
  248. MoveSlowlyInZPath();
  249. }
  250. return;
  251. }
  252. if (state.moving) {
  253. lastAutoType = 2;
  254. }
  255. if (!state.staying && !state.moving) {
  256. if (lastAutoType == 1 || lastAutoType == 0) {
  257. MoveSlowlyInZPath();
  258. } else {
  259. if (!canCreateZPath && zPathPoints.Count == 0) {
  260. MoveSlowlyInZPath();//内含判断,状态将会转化为锁定目标
  261. } else {
  262. RandomWillStayTime();
  263. Stay(true);
  264. }
  265. }
  266. }
  267. }
  268. void RandomWillStayTime() {
  269. this.willStayTime = Random.value * 4 + 2;
  270. // this.willStayTime = 0;
  271. }
  272. void LookAtHunter() {
  273. this.transform.LookAt(hunterPosition);
  274. }
  275. bool needAmbush = true;
  276. //攻击
  277. void Attack() {
  278. state.ResetActionState();
  279. state.attacking = true;
  280. curAnimIndex = -1; /*注意:这样可避免狼两次使用同一攻击动作而第二次无法播放的情况 */
  281. int hurtValue = 2;
  282. if (canAttackID == "A") {
  283. int avoidancePriority = this.agent.avoidancePriority;
  284. float baseoffset = this.agent.baseOffset;
  285. Vector3 hunterPos = hunterPosition;
  286. Vector3 jumpPoint = default; //起跳点
  287. Vector3 landPoint = default; //落地点
  288. Vector3 displace = default; //位移
  289. Sequence seq = DOTween.Sequence();
  290. //伏击动作 + 转头
  291. Quaternion ambushQuaStart = transform.rotation;
  292. Vector3 rotateDir = hunterPos - transform.position; rotateDir.y = 0;
  293. Quaternion ambushQuaEnd = Quaternion.FromToRotation(Vector3.forward, rotateDir);
  294. float needRotateAngle = Quaternion.Angle(ambushQuaStart, ambushQuaEnd);
  295. this.agent.avoidancePriority = 0;
  296. if (needRotateAngle > 20) {
  297. state.ambushRotating = true;
  298. seq.Append(DOTween.To(() => 0f, value => {
  299. transform.rotation = Quaternion.Lerp(ambushQuaStart, ambushQuaEnd, value);
  300. }, 1f, needRotateAngle / 180f * 0.5f));
  301. } else {
  302. ambushQuaEnd = ambushQuaStart;
  303. }
  304. seq.AppendCallback(delegate() {
  305. state.ambushRotating = false;
  306. if (needAmbush) state.ambushing = true;
  307. });
  308. if (needAmbush) {
  309. seq.Append(DOTween.To(() => 0f, value => {
  310. this.transform.rotation = ambushQuaEnd;
  311. }, 1f, 2f));
  312. }
  313. seq.AppendCallback(delegate() {
  314. if (state.dead) {
  315. seq.Kill();
  316. return;
  317. }
  318. needAmbush = true;
  319. state.ambushing = false;
  320. state.attackA = true;
  321. #region //起点和终点计算
  322. jumpPoint = this.transform.position;
  323. hunterPos.y = jumpPoint.y;
  324. Vector3 deltaPointer = jumpPoint - hunterPos;
  325. landPoint = hunterPos + deltaPointer.normalized * 1f;
  326. displace = landPoint - jumpPoint;
  327. #endregion
  328. });
  329. //跳跃
  330. seq.Append(DOTween.To(() => 0f, value => {
  331. this.transform.position = jumpPoint + displace * value;
  332. LookAtHunter();
  333. if (value < 0.5) {
  334. this.agent.baseOffset = baseoffset + value;
  335. } else {
  336. this.agent.baseOffset = baseoffset + (1 - value);
  337. if (!state.dead && state.attackA) {
  338. state.attackA = false;
  339. playAniJumpDown();
  340. }
  341. }
  342. }, 1f, 0.8f));
  343. seq.AppendCallback(delegate() {
  344. this.agent.avoidancePriority = avoidancePriority;
  345. this.transform.position = landPoint;
  346. LookAtHunter();
  347. if (!state.dead) onAttack?.Invoke(this, hurtValue);
  348. });
  349. seq.Append(DOTween.To(() => 0f, value => {
  350. this.transform.position = landPoint;
  351. LookAtHunter();
  352. }, 1f, 0.3f));
  353. seq.AppendCallback(delegate() {
  354. if (!state.dead) stopAniJumpDown(); //停止动画,则动画自带的位移也停止变化
  355. this.transform.position = landPoint;
  356. LookAtHunter();
  357. });
  358. seq.Append(DOTween.To(() => 0f, value => {
  359. this.transform.position = landPoint;
  360. LookAtHunter();
  361. }, 1f, 0.1f)); //通过dotween是它不被动画的位移影响;
  362. seq.AppendCallback(delegate() {
  363. state.ResetActionState();
  364. });
  365. } else if (canAttackID == "B") {
  366. state.attackB = true;
  367. hurtValue = 3;
  368. onAttack?.Invoke(this, hurtValue);
  369. }
  370. }
  371. //逃跑远离猎人
  372. void RunAwayFromHunter()
  373. {
  374. Vector3 backVec = GetPointerHunterToMe();
  375. SetDestination(transform.position + backVec.normalized * 6);
  376. state.moveQuickly = true;
  377. this.agent.speed = 5f;
  378. }
  379. //跑到能攻击玩家的坐标点
  380. string canAttackID = null;
  381. void RunToAttackHunter(bool quickly) {
  382. float needDistance;
  383. Vector3 displace = GetPointerHunterToMe().normalized;
  384. // if (Random.value < 0.33f) {
  385. // needDistance = 2;
  386. // canAttackID = "B";
  387. // } else {
  388. needDistance = 8.5f;
  389. canAttackID = "A";
  390. // }
  391. Vector3 newPos = animalsBaseT.position + displace * needDistance;
  392. SetDestination(newPos);
  393. state.moveSlowly = !quickly;
  394. state.moveQuickly = quickly;
  395. agent.speed = quickly ? 5f : 0.8f;
  396. }
  397. //在敌人身边奔跑徘徊
  398. bool useRunbackPoint = false;
  399. void RunAroundHunter(bool quickly) {
  400. Vector3 newPos;
  401. if (useRunbackPoint) {
  402. newPos = WolfActGrid.ins.GetRunBackPointAfterPounce();
  403. } else {
  404. float baseDistance = 10;
  405. float baseDistanceMoveRange = 5;
  406. Vector3 standardPointer = animalsBaseT.forward;
  407. Vector3 pointerWithLen = standardPointer.normalized * (baseDistance + Random.value * baseDistanceMoveRange);
  408. pointerWithLen = Quaternion.AngleAxis(Random.Range(-20f, 23f), Vector3.up) * pointerWithLen;
  409. newPos = animalsBaseT.position + pointerWithLen;
  410. }
  411. SetDestination(newPos);
  412. state.moveSlowly = !quickly;
  413. state.moveQuickly = quickly;
  414. agent.speed = quickly ? 5f : 0.8f;
  415. }
  416. //Z字型路径
  417. Queue<Vector3> zPathPoints = new Queue<Vector3>();
  418. bool canCreateZPath = true;
  419. void ResetZPathRecord() {
  420. zPathPoints.Clear();
  421. canCreateZPath = true;
  422. }
  423. void MoveSlowlyInZPath() {
  424. if (zPathPoints.Count > 0) {
  425. SetDestination(zPathPoints.Dequeue());
  426. state.moveSlowly = true;
  427. agent.speed = 0.8f;
  428. return;
  429. }
  430. if (!canCreateZPath) {
  431. state.lockingTarget = true;
  432. return;
  433. }
  434. //构建Z字型路径
  435. zPathPoints.Clear();
  436. Vector3 standardVec = animalsBaseT.forward;
  437. Vector3 hunterPos = animalsBaseT.position;
  438. Vector3 myPos = this.transform.position;
  439. float minDistance = 10;
  440. float maxDistance = 25;
  441. while (true) {
  442. myPos.y = hunterPos.y;
  443. float distance = Vector3.Distance(myPos, hunterPos);
  444. if (distance > minDistance) {
  445. Vector3 vecToMyPos = myPos - hunterPos;
  446. //判断位置在猎人左边还是右边
  447. float crossY = Vector3.Cross(standardVec, vecToMyPos).y;
  448. float nextAngle;
  449. if (crossY >= 0) { //在右边
  450. nextAngle = Random.Range(-20f, -10f);//下次就向左边走
  451. } else { //在左边
  452. nextAngle = Random.Range(13f, 22f);;//下次就向右边走
  453. }
  454. float nextDistance = distance - Random.Range(3.5f, 4.5f);
  455. if (nextDistance > maxDistance) {
  456. nextDistance = maxDistance;
  457. }
  458. if (nextDistance < minDistance) {
  459. nextDistance = minDistance;
  460. }
  461. if (Mathf.Abs(nextDistance - distance) < 1.2f) {
  462. break;
  463. }
  464. Vector3 displace = (Quaternion.AngleAxis(nextAngle, Vector3.up) * standardVec) * nextDistance;
  465. myPos = hunterPos + displace;
  466. zPathPoints.Enqueue(myPos);
  467. // Debug.Log(Quaternion.AngleAxis(-180f, Vector3.up) * (myPos - hunterPos));
  468. } else {
  469. break;
  470. }
  471. }
  472. canCreateZPath = false;
  473. }
  474. //停留
  475. void Stay(bool correct = false) {
  476. if (state.moving || correct) {
  477. this.agent.destination = this.agent.nextPosition;
  478. SetAgentStopped(true);
  479. }
  480. state.ResetActionState();
  481. state.staying = true;
  482. }
  483. //动画播放
  484. [System.NonSerialized] public int curAnimIndex = -1;
  485. void playAniStay() {
  486. ap.play(curAnimIndex = 7, WrapMode.Loop);
  487. }
  488. bool isAniStay() {
  489. return curAnimIndex == 7;
  490. }
  491. void playAniMoveSlowly() {
  492. ap.play(curAnimIndex = 5, WrapMode.Loop);
  493. }
  494. bool isAniMoveSlowly() {
  495. return curAnimIndex == 5;
  496. }
  497. void playAniMoveQuickly() {
  498. ap.play(curAnimIndex = 4, WrapMode.Loop);
  499. }
  500. bool isAniMoveQuickly() {
  501. return curAnimIndex == 4;
  502. }
  503. void playAniAmbush() {
  504. ap.play(curAnimIndex = 3, WrapMode.Loop);
  505. }
  506. bool isAniAmbush() {
  507. return curAnimIndex == 3;
  508. }
  509. void playAniAttakA() {
  510. ap.play(curAnimIndex = 1, WrapMode.Once);
  511. }
  512. bool isAniAttakA() {
  513. return curAnimIndex == 1;
  514. }
  515. void playAniJumpDown() {
  516. ap.play(curAnimIndex = 6, WrapMode.Once);
  517. }
  518. void stopAniJumpDown() {
  519. ap.StopAnimation(6);
  520. }
  521. void playAniAttakB() {
  522. ap.play(curAnimIndex = 0, WrapMode.Once);
  523. }
  524. bool isAniAttakB() {
  525. return curAnimIndex == 0;
  526. }
  527. void playAniDie() {
  528. ap.play(curAnimIndex = 2, WrapMode.Once);
  529. }
  530. bool isAniDie() {
  531. return curAnimIndex == 2;
  532. }
  533. void initAniListener() {
  534. this.ap.completeCallback = delegate(AnimationPlayerCompleteResult res) {
  535. if (res.index == 0) {
  536. this.state.ResetActionState();
  537. }
  538. };
  539. }
  540. //帧更新逻辑-通过状态更新动作动画
  541. void UpdateAction() {
  542. if (state.staying) {
  543. state.stayingTime += Time.deltaTime;
  544. if (!isAniStay()) playAniStay();
  545. }
  546. else if (state.moving) {
  547. state.movingTime += Time.deltaTime;
  548. if (state.moveSlowly && !isAniMoveSlowly()) playAniMoveSlowly();
  549. if (state.moveQuickly && !isAniMoveQuickly()) playAniMoveQuickly();
  550. }
  551. else if (state.attacking) {
  552. if (state.ambushRotating && !isAniMoveQuickly()) playAniMoveQuickly();
  553. if (state.ambushing && !isAniAmbush()) playAniAmbush();
  554. if (state.attackA && !isAniAttakA()) playAniAttakA();
  555. if (state.attackB && !isAniAttakB()) playAniAttakB();
  556. }
  557. else if (state.dead) {
  558. if (!isAniDie()) playAniDie();
  559. }
  560. }
  561. //状态
  562. [System.Serializable]
  563. public class State {
  564. //动作区
  565. public bool staying = false;
  566. public bool moving = false;
  567. public bool moveSlowly = false; //慢走
  568. public bool moveQuickly = false; //跑动
  569. public bool attacking = false;
  570. public bool ambushRotating = false; //扑击前的转身
  571. public bool ambushing = false; //扑击前的伏击状态
  572. public bool attackA = false; //扑击
  573. public bool attackB = false; //撕咬
  574. public bool dead = false;
  575. public float stayingTime = 0;
  576. public float movingTime = 0;
  577. //特定区
  578. public bool lockingTarget = false; //锁定目标,只有锁定目标后,才能调用攻击接口,这是给自己的规定
  579. //重置动作区状态
  580. public void ResetActionState() {
  581. this.staying = false;
  582. this.moving = false;
  583. this.moveSlowly = false;
  584. this.moveQuickly = false;
  585. this.attacking = false;
  586. this.ambushRotating = false;
  587. this.ambushing = false;
  588. this.attackA = false;
  589. this.attackB = false;
  590. this.dead = false;
  591. this.stayingTime = 0;
  592. this.movingTime = 0;
  593. }
  594. }
  595. [SerializeField] public State state;
  596. //委托
  597. public System.Action<Wolf> onDie;
  598. public System.Action<Wolf, int> onAttack;
  599. #region 联机附加部分
  600. public static HashSet<Wolf> wolfSet = new HashSet<Wolf>();
  601. public OnlineHandler onlineHandler = new OnlineHandler();
  602. public class OnlineHandler {
  603. Wolf animal;
  604. public int uid;
  605. public bool isMirror;
  606. public WolfSyncData outputSyncData;
  607. public void InitOnAwake(Wolf animal) {
  608. this.animal = animal;
  609. if (isMirror) {
  610. GameObject.Destroy(this.animal.agent);
  611. }
  612. }
  613. public void Update() {
  614. if (isMirror) {
  615. if (inputSyncData == null) return;
  616. if (!hasUpdateBySyncData) {
  617. this.animal.transform.position = syncPosition;
  618. this.animal.transform.rotation = syncRotation;
  619. hasUpdateBySyncData = true;
  620. } else {
  621. this.animal.transform.position = Vector3.Lerp(this.animal.transform.position, syncPosition, Time.deltaTime * 15);
  622. this.animal.transform.rotation = Quaternion.Lerp(this.animal.transform.rotation, syncRotation, Time.deltaTime * 15);
  623. }
  624. if (this.animal.colorType != inputSyncData.ct) {
  625. this.animal.ChangeColorByType(inputSyncData.ct);
  626. }
  627. if (inputSyncData.ai != this.animal.curAnimIndex) {
  628. this.animal.curAnimIndex = inputSyncData.ai;
  629. if (this.animal.isAniStay()) {
  630. this.animal.playAniStay();
  631. } else if (this.animal.isAniMoveSlowly()) {
  632. this.animal.playAniMoveSlowly();
  633. } else if (this.animal.isAniMoveQuickly()) {
  634. this.animal.playAniMoveQuickly();
  635. } else if (this.animal.isAniAmbush()) {
  636. this.animal.playAniAmbush();
  637. } else if (this.animal.curAnimIndex >= 0) {
  638. this.animal.ap.play(this.animal.curAnimIndex, WrapMode.Once);
  639. }
  640. }
  641. if (inputSyncData.ii != injuredID) {
  642. injuredID = inputSyncData.ii;
  643. AudioMgr.ins.PlayAnimalEffect("wolf_injured", AudioMgr.GetAudioSource(this.animal.gameObject));
  644. }
  645. if (inputSyncData.di != deadID) {
  646. deadID = inputSyncData.di;
  647. AudioMgr.ins.PlayAnimalEffect("wolf_die", AudioMgr.GetAudioSource(this.animal.gameObject));
  648. }
  649. return;
  650. }
  651. if (GlobalData.pkMatchType == PKMatchType.OnlinePK) {
  652. if (outputSyncData == null) outputSyncData = new WolfSyncData();
  653. outputSyncData.SetData(this.animal);
  654. }
  655. }
  656. private WolfSyncData _inputSyncData;
  657. public WolfSyncData inputSyncData {
  658. get {
  659. return _inputSyncData;
  660. }
  661. set {
  662. _inputSyncData = value;
  663. uid = value.id;
  664. syncRotation.x = value.rx;
  665. syncRotation.y = value.ry;
  666. syncRotation.z = value.rz;
  667. syncRotation.w = value.rw;
  668. syncPosition.x = value.px;
  669. syncPosition.y = value.py;
  670. syncPosition.z = value.pz;
  671. }
  672. }
  673. private Quaternion syncRotation;
  674. private Vector3 syncPosition;
  675. private bool hasUpdateBySyncData = false;
  676. public bool isInvalid = false; //外部运算用的
  677. public string onHitData = null;
  678. public bool onDoNextShootWillDestroy = false;
  679. public int injuredID = 0;
  680. public int deadID = 0;
  681. }
  682. #endregion
  683. public override int GetOnlineID() {
  684. return onlineHandler.uid;
  685. }
  686. }