using System; using System.Collections.Generic; namespace Awperative; public sealed partial class Body { public Generic AddComponent(object[] args) where Generic : Component { if (SingletonExists()) throw new Exception("Cannot add component when singleton exists!"); Generic component = (Generic) Activator.CreateInstance(typeof(Generic), args); 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 AddComponent() where Generic : Component { if (SingletonExists()) 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() where Generic : Component { List 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() 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() where Generic : Component { Component[] foundComponents = GetComponents(); 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() where Generic : Component { Component foundComponent = GetComponent(); 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() 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() 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(); } }