using System; using System.Collections.Generic; namespace Gravity.Kernel; public sealed partial class Scene { public List behaviors { get; private set; } = []; //todo: use extern keyword to make transform ambiguous to support potential 3D games public Generic AddBehavior(object[] args) where Generic : Behavior { if (SingletonExists()) throw new Exception("Cannot add behavior when singleton exists!"); Generic behavior = (Generic) Activator.CreateInstance(typeof(Generic), args); if(behavior == null) throw new Exception("Failed to create behavior!"); behaviors.Add(behavior); behavior.Initiate(this); BehaviorCreatedEvent?.Invoke(this, new BehaviorCreateEvent(behavior, this)); return behavior; } public Generic AddBehavior() where Generic : Behavior { if (SingletonExists()) throw new Exception("Cannot add behavior when singleton exists!"); Generic behavior = (Generic) Activator.CreateInstance(typeof(Generic)); if(behavior == null) throw new Exception("Failed to create behavior!"); behaviors.Add(behavior); behavior.Initiate(this); BehaviorCreatedEvent?.Invoke(this, new BehaviorCreateEvent(behavior, this)); return behavior; } public Generic[] GetBehaviors() where Generic : Behavior { List foundBehaviors = behaviors.FindAll(x => x.GetType() == typeof(Generic)); if(foundBehaviors.Count == 0) throw new Exception("Scene has no behaviors of that type!"); return foundBehaviors.ToArray() as Generic[]; } public Generic GetBehavior() where Generic : Behavior { Behavior foundBehavior = behaviors.Find(x => x.GetType() == typeof(Generic)); if(foundBehavior == null) throw new Exception("Scene has no behaviors of that type!"); return foundBehavior as Generic; } public void RemoveBehaviors() where Generic : Behavior { Behavior[] foundBehaviors = GetBehaviors(); if(foundBehaviors.Length == 0) throw new Exception("Scene has no behaviors of that type!"); foreach (Behavior behavior in foundBehaviors) { behavior.End(); behaviors.Remove(behavior); BehaviorDestroyedEvent?.Invoke(this, new BehaviorDestroyEvent(behavior, this)); } } public void RemoveBehavior() where Generic : Behavior { Behavior foundBehavior = GetBehavior(); if(foundBehavior == null) throw new Exception("Scene has no behaviors of that type!"); foundBehavior.End(); behaviors.Remove(foundBehavior); BehaviorDestroyedEvent?.Invoke(this ,new BehaviorDestroyEvent(foundBehavior, this)); } public void RemoveBehavior(Behavior __behavior) { __behavior.End(); behaviors.Remove(__behavior); BehaviorDestroyedEvent?.Invoke(this, new BehaviorDestroyEvent(__behavior, this)); } public Generic FindSingleton() where Generic : Behavior { foreach (Behavior __behavior in behaviors) if (__behavior.GetType() == typeof(Generic)) if(__behavior.EnforceSingleton) return (Generic) __behavior; else throw new Exception("Behavior is not a singleton"); throw new Exception("Behavior not found"); return null; } public bool SingletonExists() where Generic : Behavior { foreach (Behavior __behavior in behaviors) if (__behavior.GetType() == typeof(Generic)) if (__behavior.EnforceSingleton) return true; else return false; return false; } public void RecompileBehaviorOrder() { behaviors.Sort((a, b) => a.Priority.CompareTo(b.Priority)); behaviors.Reverse(); } }