BaseCharacter.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. namespace Terminal.Gui.TextEffects;
  2. public class EffectCharacter
  3. {
  4. public int CharacterId { get; }
  5. public string InputSymbol { get; }
  6. public Coord InputCoord { get; }
  7. public bool IsVisible { get; set; }
  8. public Animation Animation { get; }
  9. public Motion Motion { get; }
  10. public EventHandler EventHandler { get; }
  11. public int Layer { get; set; }
  12. public bool IsFillCharacter { get; set; }
  13. public EffectCharacter (int characterId, string symbol, int inputColumn, int inputRow)
  14. {
  15. CharacterId = characterId;
  16. InputSymbol = symbol;
  17. InputCoord = new Coord (inputColumn, inputRow);
  18. IsVisible = false;
  19. Animation = new Animation (this);
  20. Motion = new Motion (this);
  21. EventHandler = new EventHandler (this);
  22. Layer = 0;
  23. IsFillCharacter = false;
  24. }
  25. public bool IsActive => !Animation.ActiveSceneIsComplete() || !Motion.MovementIsComplete ();
  26. public void Tick ()
  27. {
  28. Motion.Move ();
  29. Animation.StepAnimation ();
  30. }
  31. }
  32. public class EventHandler
  33. {
  34. public EffectCharacter Character { get; }
  35. public Dictionary<(Event, object), List<(Action, object)>> RegisteredEvents { get; }
  36. public EventHandler (EffectCharacter character)
  37. {
  38. Character = character;
  39. RegisteredEvents = new Dictionary<(Event, object), List<(Action, object)>> ();
  40. }
  41. public void RegisterEvent (Event @event, object caller, Action action, object target)
  42. {
  43. var key = (@event, caller);
  44. if (!RegisteredEvents.ContainsKey (key))
  45. RegisteredEvents [key] = new List<(Action, object)> ();
  46. RegisteredEvents [key].Add ((action, target));
  47. }
  48. public void HandleEvent (Event @event, object caller)
  49. {
  50. var key = (@event, caller);
  51. if (!RegisteredEvents.ContainsKey (key))
  52. return;
  53. foreach (var (action, target) in RegisteredEvents [key])
  54. {
  55. switch (action)
  56. {
  57. case Action.ActivatePath:
  58. Character.Motion.ActivatePath (target as Path);
  59. break;
  60. case Action.DeactivatePath:
  61. Character.Motion.DeactivatePath (target as Path);
  62. break;
  63. case Action.SetLayer:
  64. Character.Layer = (int)target;
  65. break;
  66. case Action.SetCoordinate:
  67. Character.Motion.CurrentCoord = (Coord)target;
  68. break;
  69. case Action.Callback:
  70. // TODO:
  71. throw new NotImplementedException ("TODO, port (target as Action)?.Invoke ()");
  72. break;
  73. default:
  74. throw new ArgumentOutOfRangeException (nameof (action), "Unhandled action.");
  75. }
  76. }
  77. }
  78. public enum Event
  79. {
  80. SegmentEntered,
  81. SegmentExited,
  82. PathActivated,
  83. PathComplete,
  84. PathHolding,
  85. SceneActivated,
  86. SceneComplete
  87. }
  88. public enum Action
  89. {
  90. ActivatePath,
  91. DeactivatePath,
  92. SetLayer,
  93. SetCoordinate,
  94. Callback
  95. }
  96. }