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.

EntityView.cs 1.6KB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using Svelto.DataStructures;
  5. using Svelto.Utilities;
  6. namespace Svelto.ECS
  7. {
  8. public interface IEntityView
  9. {
  10. int ID { get; }
  11. }
  12. public interface IEntityStruct:IEntityView
  13. {
  14. new int ID { set; }
  15. }
  16. public class EntityView : IEntityView
  17. {
  18. public int ID { get { return _ID; } }
  19. internal FasterList<KeyValuePair<Type, CastedAction<EntityView>>> entityViewBlazingFastReflection;
  20. internal int _ID;
  21. }
  22. internal static class EntityView<T> where T: EntityView, new()
  23. {
  24. internal static T BuildEntityView(int ID)
  25. {
  26. if (FieldCache<T>.list.Count == 0)
  27. {
  28. var type = typeof(T);
  29. var fields = type.GetFields(BindingFlags.Public |
  30. BindingFlags.Instance);
  31. for (int i = fields.Length - 1; i >= 0; --i)
  32. {
  33. var field = fields[i];
  34. CastedAction<EntityView> setter = FastInvoke<T>.MakeSetter<EntityView>(field);
  35. FieldCache<T>.list.Add(new KeyValuePair<Type, CastedAction<EntityView>>(field.FieldType, setter));
  36. }
  37. }
  38. return new T { _ID = ID, entityViewBlazingFastReflection = FieldCache<T>.list };
  39. }
  40. static class FieldCache<W> where W:T
  41. {
  42. internal static readonly FasterList<KeyValuePair<Type, CastedAction<EntityView>>> list = new FasterList<KeyValuePair<Type, CastedAction<EntityView>>>();
  43. }
  44. }
  45. }