AreaEventTrigger.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. [AddComponentMenu("Trigger/Trigger_General")]
  6. public class AreaEventTrigger : FuncRegion,ITrigger
  7. {
  8. //触发时机
  9. public enum EActTiming
  10. {
  11. Init,
  12. Enter, // first one enters
  13. Leave, // last one leaves
  14. Update,
  15. EnterDura,
  16. }
  17. public enum ActionWhenFull
  18. {
  19. DoNothing,
  20. Destroy,
  21. }
  22. [Serializable]
  23. public struct STimingAction
  24. {
  25. public EActTiming Timing;
  26. public string HelperName;
  27. public int HelperIndex;
  28. //public TextAsset ActionFile;
  29. public string ActionName;
  30. }
  31. public struct STriggerContext
  32. {
  33. public Fighter fighter;
  34. public int token;
  35. public float updateTimer;
  36. public STriggerContext(Fighter actor
  37. , int token
  38. , float inUpdateInterval)
  39. {
  40. this.fighter = actor;
  41. this.token = token;
  42. updateTimer = inUpdateInterval;
  43. }
  44. }
  45. [FriendlyName("ID")]
  46. public int ID = 0;
  47. [FriendlyName("容量")]
  48. public int Capacity = 10;
  49. //[FriendlyName("满载动作")]
  50. public ActionWhenFull actionWhenFull = ActionWhenFull.DoNothing;
  51. [FriendlyName("存活时间【秒】")]
  52. public float AliveTicks; // total
  53. [FriendlyName("触发次数")]
  54. public int TriggerTimes; // total
  55. [FriendlyName("触发完毕后失效")]
  56. public bool bDeactivateSelf = true;
  57. [FriendlyName("探测频率【秒】")]
  58. public float UpdateInterval;
  59. [FriendlyName("轮询式探测")]
  60. public bool bSimpleUpdate = false;
  61. [FriendlyName("只有非满血才能触发")]
  62. public bool OnlyEffectNotFullHpOrMpActor = false;
  63. private Dictionary<long, int> _inActorsCache = null;
  64. [FriendlyName("进入时音效")]
  65. public string EnterSound = null;
  66. [FriendlyName("离开时音效")]
  67. public string LeaveSound = null;
  68. public GameObject[] NextTriggerList = new GameObject[0];
  69. [HideInInspector, NonSerialized]
  70. public Dictionary<long, STriggerContext> _inActors = new Dictionary<long, STriggerContext>();
  71. private int _testToken = 0;
  72. private Fighter[] _actorTestCache;
  73. private long[] _actorTormvCache;
  74. private int m_triggeredCount;
  75. private int m_updateTimer;
  76. [HideInInspector, NonSerialized]
  77. public bool bDoDeactivating;
  78. private bool m_bShaped = false;
  79. private int m_collidedCnt = 0;
  80. public int InActorCount { get { return _inActors.Count; } }
  81. public TriggerActionWrapper[] TriggerActions = new TriggerActionWrapper[0];
  82. [HideInInspector, NonSerialized]
  83. public TriggerActionWrapper PresetActWrapper = null;
  84. private TriggerActionWrapper[] m_internalActList = new TriggerActionWrapper[0];
  85. public GameObject GetTriggerObj()
  86. {
  87. return gameObject;
  88. }
  89. void Awake()
  90. {
  91. _actorTormvCache = new long[Capacity];
  92. m_updateTimer = (int)(UpdateInterval * 1000);
  93. BuildTriggerWrapper();
  94. BuildInternalWrappers();
  95. }
  96. private void Update()
  97. {
  98. UpdateLogic(Time.deltaTime);
  99. }
  100. void OnDestroy()
  101. {
  102. _inActorsCache = null;
  103. foreach (TriggerActionWrapper taw in m_internalActList)
  104. {
  105. if (taw != null)
  106. {
  107. taw.Destroy();
  108. }
  109. }
  110. }
  111. protected virtual void BuildTriggerWrapper()
  112. {
  113. }
  114. private void BuildInternalWrappers()
  115. {
  116. if (PresetActWrapper != null)
  117. {
  118. m_internalActList = AddElement(m_internalActList, PresetActWrapper);
  119. }
  120. if (TriggerActions.Length > 0)
  121. {
  122. m_internalActList = AppendElements(m_internalActList, TriggerActions);
  123. }
  124. foreach (TriggerActionWrapper taw in m_internalActList)
  125. {
  126. if (taw == null)
  127. {
  128. continue;
  129. }
  130. if (taw.GetActionInternal() == null)
  131. {
  132. taw.Init(ID);
  133. }
  134. }
  135. }
  136. public override void UpdateLogic(float delta)
  137. {
  138. if (!isStartup)
  139. {
  140. return;
  141. }
  142. if (bSimpleUpdate)
  143. {
  144. UpdateLogicSimple(delta);
  145. }
  146. if (AliveTicks > 0)
  147. {
  148. AliveTicks -= delta;
  149. if (AliveTicks <= 0)
  150. {
  151. AliveTicks = 0;
  152. DoSelfDeactivating();
  153. }
  154. }
  155. }
  156. private void UpdateLogicSimple(float delta)
  157. {
  158. if (TriggerTimes > 0 && m_triggeredCount >= TriggerTimes)
  159. {
  160. return;
  161. }
  162. int iDelta = (int)(delta * 1000);
  163. m_updateTimer -= iDelta;
  164. if (m_updateTimer <= 0)
  165. {
  166. m_updateTimer =(int)(UpdateInterval * 1000);
  167. for (int i = 0; i < Capacity; ++i)
  168. {
  169. Fighter curAct = _actorTestCache[i];
  170. DoActorUpdate(ref curAct);
  171. }
  172. if (++m_triggeredCount >= TriggerTimes && TriggerTimes > 0)
  173. {
  174. bDoDeactivating = bDeactivateSelf;
  175. }
  176. }
  177. }
  178. public void DoSelfDeactivating()
  179. {
  180. bDoDeactivating = false;
  181. int toRmvNum = 0;
  182. var etr = _inActors.GetEnumerator();
  183. while (etr.MoveNext())
  184. {
  185. _actorTormvCache[toRmvNum++] = etr.Current.Key;
  186. }
  187. for (int i = 0; i < toRmvNum; ++i)
  188. {
  189. long toRmvObjId = _actorTormvCache[i];
  190. Fighter refHdl = _inActors[toRmvObjId].fighter;
  191. #region 这两行时序不能随意调换
  192. DoActorLeave(ref refHdl);
  193. _inActors.Remove(toRmvObjId);
  194. #endregion 这两行时序不能随意调换
  195. }
  196. DeactivateSelf();
  197. ActivateNext();
  198. }
  199. private void DeactivateSelf()
  200. {
  201. gameObject.SetActive(false);
  202. }
  203. private void ActivateNext()
  204. {
  205. foreach (GameObject nextGo in NextTriggerList)
  206. {
  207. if (nextGo != null)
  208. {
  209. nextGo.SetActive(true);
  210. }
  211. }
  212. }
  213. protected virtual void DoActorEnter(ref Fighter inActor)
  214. {
  215. MusicMgr.Instance.PlayFightSound(EnterSound);
  216. DoActorEnterShared(ref inActor);
  217. }
  218. protected virtual void DoActorLeave(ref Fighter inActor)
  219. {
  220. DoActorLeaveShared(ref inActor);
  221. if (inActor == null)
  222. return;
  223. if (!string.IsNullOrEmpty(LeaveSound))
  224. {
  225. MusicMgr.Instance.PlayFightSound(LeaveSound);
  226. }
  227. }
  228. protected void DoActorEnterShared(ref Fighter inActor)
  229. {
  230. foreach (TriggerActionWrapper taw in m_internalActList)
  231. {
  232. if (taw != null)
  233. {
  234. taw.TriggerEnter(inActor, this);
  235. }
  236. }
  237. }
  238. protected void DoActorLeaveShared(ref Fighter inActor)
  239. {
  240. foreach (TriggerActionWrapper taw in m_internalActList)
  241. {
  242. if (taw != null)
  243. {
  244. taw.TriggerLeave(inActor, this);
  245. }
  246. }
  247. }
  248. protected virtual void DoActorUpdate(ref Fighter inActor)
  249. {
  250. DoActorUpdateShared(ref inActor);
  251. }
  252. protected void DoActorUpdateShared(ref Fighter inActor)
  253. {
  254. foreach (TriggerActionWrapper taw in m_internalActList)
  255. {
  256. if (taw != null)
  257. {
  258. taw.TriggerUpdate(inActor, this);
  259. }
  260. }
  261. }
  262. public override void Startup()
  263. {
  264. base.Startup();
  265. //_thisActor = ActorHelper.GetActorRoot(sourceActor);
  266. OnTriggerStart();
  267. }
  268. public override void Stop()
  269. {
  270. base.Stop();
  271. int count = m_internalActList.Length;
  272. for (int i = 0; i < count; ++i)
  273. {
  274. var taw = m_internalActList[i];
  275. if (taw != null)
  276. {
  277. taw.Stop();
  278. }
  279. }
  280. }
  281. protected void OnCoolingDown()
  282. {
  283. foreach (TriggerActionWrapper taw in m_internalActList)
  284. {
  285. if (taw != null)
  286. {
  287. taw.OnCoolDown(this);
  288. }
  289. }
  290. }
  291. protected void OnTriggerStart()
  292. {
  293. foreach (TriggerActionWrapper taw in m_internalActList)
  294. {
  295. if (taw != null)
  296. {
  297. taw.OnTriggerStart(this);
  298. }
  299. }
  300. }
  301. public void onActorDestroy(ref Fighter f)
  302. {
  303. if (f!=null)
  304. {
  305. long id = f.Id;
  306. if (null != _inActors && _inActors.ContainsKey(id))
  307. {
  308. DoActorLeave(ref f);
  309. _inActors.Remove(id);
  310. }
  311. if (null != _inActorsCache && _inActorsCache.ContainsKey(id))
  312. {
  313. _inActorsCache.Remove(id);
  314. }
  315. }
  316. else
  317. {
  318. DebugHelper.LogError("onActorDestroy#prm.src == null!");
  319. }
  320. }
  321. public static T[] AddElement<T>(T[] elements, T element)
  322. {
  323. List<T> elementsList = new List<T>(elements);
  324. elementsList.Add(element);
  325. return LinqS.ToArray(elementsList);
  326. }
  327. public static T[] AppendElements<T>(T[] elements, T[] appendElements)
  328. {
  329. List<T> elementsList = new List<T>(elements);
  330. if (appendElements != null)
  331. {
  332. elementsList.AddRange(appendElements);
  333. }
  334. return LinqS.ToArray(elementsList);
  335. }
  336. }