mirror of
https://github.com/DCFApixels/DragonECS.git
synced 2025-09-18 01:44:35 +08:00
update
expand functionality of WorldComponents change interface of pools
This commit is contained in:
parent
87bd13a3d0
commit
d7a209d9b0
@ -18,11 +18,8 @@ namespace DCFApixels.DragonECS
|
||||
private bool _isInit;
|
||||
|
||||
#region Properties
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
public EcsMask Mask => mask;
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
public EcsWorld World => source;
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
public bool IsInit => _isInit;
|
||||
#endregion
|
||||
|
||||
@ -169,7 +166,7 @@ namespace DCFApixels.DragonECS
|
||||
#region Iterator
|
||||
public EcsSubjectIterator GetIterator()
|
||||
{
|
||||
return new EcsSubjectIterator(this, World.Entities);
|
||||
return new EcsSubjectIterator(this, source.Entities);
|
||||
}
|
||||
public EcsSubjectIterator GetIteratorFor(EcsReadonlyGroup sourceGroup)
|
||||
{
|
||||
|
@ -2,6 +2,7 @@
|
||||
using DCFApixels.DragonECS.Utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace DCFApixels.DragonECS
|
||||
@ -54,7 +55,10 @@ namespace DCFApixels.DragonECS
|
||||
#endregion
|
||||
|
||||
#region Constructors/Destroy
|
||||
static EcsWorld() => Worlds[0] = new EcsNullWorld();
|
||||
static EcsWorld()
|
||||
{
|
||||
Worlds[0] = new EcsNullWorld();
|
||||
}
|
||||
public EcsWorld() : this(true) { }
|
||||
internal EcsWorld(bool isIndexable)
|
||||
{
|
||||
@ -155,14 +159,44 @@ namespace DCFApixels.DragonECS
|
||||
#endregion
|
||||
|
||||
#region WorldComponents
|
||||
public EcsWorld SetupComponents(IEnumerable<object> components, params object[] componentsParams)
|
||||
{
|
||||
foreach (var component in components)
|
||||
SetComponent(component);
|
||||
foreach (var component in componentsParams)
|
||||
SetComponent(component);
|
||||
return this;
|
||||
}
|
||||
public EcsWorld SetupComponents(params object[] components)
|
||||
{
|
||||
foreach (var component in components)
|
||||
SetComponent(component);
|
||||
return this;
|
||||
}
|
||||
public void SetComponent(object component)
|
||||
{
|
||||
Type componentType = component.GetType();
|
||||
if (componentType.IsValueType || componentType.IsPrimitive)
|
||||
throw new ArgumentException();
|
||||
SetComponentInternal(WorldMetaStorage.GetWorldComponentID(componentType, _worldTypeID), component);
|
||||
}
|
||||
public void SetComponent<T>(T component) where T : class
|
||||
{
|
||||
int index = WorldMetaStorage.GetWorldComponentID<T>(_worldTypeID);
|
||||
SetComponentInternal(WorldMetaStorage.GetWorldComponentID<T>(_worldTypeID), component);
|
||||
}
|
||||
private void SetComponentInternal(int index, object component)
|
||||
{
|
||||
if (index >= _components.Length)
|
||||
Array.Resize(ref _components, _components.Length << 1);
|
||||
_components[index] = component;
|
||||
if (component is IEcsWorldComponent intr)
|
||||
intr.Init(this);
|
||||
|
||||
ref var currentComponent = ref _components[index];
|
||||
if (currentComponent == component)
|
||||
return;
|
||||
if (currentComponent != null && currentComponent is IEcsWorldComponent oldComponentInterface)
|
||||
oldComponentInterface.OnRemovedFromWorld(this);
|
||||
currentComponent = component;
|
||||
if (component is IEcsWorldComponent newComponentInterface)
|
||||
newComponentInterface.OnAddedToWorld(this);
|
||||
}
|
||||
public T GetComponent<T>() where T : class
|
||||
{
|
||||
@ -174,7 +208,10 @@ namespace DCFApixels.DragonECS
|
||||
{
|
||||
int index = WorldMetaStorage.GetWorldComponentID<T>(_worldTypeID);
|
||||
if (index >= _components.Length)
|
||||
Array.Resize(ref _components, _components.Length << 1);
|
||||
{
|
||||
component = null;
|
||||
return false;
|
||||
}
|
||||
component = (T)_components[index];
|
||||
return component != null;
|
||||
}
|
||||
@ -183,7 +220,15 @@ namespace DCFApixels.DragonECS
|
||||
int index = WorldMetaStorage.GetWorldComponentID<T>(_worldTypeID);
|
||||
if (index >= _components.Length)
|
||||
return false;
|
||||
return _components[index] == null;
|
||||
return _components[index] != null;
|
||||
}
|
||||
public void RemoveComponent<T>()
|
||||
{
|
||||
int index = WorldMetaStorage.GetWorldComponentID<T>(_worldTypeID);
|
||||
ref var currentComponent = ref _components[index];
|
||||
if (currentComponent is IEcsWorldComponent componentInterface)
|
||||
componentInterface.OnRemovedFromWorld(this);
|
||||
currentComponent = null;
|
||||
}
|
||||
#endregion
|
||||
|
||||
@ -412,7 +457,8 @@ namespace DCFApixels.DragonECS
|
||||
#region Callbacks Interface
|
||||
public interface IEcsWorldComponent
|
||||
{
|
||||
void Init(EcsWorld world);
|
||||
void OnAddedToWorld(EcsWorld world);
|
||||
void OnRemovedFromWorld(EcsWorld world);
|
||||
}
|
||||
public interface IEcsWorldEventListener
|
||||
{
|
||||
|
@ -7,7 +7,7 @@ using static DCFApixels.DragonECS.EcsPoolThrowHalper;
|
||||
namespace DCFApixels.DragonECS
|
||||
{
|
||||
/// <summary>Pool for IEcsComponent components</summary>
|
||||
public sealed class EcsPool<T> : IEcsPoolImplementation<T>, IEnumerable<T> //IEnumerable<T> - IntelliSense hack
|
||||
public sealed class EcsPool<T> : IEcsPoolImplementation<T>, IEcsStructsPool<T>, IEnumerable<T> //IEnumerable<T> - IntelliSense hack
|
||||
where T : struct, IEcsComponent
|
||||
{
|
||||
private EcsWorld _source;
|
||||
@ -171,8 +171,8 @@ namespace DCFApixels.DragonECS
|
||||
void IEcsPool.AddRaw(int entityID, object dataRaw) => Add(entityID) = (T)dataRaw;
|
||||
object IEcsPool.GetRaw(int entityID) => Read(entityID);
|
||||
void IEcsPool.SetRaw(int entityID, object dataRaw) => Get(entityID) = (T)dataRaw;
|
||||
ref readonly T IEcsPool<T>.Read(int entityID) => ref Read(entityID);
|
||||
ref T IEcsPool<T>.Get(int entityID) => ref Get(entityID);
|
||||
ref readonly T IEcsStructsPool<T>.Read(int entityID) => ref Read(entityID);
|
||||
ref T IEcsStructsPool<T>.Get(int entityID) => ref Get(entityID);
|
||||
#endregion
|
||||
|
||||
#region Listeners
|
||||
|
@ -31,7 +31,7 @@ namespace DCFApixels.DragonECS
|
||||
void RemoveListener(IEcsPoolEventListener listener);
|
||||
#endregion
|
||||
}
|
||||
public interface IEcsPool<T>
|
||||
public interface IEcsStructsPool<T>
|
||||
{
|
||||
ref T Add(int entityID);
|
||||
ref readonly T Read(int entityID);
|
||||
@ -47,7 +47,7 @@ namespace DCFApixels.DragonECS
|
||||
}
|
||||
/// <summary>Only used to implement a custom pool. In other contexts use IEcsPool or IEcsPool<T>.</summary>
|
||||
/// <typeparam name="T">Component type</typeparam>
|
||||
public interface IEcsPoolImplementation<T> : IEcsPool<T>, IEcsPoolImplementation { }
|
||||
public interface IEcsPoolImplementation<T> : IEcsPoolImplementation { }
|
||||
|
||||
public static class EcsPoolThrowHalper
|
||||
{
|
||||
@ -104,9 +104,6 @@ namespace DCFApixels.DragonECS
|
||||
void IEcsPool.SetRaw(int entity, object dataRaw) => throw new NotImplementedException();
|
||||
void IEcsPool.Copy(int fromEntityID, int toEntityID) => throw new NotImplementedException();
|
||||
void IEcsPool.Copy(int fromEntityID, EcsWorld toWorld, int toEntityID) => throw new NotImplementedException();
|
||||
ref NullComponent IEcsPool<NullComponent>.Add(int entityID) => throw new NotImplementedException();
|
||||
ref readonly NullComponent IEcsPool<NullComponent>.Read(int entityID) => throw new NotImplementedException();
|
||||
ref NullComponent IEcsPool<NullComponent>.Get(int entityID) => throw new NotImplementedException();
|
||||
#endregion
|
||||
|
||||
#region Callbacks
|
||||
@ -117,8 +114,8 @@ namespace DCFApixels.DragonECS
|
||||
#endregion
|
||||
|
||||
#region Listeners
|
||||
public void AddListener(IEcsPoolEventListener listener) { }
|
||||
public void RemoveListener(IEcsPoolEventListener listener) { }
|
||||
void IEcsPool.AddListener(IEcsPoolEventListener listener) { }
|
||||
void IEcsPool.RemoveListener(IEcsPoolEventListener listener) { }
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ using static DCFApixels.DragonECS.EcsPoolThrowHalper;
|
||||
|
||||
namespace DCFApixels.DragonECS
|
||||
{
|
||||
public sealed class EcsTagPool<T> : IEcsPoolImplementation<T>, IEnumerable<T> //IEnumerable<T> - IntelliSense hack
|
||||
public sealed class EcsTagPool<T> : IEcsPoolImplementation<T>, IEcsStructsPool<T>, IEnumerable<T> //IEnumerable<T> - IntelliSense hack
|
||||
where T : struct, IEcsTagComponent
|
||||
{
|
||||
private EcsWorld _source;
|
||||
@ -131,19 +131,19 @@ namespace DCFApixels.DragonECS
|
||||
#endregion
|
||||
|
||||
#region Other
|
||||
ref T IEcsPool<T>.Add(int entityID)
|
||||
ref T IEcsStructsPool<T>.Add(int entityID)
|
||||
{
|
||||
Add(entityID);
|
||||
return ref _fakeComponent;
|
||||
}
|
||||
ref readonly T IEcsPool<T>.Read(int entityID)
|
||||
ref readonly T IEcsStructsPool<T>.Read(int entityID)
|
||||
{
|
||||
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
||||
if (!Has(entityID)) ThrowNotHaveComponent<T>(entityID);
|
||||
#endif
|
||||
return ref _fakeComponent;
|
||||
}
|
||||
ref T IEcsPool<T>.Get(int entityID)
|
||||
ref T IEcsStructsPool<T>.Get(int entityID)
|
||||
{
|
||||
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
||||
if (!Has(entityID)) ThrowNotHaveComponent<T>(entityID);
|
||||
|
@ -44,6 +44,8 @@ namespace DCFApixels.DragonECS
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int GetComponentID<T>(int worldID) => Component<T>.Get(worldID);
|
||||
public static int GetComponentID(Type type, int worldID) => _metas[worldID].GetComponentID(type);
|
||||
public static bool IsComponentTypeDeclared(int worldID, Type type) => _metas[worldID].IsDeclaredComponentType(type);
|
||||
public static Type GetComponentType(int worldID, int componentID) => _metas[worldID].GetComponentType(componentID);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int GetPoolID<T>(int worldID) => Pool<T>.Get(worldID);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
@ -52,8 +54,7 @@ namespace DCFApixels.DragonECS
|
||||
public static int GetExecutorID<T>(int worldID) => Executor<T>.Get(worldID);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int GetWorldComponentID<T>(int worldID) => WorldComponent<T>.Get(worldID);
|
||||
public static bool IsComponentTypeDeclared(int worldID, Type type) => _metas[worldID].IsDeclaredType(type);
|
||||
public static Type GetComponentType(int worldID, int componentID) => _metas[worldID].GetComponentType(componentID);
|
||||
public static int GetWorldComponentID(Type type, int worldID) => _metas[worldID].GetWorldComponentID(type);
|
||||
|
||||
|
||||
private abstract class ResizerBase
|
||||
@ -97,11 +98,7 @@ namespace DCFApixels.DragonECS
|
||||
GetIdsArray(type);
|
||||
ref int id = ref _componentTypeArrayPairs[type][token];
|
||||
if (id < 0)
|
||||
{
|
||||
var meta = _metas[token];
|
||||
id = meta.componentCount++;
|
||||
meta.AddType(id, type);
|
||||
}
|
||||
id = _metas[token].DeclareComponentType(type);
|
||||
return id;
|
||||
}
|
||||
}
|
||||
@ -237,7 +234,7 @@ namespace DCFApixels.DragonECS
|
||||
{
|
||||
ref int id = ref ids[token];
|
||||
if (id < 0)
|
||||
id = _metas[token].worldComponentCount++;
|
||||
id = _metas[token].GetWorldComponentID(typeof(T));
|
||||
return id;
|
||||
}
|
||||
private sealed class Resizer : ResizerBase
|
||||
@ -263,27 +260,41 @@ namespace DCFApixels.DragonECS
|
||||
public int subjectsCount;
|
||||
public int executorsCount;
|
||||
public int worldComponentCount;
|
||||
private Type[] _types;
|
||||
private Dictionary<Type, int> _declaredComponentTypes;
|
||||
public void AddType(int id, Type type)
|
||||
private Type[] _types = new Type[10];
|
||||
private Dictionary<Type, int> _declaredComponentTypes = new Dictionary<Type, int>();
|
||||
private Dictionary<Type, int> _declaredWorldComponentTypes = new Dictionary<Type, int>();
|
||||
|
||||
public WorldTypeMeta(Type worldType)
|
||||
{
|
||||
this.worldType = worldType;
|
||||
}
|
||||
|
||||
public int DeclareComponentType(Type type)
|
||||
{
|
||||
int id = componentCount++;
|
||||
if (_types.Length <= id)
|
||||
Array.Resize(ref _types, id + 10);
|
||||
_types[id] = type;
|
||||
|
||||
_declaredComponentTypes.Add(type, id);
|
||||
return id;
|
||||
}
|
||||
public bool IsDeclaredComponentType(Type type) => _declaredComponentTypes.ContainsKey(type);
|
||||
public Type GetComponentType(int componentID) => _types[componentID];
|
||||
public bool IsDeclaredType(Type type) => _declaredComponentTypes.ContainsKey(type);
|
||||
public int GetComponentID(Type type)
|
||||
public int GetComponentID(Type type) => PoolComponentIdArrays.GetComponentID(type, id);
|
||||
|
||||
|
||||
public int DeclareWorldComponentType(Type type)
|
||||
{
|
||||
return PoolComponentIdArrays.GetComponentID(type, id);
|
||||
int id = worldComponentCount++;
|
||||
_declaredWorldComponentTypes.Add(type, id);
|
||||
return id;
|
||||
}
|
||||
public WorldTypeMeta(Type worldType)
|
||||
public bool IsDeclaredWorldComponentType(Type type) => _declaredWorldComponentTypes.ContainsKey(type);
|
||||
public int GetWorldComponentID(Type type)
|
||||
{
|
||||
_types = new Type[10];
|
||||
_declaredComponentTypes = new Dictionary<Type, int>();
|
||||
this.worldType = worldType;
|
||||
if(!_declaredWorldComponentTypes.TryGetValue(type, out int id))
|
||||
id = DeclareWorldComponentType(type);
|
||||
return id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user