RequestEventsInventoryExtensions.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using KairoEngine.Core;
  5. namespace KairoEngine.Inventory
  6. {
  7. public static class RequestEventsInventoryExtensions
  8. {
  9. public static Dictionary<string, System.Func<ItemContainer>> list = new Dictionary<string, System.Func<ItemContainer>>();
  10. public static void Bind(this RequestEvents e, string title, System.Func<ItemContainer> listener)
  11. {
  12. System.Func<ItemContainer> 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<ItemContainer> listener)
  20. {
  21. System.Func<ItemContainer> action = null;
  22. if (list.TryGetValue(title, out action))
  23. {
  24. action -= listener;
  25. list.Remove(title);
  26. }
  27. }
  28. public static EventResponse<ItemContainer> GetItemContainer(this RequestEvents e, string title)
  29. {
  30. System.Func<ItemContainer> action = null;
  31. EventResponse<ItemContainer> response = new EventResponse<ItemContainer>();
  32. if (list.TryGetValue(title, out action))
  33. {
  34. if(action != null)
  35. {
  36. ItemContainer 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. }