This commit is contained in:
2026-02-14 17:22:42 -05:00
parent 44a9ce41f3
commit 843979e10b
12 changed files with 383 additions and 203 deletions

View File

@@ -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<Docker> Dockers => __QueryDockers();
private ImmutableArray<Docker> __QueryDockers() {
List<Docker> 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<Docker>(returnValue.ToArray());
}
public Behavior Parent => __QueryParent();
private Behavior __QueryParent() {
if (Docker is Behavior behavior)
return behavior;
return null;
}
public ImmutableArray<Behavior> Parents => __QueryBehaviors();
private ImmutableArray<Behavior> __QueryBehaviors() {
List<Behavior> returnValue = [];
Docker currentDocker = Docker;
while (!(currentDocker is Scene))
if (currentDocker is Behavior behavior) {
returnValue.Add(behavior);
currentDocker = behavior.Docker;
}
return ImmutableArray.Create<Behavior>(returnValue.ToArray());
}
}

View File

@@ -12,7 +12,7 @@ namespace Awperative;
/// Anything that inherits Component is built to work in any DockerEntity, which leads to generic /// 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. /// Assumptions. If you want to make a body specific or scene specific component both classes are available.
/// </summary> /// </summary>
public abstract partial class Behavior : Docker public abstract partial class Component : Container
{ {
@@ -20,7 +20,7 @@ public abstract partial class Behavior : Docker
/// <summary> /// <summary>
/// Current parent of the Behavior. Can be either Scene or another Behavior. /// Current parent of the Behavior. Can be either Scene or another Behavior.
/// </summary> /// </summary>
internal Docker Docker; internal Container Container;
@@ -44,8 +44,8 @@ public abstract partial class Behavior : Docker
/// To be called when the Behavior is created. /// To be called when the Behavior is created.
/// </summary> /// </summary>
/// <param name="__parent"> Docker that this spawned in this Behavior</param> /// <param name="__parent"> Docker that this spawned in this Behavior</param>
internal void Initiate(Docker __parent) { internal void Initiate(Container __parent) {
Docker = __parent; Container = __parent;
Create(); Create();
} }

View File

@@ -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<Container> Dockers => __QueryDockers();
private ImmutableArray<Container> __QueryDockers() {
List<Container> 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<Container>(returnValue.ToArray());
}
public Component Parent => __QueryParent();
private Component __QueryParent() {
if (Container is Component behavior)
return behavior;
return null;
}
public ImmutableArray<Component> Parents => __QueryBehaviors();
private ImmutableArray<Component> __QueryBehaviors() {
List<Component> returnValue = [];
Container currentContainer = Container;
while (!(currentContainer is Scene))
if (currentContainer is Component behavior) {
returnValue.Add(behavior);
currentContainer = behavior.Container;
}
return ImmutableArray.Create<Component>(returnValue.ToArray());
}
}

View File

@@ -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<Component> Behaviors => [.._behaviors];
internal HashSet<Component> _behaviors = [];
/// <summary>
/// Add a new Behavior to the Docker; This is the only way they should be created.
/// </summary>
/// <param name="__args"> Arguments to construct the Behavior with</param>
/// <typeparam name="Generic"> Type of Behavior to instantiate</typeparam>
/// <returns></returns>
public Component Add<Generic>(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;
}
/// <summary>
/// Adds a new Behavior to the Docker; This is the only way they should be created.
/// </summary>
/// <typeparam name="Generic"></typeparam>
/// <returns></returns>
public Component Add<Generic>() where Generic : Component => Add<Generic>([]);
/// <summary>
/// Transfers a Behavior to another Docker
/// </summary>
/// <param name="component"> Component to move</param>
/// <param name="__container"> Container to move component to</param>
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()]);
}
/// <summary>
/// Transfers the first found Behavior of a specific type to another Docker
/// </summary>
/// <param name="container"> Container to move component to</param>
/// <typeparam name="Generic"> Type of component</typeparam>
public void Transfer<Generic>(Container container) where Generic : Component =>
Transfer(Get<Generic>(), container);
/// <summary>
/// Transfers all Components in a list to another Container.
/// </summary>
/// <param name="__behaviors"> List of Components to transfer</param>
/// <param name="container"> Container to move Component to</param>
public void TransferAll(IEnumerable<Component> __behaviors, Container container) {
foreach (Component behavior in __behaviors) Transfer(behavior, container); }
/// <summary>
/// Transfers all Components of a type to another Container.
/// </summary>
/// <param name="container"> Target container</param>
/// <typeparam name="Generic"> Type of Components to transfer</typeparam>
public void TransferAll<Generic>(Container container) where Generic : Component => TransferAll(GetAll<Generic>(), container);
/// <summary>
/// Searches and returns the first Component of a type found on the Docker.
/// </summary>
/// <typeparam name="Generic"> The Type of Component to search for</typeparam>
/// <returns></returns>
public Component Get<Generic>() 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;
}
/// <summary>
/// Searches and returns all Components of a type found on the Docker.
/// </summary>
/// <typeparam name="Generic"> The Type of Components to search for</typeparam>
/// <returns></returns>
public ImmutableArray<Component> GetAll<Generic>() where Generic : Component {
Debug.LogAction("Searching for all Components on Docker", ["Type", "Docker"],
[typeof(Generic).ToString(), GetHashCode().ToString()]);
List<Component> 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();
}
/// <summary>
/// Destroys a Component attached to docker
/// </summary>
/// <param name="component"></param>
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<Generic>() where Generic : Component {
try
{
Component foundComponent = Get<Generic>();
foundComponent.Destroy();
_behaviors.Remove(foundComponent);
}catch { Debug.LogError("Removal failed"); }
}
public void DestroyAll<Generic>() where Generic : Component {
try {
foreach (Component component in GetAll<Generic>()) {
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<Generic>() where Generic : Component {
try
{
Component foundComponent = Get<Generic>();
_behaviors.Remove(foundComponent);
}catch { Debug.LogError("Removal failed"); }
}
public void RemoveAll<Generic>() where Generic : Component {
try {
foreach (Component component in GetAll<Generic>()) {
_behaviors.Remove(component);
}
}catch { Debug.LogError("Removal failed"); }
}
}

View File

@@ -8,8 +8,7 @@ namespace Awperative;
/// <summary> /// <summary>
/// Base class for all Awperative entities, manages components as a requirement because that is the job of all entities. /// Base class for all Awperative entities, manages components as a requirement because that is the job of all entities.
/// </summary> /// </summary>
public abstract partial class Docker public abstract partial class Container
{ {
internal Scene DockerScene; internal Scene DockerScene;
public List<Behavior> Behaviors => _behaviors.ToList();
} }

View File

@@ -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(); }
}

View File

@@ -1,107 +0,0 @@
using System;
using System.Collections.Generic;
namespace Awperative;
public abstract partial class Docker
{
internal HashSet<Behavior> _behaviors = [];
internal void Add(Behavior behavior) => _behaviors.Add(behavior);
public Behavior Add<Generic>() where Generic : Behavior => Add<Generic>([]);
public Behavior Add<Generic>(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<Generic>() where Generic : Behavior => GetAll<Generic>()[0];
public Behavior[] GetAll<Generic>() where Generic : Behavior {
List<Behavior> 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<Generic>() where Generic : Behavior {
try
{
Behavior foundBehavior = Get<Generic>();
foundBehavior.Destroy();
_behaviors.Remove(foundBehavior);
}catch { Debug.LogError("Removal failed"); }
}
public void DestroyAll<Generic>() where Generic : Behavior {
try {
foreach (Behavior component in GetAll<Generic>()) {
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<Generic>() where Generic : Behavior {
try
{
Behavior foundBehavior = Get<Generic>();
_behaviors.Remove(foundBehavior);
}catch { Debug.LogError("Removal failed"); }
}
public void RemoveAll<Generic>() where Generic : Behavior {
try {
foreach (Behavior component in GetAll<Generic>()) {
_behaviors.Remove(component);
}
}catch { Debug.LogError("Removal failed"); }
}
}

View File

@@ -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(); }
}

View File

@@ -6,7 +6,7 @@ using Microsoft.Xna.Framework;
namespace Awperative; namespace Awperative;
public sealed partial class Scene : Docker public sealed partial class Scene : Container
{ {
//todo: make useful lolol //todo: make useful lolol
} }

View File

@@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("Awperative")] [assembly: System.Reflection.AssemblyCompanyAttribute("Awperative")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] [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.AssemblyProductAttribute("Awperative")]
[assembly: System.Reflection.AssemblyTitleAttribute("Awperative")] [assembly: System.Reflection.AssemblyTitleAttribute("Awperative")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@@ -1 +1 @@
ff872ce192ff85aa8e9cbca48435b4b8029e2f896396a76931b87ad9637872b3 4c62c36f25605bdea7ac7277f2f36ad2d2ee5cd08efd68a2ab6eb48f091be658