update world component methods

This commit is contained in:
Mikhail 2023-06-18 01:02:57 +08:00
parent 7e0af4da35
commit 7915db562d

View File

@ -159,27 +159,23 @@ namespace DCFApixels.DragonECS
int index = WorldMetaStorage.GetWorldComponentID<T>(_worldTypeID); int index = WorldMetaStorage.GetWorldComponentID<T>(_worldTypeID);
if (index >= _components.Length) if (index >= _components.Length)
Array.Resize(ref _components, _components.Length << 1); Array.Resize(ref _components, _components.Length << 1);
if (component == null)
{
_components[index] = component; _components[index] = component;
if (component is IEcsWorldComponent intr) if (component is IEcsWorldComponent intr)
intr.Init(this); intr.Init(this);
} }
public T GetComponent<T>() where T : class
{
if (!TryGetComponent(out T result))
throw new NullReferenceException();
return result;
} }
public T GetComponent<T>() where T : class, new() public bool TryGetComponent<T>(out T component) where T : class
{ {
int index = WorldMetaStorage.GetWorldComponentID<T>(_worldTypeID); int index = WorldMetaStorage.GetWorldComponentID<T>(_worldTypeID);
if (index >= _components.Length) if (index >= _components.Length)
Array.Resize(ref _components, _components.Length << 1); Array.Resize(ref _components, _components.Length << 1);
var result = _components[index]; component = (T)_components[index];
if (result == null) return component != null;
{
result = new T();
_components[index] = result;
if (result is IEcsWorldComponent component)
component.Init(this);
}
return (T)result;
} }
public bool HasComponent<T>() public bool HasComponent<T>()
{ {