Mirror of Svelto.ECS because we're a fan of it
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

62 linhas
1.7KB

  1. using System.Threading;
  2. using Svelto.Common;
  3. using Svelto.ECS.DataStructures;
  4. namespace Svelto.ECS
  5. {
  6. public class GlobalTypeID
  7. {
  8. internal static uint NextID<T>() { return (uint) (Interlocked.Increment(ref value) - 1); }
  9. static GlobalTypeID() { value = 0; }
  10. static int value;
  11. }
  12. interface IFiller
  13. {
  14. void FillFromByteArray(EntityInitializer init, NativeBag buffer);
  15. }
  16. class Filler<T> : IFiller where T : struct, IEntityComponent
  17. {
  18. static Filler()
  19. {
  20. DBC.ECS.Check.Require(TypeType.isUnmanaged<T>() == true, "invalid type used");
  21. }
  22. //it's an internal interface
  23. public void FillFromByteArray(EntityInitializer init, NativeBag buffer)
  24. {
  25. var component = buffer.Dequeue<T>();
  26. init.Init(component);
  27. }
  28. }
  29. #if UNITY_NATIVE //at the moment I am still considering NativeOperations useful only for Unity
  30. static class EntityComponentID<T>
  31. {
  32. internal static readonly Unity.Burst.SharedStatic<uint> ID =
  33. Unity.Burst.SharedStatic<uint>.GetOrCreate<GlobalTypeID, T>();
  34. }
  35. static class EntityComponentIDMap
  36. {
  37. static readonly Svelto.DataStructures.FasterList<IFiller> TYPE_IDS;
  38. static EntityComponentIDMap()
  39. {
  40. TYPE_IDS = new Svelto.DataStructures.FasterList<IFiller>();
  41. }
  42. internal static void Register<T>(IFiller entityBuilder) where T : struct, IEntityComponent
  43. {
  44. var location = EntityComponentID<T>.ID.Data = GlobalTypeID.NextID<T>();
  45. TYPE_IDS.AddAt(location, entityBuilder);
  46. }
  47. internal static IFiller GetTypeFromID(uint typeId) { return TYPE_IDS[typeId]; }
  48. }
  49. #endif
  50. }