FSkillHitEvent.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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 FSkillHitEvent : FEvent
  9. {
  10. [HideInInspector]
  11. [Tooltip("What's the offset from where we play the animation?")]
  12. public int _startOffset;
  13. [SerializeField]
  14. private AudioClip _audioClip = null;
  15. public AudioClip AudioClip
  16. {
  17. get { return _audioClip; }
  18. }
  19. [HideInInspector]
  20. [SerializeField]
  21. private int _hitEffectId;
  22. public int HitEffectId
  23. {
  24. get { return _hitEffectId; }
  25. }
  26. [HideInInspector]
  27. [SerializeField]
  28. private GameObject _hitEffectPrefab = null;
  29. public GameObject HitEffectPrefab
  30. {
  31. get { return _hitEffectPrefab; }
  32. }
  33. [HideInInspector]
  34. [SerializeField]
  35. private string _hitEffectLink;
  36. public string HitEffectLink
  37. {
  38. get { return _hitEffectLink; }
  39. }
  40. [HideInInspector]
  41. [SerializeField]
  42. private int _moveType = 0;
  43. public int MoveType
  44. {
  45. get { return _moveType; }
  46. }
  47. [HideInInspector]
  48. [SerializeField]
  49. private float _initSpeed;
  50. public float InitSpeed
  51. {
  52. get { return _initSpeed; }
  53. }
  54. [HideInInspector]
  55. [SerializeField]
  56. private float _acceleration = 0;
  57. public float Acceleration
  58. {
  59. get { return _acceleration; }
  60. }
  61. [HideInInspector]
  62. [SerializeField]
  63. private float _moveDistance = 0;
  64. public float MoveDistance
  65. {
  66. get { return _moveDistance; }
  67. }
  68. [HideInInspector]
  69. [SerializeField]
  70. private int _triggerBuffId;
  71. public int TriggerBuffId
  72. {
  73. get { return _triggerBuffId; }
  74. }
  75. [HideInInspector]
  76. [SerializeField]
  77. private int _triggerBuffRate;
  78. public int TriggerBuffRate
  79. {
  80. get { return _triggerBuffRate; }
  81. }
  82. private AudioSource _source;
  83. private ParticleSystem _particleSystem = null;
  84. private GameObject _effectGo = null;
  85. private Animator[] effectAnimators;
  86. private float _previousTimeSinceTrigger = 0;
  87. private float _previousSpeed = 0;
  88. private MoveProcessor _moveProcessor;
  89. private Vector3 _destPos;
  90. private Vector3 _targetPos;
  91. public FSkillHitEvent()
  92. {
  93. _eventType = SkillActionFrameEventType.FE_Hit;
  94. }
  95. protected override void OnSetDefaultValues()
  96. {
  97. base.OnSetDefaultValues();
  98. }
  99. protected override void OnTrigger(float timeSinceTrigger)
  100. {
  101. if(Application.isPlaying)
  102. {
  103. _source = Owner.GetComponent<AudioSource>();
  104. if (_source == null)
  105. _source = Owner.gameObject.AddComponent<AudioSource>();
  106. _source.volume = 1;
  107. _source.loop = false;
  108. _source.clip = _audioClip;
  109. if (Sequence.IsPlaying)
  110. _source.Play();
  111. }
  112. else
  113. {
  114. #if UNITY_EDITOR
  115. UnityEditor.AudioUtility.PlayClip(_audioClip);
  116. #endif
  117. }
  118. if (_targetTrans != null)
  119. {
  120. FighterMoveType type = (FighterMoveType)_moveType;
  121. if (type != FighterMoveType.None)
  122. {
  123. _moveProcessor = new MoveProcessor(_targetTrans);
  124. if (type == FighterMoveType.Beat_Back)
  125. {
  126. Vector3 dir = (_targetTrans.position - _casterTrans.position).normalized;
  127. dir.y = 0;
  128. _destPos = dir * _moveDistance + _targetTrans.position;
  129. }
  130. else if (type == FighterMoveType.Beat_Fly)
  131. {
  132. Vector3 dir = Vector3.up;
  133. _destPos = dir * _moveDistance + _targetTrans.position;
  134. }
  135. }
  136. }
  137. effectAnimators = null;
  138. if (_hitEffectPrefab != null)
  139. {
  140. Vector3 pos = _targetTrans.position;
  141. Transform hitPoint = UnityEngineUtils.RecurisiveFindTransformChild(_targetTrans, _hitEffectLink);
  142. if (hitPoint != null)
  143. {
  144. pos = hitPoint.position;
  145. }
  146. _effectGo = (GameObject)Instantiate(_hitEffectPrefab);
  147. _effectGo.transform.SetParent(hitPoint);
  148. _effectGo.transform.localPosition = Vector3.zero;
  149. _effectGo.transform.localScale = Vector3.one;
  150. _effectGo.transform.localRotation = Quaternion.identity;
  151. _particleSystem = _effectGo.GetComponentInChildren<ParticleSystem>();
  152. if (_particleSystem != null)
  153. {
  154. ParticleSystem.MainModule mainModule = _particleSystem.main;
  155. mainModule.simulationSpeed = Sequence.Speed;
  156. effectAnimators = _effectGo.GetComponentsInChildren<Animator>();
  157. }
  158. }
  159. if (effectAnimators != null && effectAnimators.Length > 0)
  160. {
  161. for (int aid = 0; aid < effectAnimators.Length; aid++)
  162. {
  163. var animator = effectAnimators[aid];
  164. animator.enabled = true;
  165. animator.SetLayerWeight(0, 1);
  166. animator.Update(0f);
  167. }
  168. }
  169. if (_particleSystem != null && Sequence.IsPlaying && Sequence.IsPlayingForward)
  170. _particleSystem.Play(true);
  171. if(_targetTrans!=null)
  172. {
  173. _targetPos = _targetTrans.position;
  174. }
  175. if (_moveProcessor != null)
  176. {
  177. _moveProcessor.Start(_initSpeed, _acceleration, _destPos);
  178. _previousTimeSinceTrigger = timeSinceTrigger;
  179. }
  180. _previousTimeSinceTrigger = 0;
  181. _previousSpeed = Sequence.Speed;
  182. }
  183. protected override void OnPause()
  184. {
  185. if(Application.isPlaying)
  186. {
  187. if (_source)
  188. _source.Pause();
  189. }
  190. else
  191. {
  192. #if UNITY_EDITOR
  193. UnityEditor.AudioUtility.PauseClip(_audioClip);
  194. #endif
  195. }
  196. if (_particleSystem != null)
  197. _particleSystem.Pause();
  198. if (effectAnimators != null && effectAnimators.Length > 0)
  199. {
  200. for (int idx = 0; idx < effectAnimators.Length; idx++)
  201. effectAnimators[idx].enabled = false;
  202. }
  203. }
  204. protected override void OnResume()
  205. {
  206. if(Application.isPlaying)
  207. {
  208. if (Sequence.IsPlaying)
  209. {
  210. if (_source)
  211. _source.Play();
  212. }
  213. }
  214. else
  215. {
  216. #if UNITY_EDITOR
  217. UnityEditor.AudioUtility.PlayClip(_audioClip);
  218. #endif
  219. }
  220. if (_particleSystem != null && Sequence.IsPlayingForward)
  221. {
  222. _particleSystem.Play(true);
  223. if (effectAnimators != null && effectAnimators.Length > 0)
  224. {
  225. for (int idx = 0; idx < effectAnimators.Length; idx++)
  226. effectAnimators[idx].enabled = true;
  227. }
  228. }
  229. }
  230. protected override void OnFinish()
  231. {
  232. if(Application.isPlaying)
  233. {
  234. if (_source)
  235. {
  236. if (_source.clip == _audioClip && _source.isPlaying)
  237. {
  238. _source.Stop();
  239. _source.clip = null;
  240. }
  241. }
  242. }
  243. else
  244. {
  245. #if UNITY_EDITOR
  246. UnityEditor.AudioUtility.StopClip(_audioClip);
  247. #endif
  248. }
  249. if (effectAnimators != null && effectAnimators.Length > 0)
  250. {
  251. for (int idx = 0; idx < effectAnimators.Length; idx++)
  252. {
  253. effectAnimators[idx].SetLayerWeight(0, 0);
  254. effectAnimators[idx].enabled = false;
  255. }
  256. }
  257. if (_particleSystem != null)
  258. _particleSystem.Stop(true);
  259. if (_effectGo != null)
  260. {
  261. GameObject.DestroyImmediate(_effectGo);
  262. _effectGo = null;
  263. }
  264. if(_targetTrans!=null)
  265. {
  266. _targetTrans.position = _targetPos;
  267. }
  268. }
  269. protected override void OnStop()
  270. {
  271. if(Application.isPlaying)
  272. {
  273. if (_source)
  274. {
  275. if (_source.clip == _audioClip && _source.isPlaying)
  276. {
  277. _source.Stop();
  278. _source.clip = null;
  279. }
  280. }
  281. }
  282. else
  283. {
  284. #if UNITY_EDITOR
  285. UnityEditor.AudioUtility.StopClip(_audioClip);
  286. #endif
  287. }
  288. if (_particleSystem != null)
  289. {
  290. _particleSystem.Stop(true);
  291. _particleSystem.Clear(true);
  292. }
  293. if (_effectGo != null)
  294. {
  295. if (effectAnimators != null)
  296. {
  297. for (int idx = 0; idx < effectAnimators.Length; idx++)
  298. {
  299. effectAnimators[idx].SetLayerWeight(0, 0);
  300. effectAnimators[idx].enabled = false;
  301. }
  302. }
  303. GameObject.DestroyImmediate(_effectGo);
  304. _effectGo = null;
  305. }
  306. if (_targetTrans != null)
  307. {
  308. _targetTrans.position = _targetPos;
  309. }
  310. }
  311. protected override void OnUpdateEvent(float timeSinceTrigger)
  312. {
  313. float delta = timeSinceTrigger - _previousTimeSinceTrigger;
  314. _previousTimeSinceTrigger = timeSinceTrigger;
  315. if (_source != null && _source.clip != null)
  316. {
  317. _source.PlayScheduled(delta);
  318. }
  319. if (_particleSystem != null)
  320. {
  321. if (!Sequence.IsPlaying || !Sequence.IsPlayingForward)
  322. {
  323. _previousSpeed = 1;
  324. ParticleSystem.MainModule mainModule = _particleSystem.main;
  325. mainModule.simulationSpeed = _previousSpeed;
  326. if (Sequence.IsPlayingForward && delta > 0)
  327. {
  328. _particleSystem.Simulate(delta, true, false);
  329. }
  330. }
  331. else if (_previousSpeed != Sequence.Speed)
  332. {
  333. _previousSpeed = Sequence.Speed;
  334. ParticleSystem.MainModule mainModule = _particleSystem.main;
  335. mainModule.simulationSpeed = Mathf.Abs(_previousSpeed);
  336. }
  337. if (effectAnimators != null && effectAnimators.Length > 0)
  338. {
  339. for (int idx = 0; idx < effectAnimators.Length; idx++)
  340. {
  341. var animator = effectAnimators[idx];
  342. if (!animator.enabled)
  343. animator.enabled = true;
  344. if (delta > 0)
  345. {
  346. animator.Update(delta);
  347. }
  348. }
  349. }
  350. }
  351. if (_moveProcessor != null)
  352. {
  353. _moveProcessor.Update(delta);
  354. }
  355. }
  356. public override int GetMaxLength()
  357. {
  358. return base.GetMaxLength();
  359. }
  360. public override string Text
  361. {
  362. get { return "打击点"; }
  363. set { }
  364. }
  365. public override SecurityElement SaveToXml()
  366. {
  367. SecurityElement node = base.SaveToXml();
  368. SecurityElement paramNode = WriteParamNode("hiteffect", _hitEffectId.ToString(), "int");
  369. node.AddChild(paramNode);
  370. if (_hitEffectPrefab != null)
  371. {
  372. #if UNITY_EDITOR
  373. paramNode = WriteParamNode("hitEffectPrefab", UnityEditor.AssetDatabase.GetAssetPath(_hitEffectPrefab), "string");
  374. node.AddChild(paramNode);
  375. #endif
  376. }
  377. paramNode = WriteParamNode("hitEffectLink", _hitEffectLink, "string");
  378. if (paramNode != null)
  379. node.AddChild(paramNode);
  380. if (_audioClip != null)
  381. {
  382. #if UNITY_EDITOR
  383. string audioPath = UnityEditor.AssetDatabase.GetAssetPath(_audioClip);
  384. string relativeName = FileUtils.RemoveParent(Constants.FightAudioPath, audioPath);
  385. paramNode = WriteParamNode("sound", relativeName, "string");
  386. node.AddChild(paramNode);
  387. paramNode = WriteParamNode("soundPath", audioPath, "string");
  388. node.AddChild(paramNode);
  389. #endif
  390. }
  391. paramNode = WriteParamNode("buffid", _triggerBuffId.ToString(), "int");
  392. node.AddChild(paramNode);
  393. paramNode = WriteParamNode("rate", _triggerBuffRate.ToString(), "int");
  394. node.AddChild(paramNode);
  395. paramNode = WriteParamNode("moveType", _moveType.ToString(),"int");
  396. node.AddChild(paramNode);
  397. paramNode = WriteParamNode("initSpeed", ((int)(_initSpeed * 100)).ToString(), "int");
  398. node.AddChild(paramNode);
  399. paramNode = WriteParamNode("acceleration", ((int)(_acceleration * 100)).ToString(), "int");
  400. node.AddChild(paramNode);
  401. paramNode = WriteParamNode("moveDist", ((int)(_moveDistance*100)).ToString(), "int");
  402. node.AddChild(paramNode);
  403. return node;
  404. }
  405. public override void LoadFromXml(SecurityElement eventNode)
  406. {
  407. base.LoadFromXml(eventNode);
  408. int.TryParse(eventNode.Attribute(""), out _hitEffectId);
  409. _hitEffectId = GetNParam("hiteffect");
  410. _triggerBuffId = GetNParam("buffid");
  411. _triggerBuffRate = GetNParam("rate");
  412. _moveType = GetNParam("moveType");
  413. _initSpeed = GetNParam("initSpeed") * 0.01f;
  414. _acceleration = GetNParam("acceleration") * 0.01f;
  415. _moveDistance = GetNParam("moveDist") * 0.01f;
  416. string hitEffectPrefab = GetSParam("hitEffectPrefab");
  417. if (!string.IsNullOrEmpty(hitEffectPrefab))
  418. {
  419. #if UNITY_EDITOR
  420. _hitEffectPrefab = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(hitEffectPrefab);
  421. #endif
  422. }
  423. _hitEffectLink = GetSParam("hitEffectLink");
  424. string soundPath = GetSParam("soundPath");
  425. if (!string.IsNullOrEmpty(soundPath))
  426. {
  427. #if UNITY_EDITOR
  428. _audioClip = UnityEditor.AssetDatabase.LoadAssetAtPath<AudioClip>(soundPath);
  429. #endif
  430. }
  431. }
  432. }
  433. }