Finished docking
This commit is contained in:
23
Awperative/Kernel/Entities/DockerEntity/Addition.cs
Normal file
23
Awperative/Kernel/Entities/DockerEntity/Addition.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
|
||||
namespace Awperative;
|
||||
|
||||
public abstract partial class DockerEntity
|
||||
{
|
||||
|
||||
public Component AddComponent<Generic>() where Generic : Component => AddComponent<Generic>([]);
|
||||
public Component AddComponent<Generic>(object[] __args) where Generic : Component {
|
||||
if(typeof(Generic).GetConstructor((Type[]) __args) == null) { Debug.LogError("Component does not contain a valid constructor"); return null; };
|
||||
|
||||
try {
|
||||
Component component = (Generic)Activator.CreateInstance(typeof(Generic), __args);
|
||||
|
||||
if(component == null) { Debug.LogError("Failed to create component"); return null; }
|
||||
|
||||
_components.Add(component);
|
||||
component.Initiate(this);
|
||||
return component;
|
||||
|
||||
}catch { Debug.LogError("Failed to create component"); return null; }
|
||||
}
|
||||
}
|
||||
16
Awperative/Kernel/Entities/DockerEntity/Core.cs
Normal file
16
Awperative/Kernel/Entities/DockerEntity/Core.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
|
||||
namespace Awperative;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Base class for all Awperative entities, manages components as a requirement because that is the job of all entities.
|
||||
/// </summary>
|
||||
public abstract partial class DockerEntity
|
||||
{
|
||||
public Scene Scene;
|
||||
|
||||
internal HashSet<Component> _components;
|
||||
}
|
||||
19
Awperative/Kernel/Entities/DockerEntity/Location.cs
Normal file
19
Awperative/Kernel/Entities/DockerEntity/Location.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Awperative;
|
||||
|
||||
public abstract partial class DockerEntity
|
||||
{
|
||||
public Component GetComponent<Generic>() where Generic : Component => GetComponents<Generic>()[0];
|
||||
public Component[] GetComponents<Generic>() where Generic : Component {
|
||||
|
||||
List<Component> returnValue = [];
|
||||
foreach (Component component in _components)
|
||||
if (component is Generic) returnValue.Add(component);
|
||||
|
||||
if(returnValue.Count == 0) { Debug.LogWarning("Scene has no components of this type"); return null; }
|
||||
|
||||
return returnValue.ToArray();
|
||||
}
|
||||
}
|
||||
31
Awperative/Kernel/Entities/DockerEntity/Removal.cs
Normal file
31
Awperative/Kernel/Entities/DockerEntity/Removal.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
namespace Awperative;
|
||||
|
||||
public abstract partial class DockerEntity
|
||||
{
|
||||
public void RemoveComponent(Component __component) {
|
||||
|
||||
if(!_components.Contains(__component)) { Debug.LogError("Body does not have a component of this type"); return; }
|
||||
|
||||
__component.End();
|
||||
_components.Remove(__component);
|
||||
}
|
||||
|
||||
public void RemoveComponent<Generic>() where Generic : Component {
|
||||
try
|
||||
{
|
||||
Component foundComponent = GetComponent<Generic>();
|
||||
|
||||
foundComponent.End();
|
||||
_components.Remove(foundComponent);
|
||||
}catch { Debug.LogError("Removal failed"); }
|
||||
}
|
||||
|
||||
public void RemoveComponents<Generic>() where Generic : Component {
|
||||
try {
|
||||
foreach (Component component in GetComponents<Generic>()) {
|
||||
component.End();
|
||||
_components.Remove(component);
|
||||
}
|
||||
}catch { Debug.LogError("Removal failed"); }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user