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.

47 lines
1.2KB

  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. static class ResourcesECSDB<T>
  10. {
  11. static readonly FasterList<T> _resources = new FasterList<T>();
  12. internal static ref T resources(uint id)
  13. {
  14. return ref _resources[(int) id - 1];
  15. }
  16. internal static uint ToECS(T resource)
  17. {
  18. _resources.Add(resource);
  19. return (uint)_resources.Count;
  20. }
  21. public static T FromECS(uint id)
  22. {
  23. if (id - 1 < _resources.Count)
  24. return _resources[(int) id - 1];
  25. return default;
  26. }
  27. }
  28. public static class ResourceExtensions
  29. {
  30. public static void Set<T>(ref this ECSResources<T> resource, T newText)
  31. {
  32. if (resource.id != 0)
  33. ResourcesECSDB<T>.resources(resource.id) = newText;
  34. else
  35. resource.id = ResourcesECSDB<T>.ToECS(newText);
  36. }
  37. }
  38. }