ArrowSync.cs 8.2 KB

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