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.
69 lines
2.1 KiB
69 lines
2.1 KiB
using System; |
|
using Unity.Collections; |
|
using Unity.Mathematics; |
|
|
|
namespace GameCore.TinyECS |
|
{ |
|
public struct EntityDatabase<TEnum, TData> : IDisposable |
|
where TEnum : unmanaged, IConvertible |
|
where TData : unmanaged, IEntityDataStruct |
|
{ |
|
public TData dataStruct; |
|
public EntityGenerator<TEnum> generator; |
|
public EntityCollection<TEnum> collection; |
|
|
|
public int Count => collection.Count; |
|
|
|
internal EntityDatabase(TEnum type, TData dataStruct, int initCapacity, |
|
AllocatorManager.AllocatorHandle allocator) |
|
{ |
|
generator = new(type, initCapacity, allocator); |
|
collection = new(type, initCapacity, allocator); |
|
this.dataStruct = dataStruct; |
|
} |
|
|
|
public Entity<TEnum> AddEntity() |
|
{ |
|
var entity = generator.Create(); |
|
collection.Add(entity); |
|
dataStruct.Add(); |
|
return entity; |
|
} |
|
|
|
public void RemoveEntity(Entity<TEnum> entity) |
|
{ |
|
generator.Delete(entity); |
|
if (collection.Remove(entity, out var index, out var _)) |
|
dataStruct.Remove(index); |
|
} |
|
|
|
public T GetData<T>(Entity<TEnum> entity) where T : unmanaged |
|
{ |
|
var index = collection.GetIndex(entity); |
|
return dataStruct.GetData<T>(index); |
|
} |
|
|
|
public ref T GetDataRef<T>(Entity<TEnum> entity) where T : unmanaged |
|
{ |
|
var index = collection.GetIndex(entity); |
|
return ref dataStruct.GetDataRef<T>(index); |
|
} |
|
|
|
public void Dispose() |
|
{ |
|
generator.Dispose(); |
|
collection.Dispose(); |
|
dataStruct.Dispose(); |
|
} |
|
} |
|
|
|
public static class EntityDataBaseExtensions |
|
{ |
|
public static EntityDatabase<TEnum, TData> CreateEntityDatabase<TEnum, TData>(this TData dataStruct, TEnum type, int initCapacity) |
|
where TEnum : unmanaged, IConvertible |
|
where TData : unmanaged, IEntityDataStruct |
|
{ |
|
return new EntityDatabase<TEnum, TData>(type, dataStruct, initCapacity, dataStruct.Allocator); |
|
} |
|
} |
|
} |