|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- using System.Runtime.CompilerServices;
- using Svelto.DataStructures;
- using Svelto.ECS.Internal;
-
- //todo: once using native memory for unmanaged struct will be optional, this will need to be moved under the Native namespace
- namespace Svelto.ECS
- {
- public static class EntityNativeDBExtensions
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static NB<T> QueryEntitiesAndIndex<T>
- (this EntitiesDB entitiesDb, EGID entityGID, out uint index) where T : unmanaged, IEntityComponent
- {
- if (entitiesDb.QueryEntitiesAndIndexInternal(entityGID, out index, out NB<T> array) == true)
- return array;
-
- throw new EntityNotFoundException(entityGID, typeof(T));
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static NB<T> QueryEntitiesAndIndex<T>
- (this EntitiesDB entitiesDb, uint id, ExclusiveGroupStruct group, out uint index)
- where T : unmanaged, IEntityComponent
- {
- EGID entityGID = new EGID(id, group);
- if (entitiesDb.QueryEntitiesAndIndexInternal(entityGID, out index, out NB<T> array) == true)
- return array;
-
- throw new EntityNotFoundException(entityGID, typeof(T));
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool TryQueryEntitiesAndIndex<T>
- (this EntitiesDB entitiesDb, EGID entityGID, out uint index, out NB<T> array)
- where T : unmanaged, IEntityComponent
- {
- if (entitiesDb.QueryEntitiesAndIndexInternal(entityGID, out index, out array) == true)
- return true;
-
- return false;
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool TryQueryEntitiesAndIndex<T>
- (this EntitiesDB entitiesDb, uint id, ExclusiveGroupStruct group, out uint index, out NB<T> array)
- where T : unmanaged, IEntityComponent
- {
- if (entitiesDb.QueryEntitiesAndIndexInternal(new EGID(id, group), out index, out array) == true)
- return true;
-
- return false;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool TryGetEntity<T>(this EntitiesDB entitiesDb, uint entityID, ExclusiveGroupStruct @group, out T value)
- where T : unmanaged, IEntityComponent
- {
- if (TryQueryEntitiesAndIndex<T>(entitiesDb, entityID, group, out var index, out var array))
- {
- value = array[index];
- return true;
- }
-
- value = default;
- return false;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool TryGetEntity<T>(this EntitiesDB entitiesDb, EGID egid, out T value)
- where T : unmanaged, IEntityComponent
- {
- return TryGetEntity<T>(entitiesDb, egid.entityID, egid.groupID, out value);
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- static bool QueryEntitiesAndIndexInternal<T>
- (this EntitiesDB entitiesDb, EGID entityGID, out uint index, out NB<T> buffer)
- where T : unmanaged, IEntityComponent
- {
- index = 0;
- buffer = default;
- if (entitiesDb.SafeQueryEntityDictionary<T>(entityGID.groupID, out var safeDictionary) == false)
- return false;
-
- if (safeDictionary.TryFindIndex(entityGID.entityID, out index) == false)
- return false;
-
- buffer = (NB<T>) (safeDictionary as ITypeSafeDictionary<T>).GetValues(out _);
-
- return true;
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static ref T QueryEntity<T>
- (this EntitiesDB entitiesDb, EGID entityGID) where T : unmanaged, IEntityComponent
- {
- var array = entitiesDb.QueryEntitiesAndIndex<T>(entityGID, out var index);
-
- return ref array[(int) index];
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static ref T QueryEntity<T>
- (this EntitiesDB entitiesDb, uint id, ExclusiveGroupStruct group) where T : unmanaged, IEntityComponent
- {
- return ref entitiesDb.QueryEntity<T>(new EGID(id, group));
- }
-
- /// <summary>
- /// Expects that only one entity of type T exists in the group
- /// </summary>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static ref T QueryUniqueEntity<T>
- (this EntitiesDB entitiesDb, ExclusiveGroupStruct group) where T : unmanaged, IEntityComponent
- {
- var (entities, entitiescount) = entitiesDb.QueryEntities<T>(@group);
-
- #if DEBUG && !PROFILE_SVELTO
- if (entitiescount == 0)
- throw new ECSException("Unique entity not found '".FastConcat(typeof(T).ToString()).FastConcat("'"));
- if (entitiescount != 1)
- throw new ECSException("Unique entities must be unique! '".FastConcat(typeof(T).ToString())
- .FastConcat("'"));
- #endif
- return ref entities[0];
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static NB<T> GetArrayAndEntityIndex<T>
- (this EGIDMapper<T> mapper, uint entityID, out uint index) where T : unmanaged, IEntityComponent
- {
- if (mapper._map.TryFindIndex(entityID, out index))
- {
- return (NB<T>) mapper._map.GetValues(out _);
- }
-
- throw new ECSException("Entity not found");
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool TryGetArrayAndEntityIndex<T>
- (this EGIDMapper<T> mapper, uint entityID, out uint index, out NB<T> array)
- where T : unmanaged, IEntityComponent
- {
- index = default;
- if (mapper._map != null && mapper._map.TryFindIndex(entityID, out index))
- {
- array = (NB<T>) mapper._map.GetValues(out _);
- return true;
- }
-
- array = default;
- return false;
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static AllGroupsEnumerable<T1> QueryEntities<T1>(this EntitiesDB db)
- where T1 :unmanaged, IEntityComponent
- {
- return new AllGroupsEnumerable<T1>(db);
- }
- }
- }
|