FSkillDelayHurtEvent.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Security;
  4. using UnityEngine;
  5. namespace Flux
  6. {
  7. [FEvent("Skill/伤害相关/延迟伤害")]
  8. public class FSkillDelayHurtEvent :FEvent
  9. {
  10. [SerializeField]
  11. private AudioClip _hitSoundClip = null;
  12. public AudioClip HitSoundClip
  13. {
  14. get { return _hitSoundClip; }
  15. }
  16. [HideInInspector]
  17. [SerializeField]
  18. private int _hitEffectId;
  19. public int HitEffectId
  20. {
  21. get { return _hitEffectId; }
  22. }
  23. [HideInInspector]
  24. [SerializeField]
  25. private GameObject _hitEffectPrefab = null;
  26. public GameObject HitEffectPrefab
  27. {
  28. get { return _hitEffectPrefab; }
  29. }
  30. [HideInInspector]
  31. [SerializeField]
  32. private string _hitEffectLink = null;
  33. public string HitEffectLink
  34. {
  35. get { return _hitEffectLink; }
  36. }
  37. [HideInInspector]
  38. [SerializeField]
  39. private int _hurtDelayFrame = 0;
  40. public int HurtDelayFrame
  41. {
  42. get { return _hurtDelayFrame; }
  43. }
  44. [HideInInspector]
  45. [SerializeField]
  46. private int _delayHurtInterval = 0;
  47. public int DelayHurtInterval
  48. {
  49. get { return _delayHurtInterval; }
  50. }
  51. [HideInInspector]
  52. [SerializeField]
  53. private int _delayHurtCount = 0;
  54. public int DelayHurtCount
  55. {
  56. get { return _delayHurtCount; }
  57. }
  58. private int _leftFrame = 0;
  59. private int _hitCnt = 0;
  60. private AudioSource _source;
  61. private GameObject hitEffectGo = null;
  62. private ParticleSystem _hitPs = null;
  63. private float _previousTimeSinceTrigger = 0;
  64. private float _previousSpeed = 0;
  65. public FSkillDelayHurtEvent()
  66. {
  67. _eventType = SkillActionFrameEventType.FE_Delay_Hurt;
  68. }
  69. protected override void OnTrigger(float timeSinceTrigger)
  70. {
  71. _leftFrame = _hurtDelayFrame;
  72. _hitCnt = 0;
  73. _previousTimeSinceTrigger = timeSinceTrigger;
  74. _previousSpeed = Sequence.Speed;
  75. }
  76. protected override void OnPause()
  77. {
  78. if(Application.isPlaying)
  79. {
  80. if (_source)
  81. _source.Pause();
  82. }
  83. else
  84. {
  85. #if UNITY_EDITOR
  86. UnityEditor.AudioUtility.PauseClip(_hitSoundClip);
  87. #endif
  88. }
  89. if(_hitPs!=null)
  90. {
  91. _hitPs.Pause(true);
  92. }
  93. }
  94. protected override void OnResume()
  95. {
  96. if(Application.isPlaying)
  97. {
  98. if (Sequence.IsPlaying)
  99. {
  100. if (_source)
  101. _source.Play();
  102. }
  103. }
  104. else
  105. {
  106. #if UNITY_EDITOR
  107. UnityEditor.AudioUtility.PlayClip(_hitSoundClip);
  108. #endif
  109. }
  110. if (_hitPs != null)
  111. {
  112. _hitPs.Play(true);
  113. }
  114. }
  115. protected override void OnFinish()
  116. {
  117. if (Application.isPlaying)
  118. {
  119. if (_source)
  120. {
  121. if (_source.clip == _hitSoundClip && _source.isPlaying)
  122. {
  123. _source.Stop();
  124. _source.clip = null;
  125. }
  126. }
  127. }
  128. else
  129. {
  130. #if UNITY_EDITOR
  131. UnityEditor.AudioUtility.StopClip(_hitSoundClip);
  132. #endif
  133. }
  134. if (hitEffectGo != null)
  135. {
  136. GameObject.DestroyImmediate(hitEffectGo);
  137. hitEffectGo = null;
  138. }
  139. }
  140. protected override void OnStop()
  141. {
  142. if(Application.isPlaying)
  143. {
  144. if (_source)
  145. {
  146. if (_source.clip == _hitSoundClip && _source.isPlaying)
  147. {
  148. _source.Stop();
  149. _source.clip = null;
  150. }
  151. }
  152. }
  153. else
  154. {
  155. #if UNITY_EDITOR
  156. UnityEditor.AudioUtility.StopClip(_hitSoundClip);
  157. #endif
  158. }
  159. if (hitEffectGo != null)
  160. {
  161. GameObject.DestroyImmediate(hitEffectGo);
  162. hitEffectGo = null;
  163. }
  164. }
  165. public override int GetMaxLength()
  166. {
  167. return base.GetMaxLength();
  168. }
  169. public override string Text
  170. {
  171. get { return "延迟伤害"; }
  172. set { }
  173. }
  174. protected override void OnUpdateEvent(float timeSinceTrigger)
  175. {
  176. float delta = timeSinceTrigger - _previousTimeSinceTrigger;
  177. _previousTimeSinceTrigger = timeSinceTrigger;
  178. bool restart = false;
  179. if (_hitCnt < _delayHurtCount)
  180. {
  181. if (_leftFrame == 0)
  182. {
  183. restart = true;
  184. ExecuteHit();
  185. }
  186. else
  187. {
  188. _leftFrame--;
  189. }
  190. }
  191. if (_hitPs && !Sequence.IsPlaying || !Sequence.IsPlayingForward)
  192. {
  193. _previousSpeed = 1;
  194. ParticleSystem.MainModule mainModule = _hitPs.main;
  195. mainModule.simulationSpeed = _previousSpeed;
  196. if (Sequence.IsPlayingForward && delta > 0)
  197. {
  198. _hitPs.Simulate(delta, true, restart);
  199. }
  200. }
  201. else if (_previousSpeed != Sequence.Speed)
  202. {
  203. _previousSpeed = Sequence.Speed;
  204. ParticleSystem.MainModule mainModule = _hitPs.main;
  205. mainModule.simulationSpeed = Mathf.Abs(_previousSpeed);
  206. }
  207. }
  208. private void ExecuteHit()
  209. {
  210. _hitCnt++;
  211. _leftFrame = _delayHurtInterval;
  212. _source = Owner.GetComponent<AudioSource>();
  213. if (_source == null)
  214. _source = Owner.gameObject.AddComponent<AudioSource>();
  215. _source.volume = 1;
  216. _source.loop = false;
  217. _source.clip = _hitSoundClip;
  218. if (Sequence.IsPlaying)
  219. _source.Play();
  220. if (_hitEffectPrefab != null)
  221. {
  222. Vector3 pos = _targetTrans.position;
  223. Transform hitPoint = UnityEngineUtils.RecurisiveFindTransformChild(_targetTrans, _hitEffectLink);
  224. if (hitPoint != null)
  225. {
  226. pos = hitPoint.position;
  227. }
  228. if(hitEffectGo!=null)
  229. {
  230. GameObject.DestroyImmediate(hitEffectGo);
  231. hitEffectGo = null;
  232. }
  233. hitEffectGo = (GameObject)Instantiate(_hitEffectPrefab);
  234. hitEffectGo.transform.SetParent(hitPoint);
  235. hitEffectGo.transform.localPosition = Vector3.zero;
  236. hitEffectGo.transform.localScale = Vector3.one;
  237. hitEffectGo.transform.localRotation = Quaternion.identity;
  238. _hitPs = hitEffectGo.GetComponentInChildren<ParticleSystem>();
  239. if (_hitPs != null)
  240. {
  241. ParticleSystem.MainModule mainModule = _hitPs.main;
  242. mainModule.simulationSpeed = Sequence.Speed;
  243. if (Sequence.IsPlaying && Sequence.IsPlayingForward)
  244. {
  245. _hitPs.Play(true);
  246. }
  247. }
  248. }
  249. }
  250. public override SecurityElement SaveToXml()
  251. {
  252. SecurityElement node = base.SaveToXml();
  253. SecurityElement paramNode = WriteParamNode("delay_hurt_count", _delayHurtCount.ToString(), "int");
  254. node.AddChild(paramNode);
  255. paramNode = WriteParamNode("delay_hurt_interval", _delayHurtInterval.ToString(), "int");
  256. node.AddChild(paramNode);
  257. paramNode = WriteParamNode("hurt_delay_frame", _hurtDelayFrame.ToString(), "int");
  258. node.AddChild(paramNode);
  259. paramNode = WriteParamNode("effect", _hitEffectId.ToString(), "int");
  260. node.AddChild(paramNode);
  261. if (_hitEffectPrefab != null)
  262. {
  263. #if UNITY_EDITOR
  264. paramNode = WriteParamNode("effectPrefab", UnityEditor.AssetDatabase.GetAssetPath(_hitEffectPrefab), "string");
  265. node.AddChild(paramNode);
  266. #endif
  267. }
  268. paramNode = WriteParamNode("effectLink", _hitEffectLink, "string");
  269. if (paramNode != null)
  270. node.AddChild(paramNode);
  271. if (_hitSoundClip != null)
  272. {
  273. #if UNITY_EDITOR
  274. string audioPath = UnityEditor.AssetDatabase.GetAssetPath(_hitSoundClip);
  275. string relativeName = FileUtils.RemoveParent(Constants.FightAudioPath, audioPath);
  276. paramNode = WriteParamNode("hitsound", relativeName, "string");
  277. node.AddChild(paramNode);
  278. paramNode = WriteParamNode("soundPath", audioPath, "string");
  279. node.AddChild(paramNode);
  280. #endif
  281. }
  282. return node;
  283. }
  284. public override void LoadFromXml(SecurityElement eventNode)
  285. {
  286. base.LoadFromXml(eventNode);
  287. _delayHurtCount = GetNParam("delay_hurt_count");
  288. _delayHurtInterval = GetNParam("delay_hurt_interval");
  289. _hurtDelayFrame = GetNParam("hurt_delay_frame");
  290. _hitEffectId = GetNParam("effect");
  291. string effectPrefab = GetSParam("effectPrefab");
  292. if (!string.IsNullOrEmpty(effectPrefab))
  293. {
  294. #if UNITY_EDITOR
  295. _hitEffectPrefab = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(effectPrefab);
  296. #endif
  297. }
  298. _hitEffectLink = GetSParam("effectLink");
  299. string soundPath = GetSParam("soundPath");
  300. if (!string.IsNullOrEmpty(soundPath))
  301. {
  302. #if UNITY_EDITOR
  303. _hitSoundClip = UnityEditor.AssetDatabase.LoadAssetAtPath<AudioClip>(soundPath);
  304. #endif
  305. }
  306. }
  307. }
  308. }