ArrowSync.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using DG.Tweening;
  5. public class ArrowSync : MonoBehaviour
  6. {
  7. void Awake()
  8. {
  9. this.transform.Find("Head/_hunse_jian").gameObject.layer = 0;
  10. }
  11. void Update()
  12. {
  13. if (!hasAttachAnything) {
  14. UpdatePosAndRot(Time.deltaTime * 15);
  15. }
  16. }
  17. void UpdatePosAndRot(float t) {
  18. transform.position = Vector3.Lerp(transform.position, position, t);
  19. Head().position = transform.position;
  20. transform.rotation = Quaternion.Lerp(transform.rotation, rotation, t);
  21. }
  22. public Transform Head() {
  23. return transform.Find("Head");
  24. }
  25. #region ---------箭矢特效---------
  26. public void activeEffectCyclone(bool value)
  27. {
  28. this.transform.Find("Head/EF_kuosanquan").gameObject.SetActive(value);
  29. if (!value) return;
  30. ParticleSystemRenderer ps = this.transform.Find("Head/EF_kuosanquan/kuosan").GetComponent<ParticleSystemRenderer>();
  31. ParticleSystemRenderer ps1 = this.transform.Find("Head/EF_kuosanquan/kuosan (1)").GetComponent<ParticleSystemRenderer>();
  32. DOTween.To(() => ps.minParticleSize, value => {
  33. ps.minParticleSize = value;
  34. ps.maxParticleSize = value;
  35. }, 0.4f, 0.6f);
  36. DOTween.To(() => ps1.minParticleSize, value => {
  37. ps1.minParticleSize = value;
  38. ps1.maxParticleSize = value;
  39. }, 0.8f, 0.6f);
  40. }
  41. void activeEffectBomb(bool value)
  42. {
  43. this.transform.Find("Head/EF_baodian").gameObject.SetActive(value);
  44. }
  45. void activeEffectTrail(bool value)
  46. {
  47. this.transform.Find("EF_tuowei").gameObject.SetActive(value);
  48. this.transform.Find("EF_tuowei/Trail").GetComponent<TrailRenderer>().time = 1.6f / mySpeed;
  49. }
  50. #endregion
  51. Quaternion rotation;
  52. Vector3 position;
  53. float mySpeed;
  54. bool hasPlayHitAudio = false;
  55. bool hasPlayHitEffect = false;
  56. string[] hitTargetAnimalInfo;
  57. bool hasAttachAnything = false;//是否附着在什么东西上,比如动物身上,这时候其实就不需要通过同步状态更新坐标了
  58. ArrowCamera arrowCameraComp;
  59. [System.NonSerialized] public bool isHit;
  60. [System.NonSerialized] public bool hasDoneNextShoot;
  61. [System.NonSerialized] public bool canUseSideCamera;
  62. [System.NonSerialized] public bool hasSetSyncData = false; //外部运算用的
  63. public void SetSyncData(SyncData syncData, bool apply = false) {
  64. rotation.x = syncData.rx;
  65. rotation.y = syncData.ry;
  66. rotation.z = syncData.rz;
  67. rotation.w = syncData.rw;
  68. position.x = syncData.px;
  69. position.y = syncData.py;
  70. position.z = syncData.pz;
  71. canUseSideCamera = syncData.cs;
  72. bool oldIsHit = isHit;
  73. isHit = syncData.ih;
  74. hasDoneNextShoot = syncData.dns;
  75. hitTargetAnimalInfo = syncData.htai;
  76. mySpeed = syncData.sp;
  77. if (apply) {
  78. transform.position = position;
  79. transform.rotation = rotation;
  80. if (!isHit || !hasDoneNextShoot) {
  81. //激活镜头
  82. Transform cameraTF = this.transform.Find("Camera");
  83. cameraTF.gameObject.SetActive(true);
  84. arrowCameraComp = cameraTF.gameObject.AddComponent<ArrowCamera>();
  85. arrowCameraComp.SetArrowSync(this);
  86. //射出的声音
  87. AudioMgr.ins.PlayShoot(AudioMgr.GetAudioSource(this.gameObject));
  88. //拖尾
  89. activeEffectTrail(true);
  90. }
  91. else if (isHit) {
  92. hasPlayHitAudio = true;
  93. hasPlayHitEffect = true;
  94. activeEffectCyclone(false);
  95. activeEffectTrail(false);
  96. }
  97. } else {
  98. if (!oldIsHit && isHit) {
  99. if (arrowCameraComp) {
  100. if (arrowCameraComp.arrowCameraTemplate == null) {
  101. isHit = false;
  102. } else if (arrowCameraComp.arrowCameraTemplate.GetType().Equals(typeof(ArrowCameraTemplate3))) {
  103. bool hasBlockByTree = false;
  104. if (syncData.HasArrowCameraTemplate3()) {
  105. hasBlockByTree = syncData.GetArrowCameraTemplate3().Item1;
  106. }
  107. UpdatePosAndRot(1f);
  108. ((ArrowCameraTemplate3) arrowCameraComp.arrowCameraTemplate).beforHitWhenSync(hasBlockByTree);
  109. if (hitTargetAnimalInfo != null) {
  110. int onlineID = int.Parse(hitTargetAnimalInfo[0]);
  111. int partIndex = int.Parse(hitTargetAnimalInfo[1]);
  112. TargetAnimal[] targetAnimals = GameObject.FindObjectsOfType<TargetAnimal>();
  113. foreach (var item in targetAnimals) {
  114. if (item.GetOnlineID() == onlineID) {
  115. this.transform.SetParent(item.targetAnimalParts[partIndex]);
  116. hasAttachAnything = true;
  117. break;
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }
  124. if (syncData.HasArrowCameraTemplate3()) {
  125. if (arrowCameraComp && arrowCameraComp.arrowCameraTemplate.GetType().Equals(typeof(ArrowCameraTemplate3))) {
  126. ((ArrowCameraTemplate3) arrowCameraComp.arrowCameraTemplate).quicklyNextShoot = syncData.GetArrowCameraTemplate3().Item2;
  127. }
  128. }
  129. }
  130. if (isHit && !hasPlayHitAudio) {
  131. hasPlayHitAudio = true;
  132. int hitType = syncData.ht;
  133. if (hitType == 1 || hitType == 2) {
  134. AudioMgr.ins.PlayHit(AudioMgr.GetAudioSource(TargetBody.ins.gameObject));
  135. AudioMgr.ins.PlayCheer(hitType == 1 ? true : false);
  136. } else if (hitType == 4 || hitType == 5) {
  137. AudioMgr.ins.PlayArrowEnter();
  138. } else if (hitType == 3) {
  139. AudioMgr.ins.PlayCheer(false);
  140. }
  141. }
  142. if (isHit && !hasPlayHitEffect) {
  143. hasPlayHitEffect = true;
  144. activeEffectBomb(true);
  145. activeEffectCyclone(false);
  146. activeEffectTrail(false);
  147. }
  148. }
  149. public class SyncData {
  150. [System.NonSerialized] public bool inited;
  151. static int autoID;
  152. public int id;
  153. public float rx;
  154. public float ry;
  155. public float rz;
  156. public float rw;
  157. public float px;
  158. public float py;
  159. public float pz;
  160. public bool cs;
  161. public bool ih;
  162. public int ht;
  163. public float sp;
  164. public bool dns; //hasDoneNextShoot
  165. public string act3; // ArrowCameraTemplate3
  166. public string[] htai;
  167. public void SetData(Arrow arrow) {
  168. Quaternion r = arrow.transform.rotation;
  169. Vector3 p = arrow.Head().transform.position;
  170. rx = r.x;
  171. ry = r.y;
  172. rz = r.z;
  173. rw = r.w;
  174. px = p.x;
  175. py = p.y;
  176. pz = p.z;
  177. cs = arrow.canUseSideCamera;
  178. ih = arrow.isHit;
  179. ht = arrow.hitType;
  180. sp = arrow.mySpeed;
  181. dns = arrow.hasDoneNextShoot;
  182. htai = arrow.hitTargetAnimalInfo;
  183. if (!inited) {
  184. id = autoID++;
  185. if (autoID > 1000) autoID = 0;
  186. }
  187. inited = true;
  188. }
  189. public void SetArrowCameraTemplate3(bool hasBlockByTree, bool quicklyNextShoot) {
  190. act3 = hasBlockByTree + "," + quicklyNextShoot;
  191. }
  192. public bool HasArrowCameraTemplate3() {
  193. return act3 != null;
  194. }
  195. public System.Tuple<bool, bool> GetArrowCameraTemplate3() {
  196. string[] strs = act3.Split(',');
  197. return new System.Tuple<bool, bool>(bool.Parse(strs[0]), bool.Parse(strs[1]));
  198. }
  199. }
  200. }