Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.3KB

  1. using Svelto.DataStructures;
  2. namespace Svelto.ECS.Experimental
  3. {
  4. public struct ECSResources<T>
  5. {
  6. internal uint id;
  7. public static implicit operator T(ECSResources<T> ecsString) { return ResourcesECSDB<T>.FromECS(ecsString.id); }
  8. }
  9. /// <summary>
  10. /// To do. Or we reuse the ID or we need to clear this
  11. /// </summary>
  12. /// <typeparam name="T"></typeparam>
  13. static class ResourcesECSDB<T>
  14. {
  15. static readonly FasterList<T> _resources = new FasterList<T>();
  16. internal static ref T resources(uint id)
  17. {
  18. return ref _resources[(int) id - 1];
  19. }
  20. internal static uint ToECS(T resource)
  21. {
  22. _resources.Add(resource);
  23. return (uint)_resources.count;
  24. }
  25. public static T FromECS(uint id)
  26. {
  27. if (id - 1 < _resources.count)
  28. return _resources[(int) id - 1];
  29. return default;
  30. }
  31. }
  32. public static class ResourceExtensions
  33. {
  34. public static void Set<T>(ref this ECSResources<T> resource, T newText)
  35. {
  36. if (resource.id != 0)
  37. ResourcesECSDB<T>.resources(resource.id) = newText;
  38. else
  39. resource.id = ResourcesECSDB<T>.ToECS(newText);
  40. }
  41. }
  42. }