RequestEventExtensions.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using KairoEngine.Core;
  5. namespace KairoEngine.CharacterSystem
  6. {
  7. public static class RequestEventExtensions
  8. {
  9. public static Dictionary<string, System.Func<CharacterController>> list = new Dictionary<string, System.Func<CharacterController>>();
  10. public static void Bind(this RequestEvents e, string title, System.Func<CharacterController> listener)
  11. {
  12. System.Func<CharacterController> action = null;
  13. if (list.TryGetValue(title, out action) == false)
  14. {
  15. action += listener;
  16. list.Add(title, action);
  17. }
  18. }
  19. public static void Unbind(this RequestEvents e, string title, System.Func<CharacterController> listener)
  20. {
  21. System.Func<CharacterController> action = null;
  22. if (list.TryGetValue(title, out action))
  23. {
  24. action -= listener;
  25. list.Remove(title);
  26. }
  27. }
  28. public static EventResponse<CharacterController> GetCharacterController(this RequestEvents e, string title)
  29. {
  30. System.Func<CharacterController> action = null;
  31. EventResponse<CharacterController> response = new EventResponse<CharacterController>();
  32. if (list.TryGetValue(title, out action))
  33. {
  34. if(action != null)
  35. {
  36. CharacterController result = action();
  37. response.value = result;
  38. if(response.value == null) response.status = EventResponseStatus.EmptyResponse;
  39. else response.status = EventResponseStatus.OK;
  40. }
  41. }
  42. else response.status = EventResponseStatus.NotFound;
  43. return response;
  44. }
  45. }
  46. }