From 843979e10b102dbae5dab3a082ccb2d1d20e571b Mon Sep 17 00:00:00 2001 From: Avery Norris Date: Sat, 14 Feb 2026 17:22:42 -0500 Subject: [PATCH] Comptime --- Awperative/Kernel/Behavior/Utility.cs | 68 ----- .../{Behavior => Component}/Behavior.md | 0 .../Kernel/{Behavior => Component}/Core.cs | 8 +- Awperative/Kernel/Component/Utility.cs | 68 +++++ Awperative/Kernel/Container/Behaviors.cs | 288 ++++++++++++++++++ .../Kernel/{Docker => Container}/Core.cs | 3 +- Awperative/Kernel/Container/Time.cs | 19 ++ Awperative/Kernel/Docker/Behaviors.cs | 107 ------- Awperative/Kernel/Docker/Time.cs | 19 -- Awperative/Kernel/Scene/Core.cs | 2 +- .../Debug/net8.0/Awperative.AssemblyInfo.cs | 2 +- .../Awperative.AssemblyInfoInputs.cache | 2 +- 12 files changed, 383 insertions(+), 203 deletions(-) delete mode 100644 Awperative/Kernel/Behavior/Utility.cs rename Awperative/Kernel/{Behavior => Component}/Behavior.md (100%) rename Awperative/Kernel/{Behavior => Component}/Core.cs (93%) create mode 100644 Awperative/Kernel/Component/Utility.cs create mode 100644 Awperative/Kernel/Container/Behaviors.cs rename Awperative/Kernel/{Docker => Container}/Core.cs (73%) create mode 100644 Awperative/Kernel/Container/Time.cs delete mode 100644 Awperative/Kernel/Docker/Behaviors.cs delete mode 100644 Awperative/Kernel/Docker/Time.cs diff --git a/Awperative/Kernel/Behavior/Utility.cs b/Awperative/Kernel/Behavior/Utility.cs deleted file mode 100644 index 952a711..0000000 --- a/Awperative/Kernel/Behavior/Utility.cs +++ /dev/null @@ -1,68 +0,0 @@ -using System.Collections.Generic; -using System.Collections.Immutable; - -namespace Awperative; - -public abstract partial class Behavior : Docker -{ - - - - public Scene Scene => __QueryScene(); - private Scene __QueryScene() { - if (Docker is Scene scene) return scene; - if (Docker is Behavior behavior) return behavior.__QueryScene(); - - return null; - } - - - - - - public ImmutableArray Dockers => __QueryDockers(); - private ImmutableArray __QueryDockers() { - List returnValue = []; - Docker currentDocker = Docker; - - while (!(currentDocker is Scene)) - if (currentDocker is Behavior behavior) { - returnValue.Add(currentDocker); - currentDocker = behavior.Docker; - } - returnValue.Add(currentDocker); - - return ImmutableArray.Create(returnValue.ToArray()); - } - - - - - - public Behavior Parent => __QueryParent(); - private Behavior __QueryParent() { - if (Docker is Behavior behavior) - return behavior; - return null; - } - - - - - - public ImmutableArray Parents => __QueryBehaviors(); - private ImmutableArray __QueryBehaviors() { - List returnValue = []; - Docker currentDocker = Docker; - - while (!(currentDocker is Scene)) - if (currentDocker is Behavior behavior) { - returnValue.Add(behavior); - currentDocker = behavior.Docker; - } - return ImmutableArray.Create(returnValue.ToArray()); - } - - - -} \ No newline at end of file diff --git a/Awperative/Kernel/Behavior/Behavior.md b/Awperative/Kernel/Component/Behavior.md similarity index 100% rename from Awperative/Kernel/Behavior/Behavior.md rename to Awperative/Kernel/Component/Behavior.md diff --git a/Awperative/Kernel/Behavior/Core.cs b/Awperative/Kernel/Component/Core.cs similarity index 93% rename from Awperative/Kernel/Behavior/Core.cs rename to Awperative/Kernel/Component/Core.cs index 01629ca..97bed24 100644 --- a/Awperative/Kernel/Behavior/Core.cs +++ b/Awperative/Kernel/Component/Core.cs @@ -12,7 +12,7 @@ namespace Awperative; /// Anything that inherits Component is built to work in any DockerEntity, which leads to generic /// Assumptions. If you want to make a body specific or scene specific component both classes are available. /// -public abstract partial class Behavior : Docker +public abstract partial class Component : Container { @@ -20,7 +20,7 @@ public abstract partial class Behavior : Docker /// /// Current parent of the Behavior. Can be either Scene or another Behavior. /// - internal Docker Docker; + internal Container Container; @@ -44,8 +44,8 @@ public abstract partial class Behavior : Docker /// To be called when the Behavior is created. /// /// Docker that this spawned in this Behavior - internal void Initiate(Docker __parent) { - Docker = __parent; + internal void Initiate(Container __parent) { + Container = __parent; Create(); } diff --git a/Awperative/Kernel/Component/Utility.cs b/Awperative/Kernel/Component/Utility.cs new file mode 100644 index 0000000..1e30bc5 --- /dev/null +++ b/Awperative/Kernel/Component/Utility.cs @@ -0,0 +1,68 @@ +using System.Collections.Generic; +using System.Collections.Immutable; + +namespace Awperative; + +public abstract partial class Component : Container +{ + + + + public Scene Scene => __QueryScene(); + private Scene __QueryScene() { + if (Container is Scene scene) return scene; + if (Container is Component behavior) return behavior.__QueryScene(); + + return null; + } + + + + + + public ImmutableArray Dockers => __QueryDockers(); + private ImmutableArray __QueryDockers() { + List returnValue = []; + Container currentContainer = Container; + + while (!(currentContainer is Scene)) + if (currentContainer is Component behavior) { + returnValue.Add(currentContainer); + currentContainer = behavior.Container; + } + returnValue.Add(currentContainer); + + return ImmutableArray.Create(returnValue.ToArray()); + } + + + + + + public Component Parent => __QueryParent(); + private Component __QueryParent() { + if (Container is Component behavior) + return behavior; + return null; + } + + + + + + public ImmutableArray Parents => __QueryBehaviors(); + private ImmutableArray __QueryBehaviors() { + List returnValue = []; + Container currentContainer = Container; + + while (!(currentContainer is Scene)) + if (currentContainer is Component behavior) { + returnValue.Add(behavior); + currentContainer = behavior.Container; + } + return ImmutableArray.Create(returnValue.ToArray()); + } + + + +} \ No newline at end of file diff --git a/Awperative/Kernel/Container/Behaviors.cs b/Awperative/Kernel/Container/Behaviors.cs new file mode 100644 index 0000000..d074a3b --- /dev/null +++ b/Awperative/Kernel/Container/Behaviors.cs @@ -0,0 +1,288 @@ +using System; +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Linq; + +namespace Awperative; + +public abstract partial class Container +{ + public ImmutableArray Behaviors => [.._behaviors]; + + + internal HashSet _behaviors = []; + + + + /// + /// Add a new Behavior to the Docker; This is the only way they should be created. + /// + /// Arguments to construct the Behavior with + /// Type of Behavior to instantiate + /// + public Component Add(object[] __args) where Generic : Component { + + //Log Action + Debug.LogAction("Adding Component to Docker", ["Type", "Args", "Docker"], + [typeof(Generic).ToString(), "[" + string.Join(", ", __args.SelectMany(x => x.ToString())) + "]", GetHashCode().ToString()]); + + + + //Behavior does not have a constructor that matches the given args + if (typeof(Generic).GetConstructor((Type[])__args) == null) + { + Debug.LogError("Behavior cannot be constructed with the given arguments", + ["Type", "Args"], + [typeof(Generic).ToString(), "[" + string.Join(", ", __args.SelectMany(x => x.ToString())) + "]"]); return null; + }; + + + Component newComponent; + + + //Tries to instantiate behavior, and sends a fail call if an issue occurs. + try { newComponent = (Generic)Activator.CreateInstance(typeof(Generic), __args); } + catch { + Debug.LogError("Behavior creation failed!", ["Type", "Args", "Docker"], + [typeof(Generic).ToString(), "[" + string.Join(", ", __args.SelectMany(x => x.ToString())) + "]", GetHashCode().ToString()]); return null; + } + + + //If behavior is null do not add + if(newComponent == null) { + Debug.LogError("Activator created Null Behavior", ["Type", "Args", "Docker"], + [typeof(Generic).ToString(), "[" + string.Join(", ", __args.SelectMany(x => x.ToString())) + "]", GetHashCode().ToString()]); return null; + } + + + //Add to docker and initialize the new Behavior + _behaviors.Add(newComponent); + newComponent.Initiate(this); + + + //Logs successful action! + Debug.LogState("Successfully Created Behavior and Attached it to Docker", ["Type", "Args", "Docker", "Behavior"], + [typeof(Generic).ToString(), "[" + string.Join(", ", __args.SelectMany(x => x.ToString())) + "]", GetHashCode().ToString(), newComponent.GetHashCode().ToString()]); + + + return newComponent; + } + + + + /// + /// Adds a new Behavior to the Docker; This is the only way they should be created. + /// + /// + /// + public Component Add() where Generic : Component => Add([]); + + + + + + /// + /// Transfers a Behavior to another Docker + /// + /// Component to move + /// Container to move component to + public void Transfer(Component __component, Container __container) { + Debug.LogAction("Transferring Component to Different Docker", ["Component", "Type", "CurrentDocker", "NewDocker"], + [__component.GetHashCode().ToString(), __component.GetType().ToString(), GetHashCode().ToString(), __container.GetHashCode().ToString()]); + + if (!_behaviors.Contains(__component)) { + Debug.LogError("Docker does not have ownership of Behavior", ["Component", "Type", "CurrentDocker", "NewDocker"], + [__component.GetHashCode().ToString(), __component.GetType().ToString(), GetHashCode().ToString(), __container.GetHashCode().ToString()]); return; } + + + + //Update docker lists + __container._behaviors.Add(__component); + _behaviors.Remove(__component); + + + + //Update components parent + __component.Container = __container; + + + + Debug.LogState("Successfully Transferred Component to a new Docker" , ["Component", "Type", "CurrentDocker", "NewDocker"], + [__component.GetHashCode().ToString(), __component.GetType().ToString(), GetHashCode().ToString(), __container.GetHashCode().ToString()]); + } + + + + /// + /// Transfers the first found Behavior of a specific type to another Docker + /// + /// Container to move component to + /// Type of component + public void Transfer(Container container) where Generic : Component => + Transfer(Get(), container); + + + + /// + /// Transfers all Components in a list to another Container. + /// + /// List of Components to transfer + /// Container to move Component to + public void TransferAll(IEnumerable __behaviors, Container container) { + foreach (Component behavior in __behaviors) Transfer(behavior, container); } + + + + /// + /// Transfers all Components of a type to another Container. + /// + /// Target container + /// Type of Components to transfer + public void TransferAll(Container container) where Generic : Component => TransferAll(GetAll(), container); + + + + + + /// + /// Searches and returns the first Component of a type found on the Docker. + /// + /// The Type of Component to search for + /// + public Component Get() where Generic : Component { + + Debug.LogAction("Searching for Component", ["Type", "Docker"], + [typeof(Generic).ToString(), GetHashCode().ToString()]); + + + + //Iterates through the loop and returns if a match is found + foreach (Component component in _behaviors) { + if (component is Generic foundComponent) { + Debug.LogState("Found Component", ["Type", "Behavior", "Docker"], + [typeof(Generic).ToString(), foundComponent.GetHashCode().ToString(), GetHashCode().ToString()]); + return foundComponent; + } + } + + + //Throws error if there is no Component found + Debug.LogError("Docker does not have target Component", ["Type", "Docker"], + [typeof(Generic).ToString(), GetHashCode().ToString()]); return null; + } + + + + + + /// + /// Searches and returns all Components of a type found on the Docker. + /// + /// The Type of Components to search for + /// + public ImmutableArray GetAll() where Generic : Component { + + Debug.LogAction("Searching for all Components on Docker", ["Type", "Docker"], + [typeof(Generic).ToString(), GetHashCode().ToString()]); + + + List foundComponents = []; + + + //Iterates through the loop and returns if a match is found + foreach (Component component in _behaviors) { + if (component is Generic foundComponent) { + foundComponents.Add(foundComponent); + } + } + + + //Throws error if there is no Component found + if (foundComponents.Count == 0) { + Debug.LogError("Docker does not have target Component", ["Type", "Docker"], + [typeof(Generic).ToString(), GetHashCode().ToString()]); + return []; + } + + + + Debug.LogState("Found Components on Docker", ["Components", "Type", "Docker"], + [(foundComponents.SelectMany(x => x.GetHashCode().ToString()) + "]").ToString(), typeof(Generic).ToString(), GetHashCode().ToString()]); + + return foundComponents.ToImmutableArray(); + } + + + + + + /// + /// Destroys a Component attached to docker + /// + /// + public void Destroy(Component component) { + + //Component is null + if (component == null) { + Debug.LogError("Component is null", ["CurrentDocker"], + [GetHashCode().ToString()]); return; + } + + + + //Docker doesn't have Component + if (!_behaviors.Contains(component)) { + Debug.LogError("Docker does not have ownership of Behavior", ["Component", "Type", "CurrentDocker"], + [component.GetHashCode().ToString(), component.GetType().ToString(), GetHashCode().ToString()]); return; + } + + + + + component.Destroy(); + _behaviors.Remove(component); + } + + public void Destroy() where Generic : Component { + try + { + Component foundComponent = Get(); + + foundComponent.Destroy(); + _behaviors.Remove(foundComponent); + }catch { Debug.LogError("Removal failed"); } + } + + public void DestroyAll() where Generic : Component { + try { + foreach (Component component in GetAll()) { + component.Destroy(); + _behaviors.Remove(component); + } + }catch { Debug.LogError("Removal failed"); } + } + + public void Remove(Component component) + { + if(!_behaviors.Contains(component)) { Debug.LogError("Body does not have a component of this type"); return; } + + _behaviors.Remove(component); + } + + public void Remove() where Generic : Component { + try + { + Component foundComponent = Get(); + + _behaviors.Remove(foundComponent); + }catch { Debug.LogError("Removal failed"); } + } + + public void RemoveAll() where Generic : Component { + try { + foreach (Component component in GetAll()) { + _behaviors.Remove(component); + } + }catch { Debug.LogError("Removal failed"); } + } +} \ No newline at end of file diff --git a/Awperative/Kernel/Docker/Core.cs b/Awperative/Kernel/Container/Core.cs similarity index 73% rename from Awperative/Kernel/Docker/Core.cs rename to Awperative/Kernel/Container/Core.cs index 9880831..c65eef4 100644 --- a/Awperative/Kernel/Docker/Core.cs +++ b/Awperative/Kernel/Container/Core.cs @@ -8,8 +8,7 @@ namespace Awperative; /// /// Base class for all Awperative entities, manages components as a requirement because that is the job of all entities. /// -public abstract partial class Docker +public abstract partial class Container { internal Scene DockerScene; - public List Behaviors => _behaviors.ToList(); } \ No newline at end of file diff --git a/Awperative/Kernel/Container/Time.cs b/Awperative/Kernel/Container/Time.cs new file mode 100644 index 0000000..108dd1d --- /dev/null +++ b/Awperative/Kernel/Container/Time.cs @@ -0,0 +1,19 @@ +using Microsoft.Xna.Framework; + +namespace Awperative; + +public abstract partial class Container +{ + internal virtual void ChainUnload() { foreach (Component component in (Component[])[.._behaviors]) component.Unload(); } + internal virtual void ChainLoad() { foreach (Component component in (Component[])[.._behaviors]) { component.Load(); } } + + + + internal virtual void ChainUpdate() { foreach (Component component in (Component[])[.._behaviors]) { component.Update(); } } + internal virtual void ChainDraw() { foreach (Component component in (Component[])[.._behaviors]) { component.Draw(); } } + + + + internal virtual void ChainDestroy() { foreach(Component component in (Component[])[.._behaviors]) component.Destroy(); } + internal virtual void ChainCreate() { foreach (Component component in (Component[])[.._behaviors]) component.Create(); } +} \ No newline at end of file diff --git a/Awperative/Kernel/Docker/Behaviors.cs b/Awperative/Kernel/Docker/Behaviors.cs deleted file mode 100644 index 2cc95cf..0000000 --- a/Awperative/Kernel/Docker/Behaviors.cs +++ /dev/null @@ -1,107 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace Awperative; - -public abstract partial class Docker -{ - - - internal HashSet _behaviors = []; - - internal void Add(Behavior behavior) => _behaviors.Add(behavior); - public Behavior Add() where Generic : Behavior => Add([]); - public Behavior Add(object[] __args) where Generic : Behavior { - if(typeof(Generic).GetConstructor((Type[]) __args) == null) { Debug.LogError("Component does not contain a valid constructor"); return null; }; - - try { - Behavior behavior = (Generic)Activator.CreateInstance(typeof(Generic), __args); - - if(behavior == null) { Debug.LogError("Failed to create component"); return null; } - - _behaviors.Add(behavior); - behavior.Initiate(this); - return behavior; - - }catch { Debug.LogError("Failed to create component"); return null; } - } - - - public void Transfer(Behavior __behavior, Docker __docker) { - if (!_behaviors.Contains(__behavior)) { Debug.LogError("Docker does not have ownership of Behavior"); return; } - - __docker.Add(__behavior); - _behaviors.Remove(__behavior); - - __behavior.Docker = __docker; - } - - - - public Behavior Get() where Generic : Behavior => GetAll()[0]; - public Behavior[] GetAll() where Generic : Behavior { - - List returnValue = []; - foreach (Behavior component in _behaviors) - 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 void Destroy(Behavior behavior) { - - if(!_behaviors.Contains(behavior)) { Debug.LogError("Body does not have a component of this type"); return; } - - behavior.Destroy(); - _behaviors.Remove(behavior); - } - - public void Destroy() where Generic : Behavior { - try - { - Behavior foundBehavior = Get(); - - foundBehavior.Destroy(); - _behaviors.Remove(foundBehavior); - }catch { Debug.LogError("Removal failed"); } - } - - public void DestroyAll() where Generic : Behavior { - try { - foreach (Behavior component in GetAll()) { - component.Destroy(); - _behaviors.Remove(component); - } - }catch { Debug.LogError("Removal failed"); } - } - - public void Remove(Behavior behavior) - { - if(!_behaviors.Contains(behavior)) { Debug.LogError("Body does not have a component of this type"); return; } - - _behaviors.Remove(behavior); - } - - public void Remove() where Generic : Behavior { - try - { - Behavior foundBehavior = Get(); - - _behaviors.Remove(foundBehavior); - }catch { Debug.LogError("Removal failed"); } - } - - public void RemoveAll() where Generic : Behavior { - try { - foreach (Behavior component in GetAll()) { - _behaviors.Remove(component); - } - }catch { Debug.LogError("Removal failed"); } - } -} \ No newline at end of file diff --git a/Awperative/Kernel/Docker/Time.cs b/Awperative/Kernel/Docker/Time.cs deleted file mode 100644 index ea0d10c..0000000 --- a/Awperative/Kernel/Docker/Time.cs +++ /dev/null @@ -1,19 +0,0 @@ -using Microsoft.Xna.Framework; - -namespace Awperative; - -public abstract partial class Docker -{ - internal virtual void ChainUnload() { foreach (Behavior component in _behaviors) component.Unload(); } - internal virtual void ChainLoad() { foreach (Behavior component in _behaviors) { component.Load(); } } - - - - internal virtual void ChainUpdate() { foreach (Behavior component in _behaviors) { component.Update(); } } - internal virtual void ChainDraw() { foreach (Behavior component in _behaviors) { component.Draw(); } } - - - - internal virtual void ChainDestroy() { foreach(Behavior component in _behaviors) component.Destroy(); } - internal virtual void ChainCreate() { foreach (Behavior component in _behaviors) component.Create(); } -} \ No newline at end of file diff --git a/Awperative/Kernel/Scene/Core.cs b/Awperative/Kernel/Scene/Core.cs index dd0077d..74be5a5 100644 --- a/Awperative/Kernel/Scene/Core.cs +++ b/Awperative/Kernel/Scene/Core.cs @@ -6,7 +6,7 @@ using Microsoft.Xna.Framework; namespace Awperative; -public sealed partial class Scene : Docker +public sealed partial class Scene : Container { //todo: make useful lolol } diff --git a/Awperative/obj/Debug/net8.0/Awperative.AssemblyInfo.cs b/Awperative/obj/Debug/net8.0/Awperative.AssemblyInfo.cs index fa616d5..e8efb79 100644 --- a/Awperative/obj/Debug/net8.0/Awperative.AssemblyInfo.cs +++ b/Awperative/obj/Debug/net8.0/Awperative.AssemblyInfo.cs @@ -13,7 +13,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyCompanyAttribute("Awperative")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] -[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+c234315ee38dbfa8b6293c6dd44871d9aeb40991")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+44a9ce41f3fbfbf8cffaddb5d08041308b0f578c")] [assembly: System.Reflection.AssemblyProductAttribute("Awperative")] [assembly: System.Reflection.AssemblyTitleAttribute("Awperative")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] diff --git a/Awperative/obj/Debug/net8.0/Awperative.AssemblyInfoInputs.cache b/Awperative/obj/Debug/net8.0/Awperative.AssemblyInfoInputs.cache index 04b56b3..b2ba7a4 100644 --- a/Awperative/obj/Debug/net8.0/Awperative.AssemblyInfoInputs.cache +++ b/Awperative/obj/Debug/net8.0/Awperative.AssemblyInfoInputs.cache @@ -1 +1 @@ -ff872ce192ff85aa8e9cbca48435b4b8029e2f896396a76931b87ad9637872b3 +4c62c36f25605bdea7ac7277f2f36ad2d2ee5cd08efd68a2ab6eb48f091be658