BaseTrainCallBack.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace ShotSimulator.Train
  6. {
  7. public delegate void TrainEvent(BaseTrainHandle trainHandle);
  8. public enum TrainEventType
  9. {
  10. OnPrepare,
  11. OnStartTrain,
  12. OnPauseTrain,
  13. OnContinueTrain,
  14. OnCompleteTrain,
  15. OnExitTrain
  16. }
  17. public class BaseTrainCallBack
  18. {
  19. private Dictionary<TrainEventType, TrainEvent> m_TrainEvents = new Dictionary<TrainEventType, TrainEvent>();
  20. public void AddTrainCallBack(TrainEventType type, TrainEvent callback)
  21. {
  22. if (m_TrainEvents.TryGetValue(type, out var trainEvent))
  23. {
  24. trainEvent += callback;
  25. }
  26. else
  27. {
  28. m_TrainEvents.Add(type, callback);
  29. }
  30. }
  31. public void TriggerTrainEvent(TrainEventType type, BaseTrainHandle handle)
  32. {
  33. if (m_TrainEvents.TryGetValue(type, out var trainEvent))
  34. {
  35. if (trainEvent != null)
  36. {
  37. trainEvent(handle);
  38. }
  39. }
  40. }
  41. public void RemoveTrainCallBack(TrainEventType type, TrainEvent callback)
  42. {
  43. if (m_TrainEvents.TryGetValue(type, out var trainEvent))
  44. {
  45. trainEvent -= callback;
  46. }
  47. }
  48. }
  49. }