About to remove events

This commit is contained in:
2026-02-08 15:07:02 -05:00
parent e987c8092f
commit 6f16a1170f
19 changed files with 209 additions and 168 deletions

View File

@@ -0,0 +1,28 @@
using System;
namespace Awperative;
public sealed partial class Body
{
public Component AddComponent<Generic>() where Generic : Component => AddComponent<Generic>([]);
public Component AddComponent<Generic>(object[] __args) where Generic : Component {
if (SingletonExists<Generic>()) { Debug.LogError("Cannot add component when singleton exists"); return null; }
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);
ComponentCreatedEvent?.Invoke(this, new ComponentCreateEvent(component, this, Scene));
return component;
}catch { Debug.LogError("Failed to create component"); return null; }
}
}

View File

@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
namespace Awperative;
public sealed partial class Body
{
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();
}
public Generic FindSingleton<Generic>() where Generic : Component
{
foreach (Component component in _components)
if (component.GetType() == typeof(Generic))
if(component.EnforceSingleton)
return (Generic) component;
else
throw new Exception("Component is not a singleton");
throw new Exception("Component not found");
return null;
}
public bool SingletonExists<Generic>() where Generic : Component
{
foreach (Component __component in _components)
if (__component.GetType() == typeof(Generic))
if (__component.EnforceSingleton)
return true;
else
return false;
return false;
}
}

View File

@@ -0,0 +1,33 @@
namespace Awperative;
public sealed partial class Body
{
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);
ComponentDestroyedEvent?.Invoke(this, new ComponentDestroyEvent(foundComponent, this, Scene));
}catch { Debug.LogError("Removal failed"); }
}
public void RemoveComponents<Generic>() where Generic : Component {
try {
foreach (Component component in GetComponents<Generic>()) {
component.End();
_components.Remove(component);
ComponentDestroyedEvent?.Invoke(this, new ComponentDestroyEvent(component, this, Scene));
}
}catch { Debug.LogError("Removal failed"); }
}
}