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

@@ -1,124 +0,0 @@
using System;
using System.Collections.Generic;
namespace Awperative;
public sealed partial class Body
{
public Generic AddComponent<Generic>(object[] args) where Generic : Component {
if (SingletonExists<Generic>()) { Debug.LogError("Cannot add component when singleton exists!"); return null; }
Generic 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;
}
public Generic AddComponent<Generic>() where Generic : Component {
if (SingletonExists<Generic>())
throw new Exception("Cannot add component when singleton exists!");
Generic component = (Generic) Activator.CreateInstance(typeof(Generic));
if(component == null)
throw new Exception("Failed to create component!");
components.Add(component);
component.Initiate(this);
ComponentCreatedEvent?.Invoke(this, new ComponentCreateEvent(component, this, scene));
return component;
}
public Generic[] GetComponents<Generic>() where Generic : Component {
List<Component> foundComponents = components.FindAll(x => x.GetType() == typeof(Generic));
if(foundComponents.Count == 0)
throw new Exception("Scene has no components of that type!");
return foundComponents.ToArray() as Generic[];
}
public Generic GetComponent<Generic>() where Generic : Component {
Component foundComponent = components.Find(x => x.GetType() == typeof(Generic));
if(foundComponent == null)
throw new Exception("Scene has no components of that type!");
return foundComponent as Generic;
}
public void RemoveComponent(Component __component) {
__component.End();
components.Remove(__component);
}
public void RemoveComponents<Generic>() where Generic : Component {
Component[] foundComponents = GetComponents<Generic>();
if(foundComponents.Length == 0)
throw new Exception("Scene has no components of that type!");
foreach (Component component in foundComponents) {
component.End();
components.Remove(component);
ComponentDestroyedEvent?.Invoke(this, new ComponentDestroyEvent(component, this, scene));
}
}
public void RemoveComponent<Generic>() where Generic : Component {
Component foundComponent = GetComponent<Generic>();
if(foundComponent == null)
throw new Exception("Scene has no components of that type!");
foundComponent.End();
components.Remove(foundComponent);
ComponentDestroyedEvent?.Invoke(this ,new ComponentDestroyEvent(foundComponent, this, scene));
}
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;
}
public void RecompileComponentOrder() {
components.Sort((a, b) => a.Priority.CompareTo(b.Priority));
components.Reverse();
}
}

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"); }
}
}

View File

@@ -5,8 +5,6 @@ namespace Awperative;
public sealed partial class Body
{
//todo: add component events to scene in v5
public event EventHandler<ComponentCreateEvent> ComponentCreatedEvent;
public event EventHandler<ComponentDestroyEvent> ComponentDestroyedEvent;
}

View File

@@ -7,13 +7,16 @@ namespace Awperative;
public sealed partial class Body
{
public void Unload() { foreach (Component component in components.ToList()) component.Unload(); }
public void Load() { foreach (Component component in components.ToList()) { component.Load(); } }
internal void Unload() { foreach (Component component in _components) component.Unload(); }
internal void Load() { foreach (Component component in _components) { component.Load(); } }
public void Update(GameTime __gameTime) { foreach (Component component in components.ToList()) { component.Update(__gameTime); } }
public void Draw(GameTime __gameTime) { foreach (Component component in components.ToList()) { component.Draw(__gameTime); } }
public void Destroy() { foreach(Component component in components.ToList()) component.Destroy(); }
public void Create() { foreach (Component component in components.ToList()) component.Create(); }
internal void Update(GameTime __gameTime) { foreach (Component component in _components) { component.Update(__gameTime); } }
internal void Draw(GameTime __gameTime) { foreach (Component component in _components) { component.Draw(__gameTime); } }
internal void Destroy() { foreach(Component component in _components) component.Destroy(); }
internal void Create() { foreach (Component component in _components) component.Create(); }
}