EcsHelpers.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // ----------------------------------------------------------------------------
  2. // The MIT License
  3. // Simple Entity Component System framework https://github.com/Leopotam/ecs
  4. // Copyright (c) 2017-2019 Leopotam <leopotam@gmail.com>
  5. // ----------------------------------------------------------------------------
  6. using System;
  7. using System.Runtime.CompilerServices;
  8. namespace Leopotam.Ecs {
  9. static class EcsHelpers {
  10. const int EntityComponentsCount = 8;
  11. public const int FilterEntitiesSize = 256;
  12. public const int EntityComponentsCountX2 = EntityComponentsCount * 2;
  13. }
  14. /// <summary>
  15. /// Fast List replacement for growing only collections.
  16. /// </summary>
  17. /// <typeparam name="T">Type of item.</typeparam>
  18. public class EcsGrowList<T> {
  19. public T[] Items;
  20. public int Count;
  21. [MethodImpl (MethodImplOptions.AggressiveInlining)]
  22. public EcsGrowList (int capacity) {
  23. Items = new T[capacity];
  24. Count = 0;
  25. }
  26. [MethodImpl (MethodImplOptions.AggressiveInlining)]
  27. public void Add (T item) {
  28. if (Items.Length == Count) {
  29. Array.Resize (ref Items, Items.Length << 1);
  30. }
  31. Items[Count++] = item;
  32. }
  33. [MethodImpl (MethodImplOptions.AggressiveInlining)]
  34. public void EnsureCapacity (int count) {
  35. if (Items.Length < count) {
  36. var len = Items.Length << 1;
  37. while (len <= count) {
  38. len <<= 1;
  39. }
  40. Array.Resize (ref Items, len);
  41. }
  42. }
  43. }
  44. }
  45. #if ENABLE_IL2CPP
  46. // Unity IL2CPP performance optimization attribute.
  47. namespace Unity.IL2CPP.CompilerServices {
  48. enum Option {
  49. NullChecks = 1,
  50. ArrayBoundsChecks = 2
  51. }
  52. [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
  53. class Il2CppSetOptionAttribute : Attribute {
  54. public Option Option { get; private set; }
  55. public object Value { get; private set; }
  56. public Il2CppSetOptionAttribute (Option option, object value) { Option = option; Value = value; }
  57. }
  58. }
  59. #endif