|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- using System;
- using System.Runtime.CompilerServices;
- using Svelto.Common;
- using Svelto.DataStructures;
- using Svelto.ECS.Hybrid;
-
- namespace Svelto.ECS
- {
- namespace Native
- {
- public struct EGIDMultiMapper<T> where T : unmanaged, IEntityComponent
- {
- public EGIDMultiMapper
- (SveltoDictionary<ExclusiveGroupStruct,
- SveltoDictionary<uint, T, NativeStrategy<
- SveltoDictionaryNode<uint>>, NativeStrategy<T>, NativeStrategy<int>>,
- ManagedStrategy<SveltoDictionaryNode<ExclusiveGroupStruct>>,
- ManagedStrategy<SveltoDictionary<uint, T, NativeStrategy<SveltoDictionaryNode<uint>>, NativeStrategy<T>
- , NativeStrategy<int>>>, NativeStrategy<int>> dictionary)
- {
- _dic = dictionary;
- }
-
- public int count
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get => _dic.count;
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public ref T Entity(EGID entity)
- {
- #if DEBUG && !PROFILE_SVELTO
- if (Exists(entity) == false)
- throw new Exception("NativeEGIDMultiMapper: Entity not found");
- #endif
- ref var sveltoDictionary = ref _dic.GetValueByRef(entity.groupID);
- return ref sveltoDictionary.GetValueByRef(entity.entityID);
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool Exists(EGID entity)
- {
- return _dic.TryFindIndex(entity.groupID, out var index)
- && _dic.GetDirectValueByRef(index).ContainsKey(entity.entityID);
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool TryGetEntity(EGID entity, out T component)
- {
- component = default;
- return _dic.TryFindIndex(entity.groupID, out var index)
- && _dic.GetDirectValueByRef(index).TryGetValue(entity.entityID, out component);
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool FindIndex(ExclusiveGroupStruct group, uint entityID, out uint index)
- {
- index = 0;
- return _dic.TryFindIndex(group, out var groupIndex) &&
- _dic.GetDirectValueByRef(groupIndex).TryFindIndex(entityID, out index);
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public uint GetIndex(ExclusiveGroupStruct group, uint entityID)
- {
- uint groupIndex = _dic.GetIndex(group);
- return _dic.GetDirectValueByRef(groupIndex).GetIndex(entityID);
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool Exists(ExclusiveGroupStruct group, uint entityID)
- {
- return _dic.TryFindIndex(group, out var groupIndex) &&
- _dic.GetDirectValueByRef(groupIndex).ContainsKey(entityID);
- }
-
- public Type entityType => TypeCache<T>.type;
-
- SveltoDictionary<ExclusiveGroupStruct,
- SveltoDictionary<uint, T, NativeStrategy<SveltoDictionaryNode<uint>>, NativeStrategy<T>,
- NativeStrategy<int>>, ManagedStrategy<SveltoDictionaryNode<ExclusiveGroupStruct>>,
- ManagedStrategy<SveltoDictionary<uint, T, NativeStrategy<SveltoDictionaryNode<uint>>, NativeStrategy<T>,
- NativeStrategy<int>>>, NativeStrategy<int>> _dic;
- }
-
- public interface IEGIDMultiMapper
- {
- bool FindIndex(ExclusiveGroupStruct group, uint entityID, out uint index);
-
- uint GetIndex(ExclusiveGroupStruct group, uint entityID);
-
- bool Exists(ExclusiveGroupStruct group, uint entityID);
-
- Type entityType { get; }
- }
- }
-
- public struct EGIDMultiMapper<T> where T : struct, IEntityViewComponent
- {
- public EGIDMultiMapper
- (SveltoDictionary<ExclusiveGroupStruct,
- SveltoDictionary<uint, T, ManagedStrategy<SveltoDictionaryNode<uint>>, ManagedStrategy<T>,
- ManagedStrategy<int>>, ManagedStrategy<SveltoDictionaryNode<ExclusiveGroupStruct>>,
- ManagedStrategy<SveltoDictionary<uint, T, ManagedStrategy<SveltoDictionaryNode<uint>>, ManagedStrategy<T>,
- ManagedStrategy<int>>>, ManagedStrategy<int>> dictionary)
- {
- _dic = dictionary;
- }
-
- public int count
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get => _dic.count;
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public ref T Entity(EGID entity)
- {
- #if DEBUG && !PROFILE_SVELTO
- if (Exists(entity) == false)
- throw new Exception("NativeEGIDMultiMapper: Entity not found");
- #endif
- ref var sveltoDictionary = ref _dic.GetValueByRef(entity.groupID);
- return ref sveltoDictionary.GetValueByRef(entity.entityID);
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool Exists(EGID entity)
- {
- return _dic.TryFindIndex(entity.groupID, out var index)
- && _dic.GetDirectValueByRef(index).ContainsKey(entity.entityID);
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool TryGetEntity(EGID entity, out T component)
- {
- component = default;
- return _dic.TryFindIndex(entity.groupID, out var index)
- && _dic.GetDirectValueByRef(index).TryGetValue(entity.entityID, out component);
- }
-
- SveltoDictionary<ExclusiveGroupStruct,
- SveltoDictionary<uint, T, ManagedStrategy<SveltoDictionaryNode<uint>>, ManagedStrategy<T>,
- ManagedStrategy<int>>, ManagedStrategy<SveltoDictionaryNode<ExclusiveGroupStruct>>,
- ManagedStrategy<SveltoDictionary<uint, T, ManagedStrategy<SveltoDictionaryNode<uint>>, ManagedStrategy<T>,
- ManagedStrategy<int>>>, ManagedStrategy<int>> _dic;
- }
- }
|