Close we are CLOSE

This commit is contained in:
2026-02-13 22:19:55 -05:00
parent bdd9b7bed8
commit 6cb3c2e53f
5 changed files with 120 additions and 29 deletions

View File

@@ -14,43 +14,80 @@ namespace Awperative;
/// </summary> /// </summary>
public abstract partial class Behavior : Docker public abstract partial class Behavior : Docker
{ {
public Scene Scene;
public Behavior Parent = null;
//todo tags and order
internal void Initiate(Scene __scene, Docker __parent) { /// <summary>
Scene = __scene; /// Current parent of the Behavior. Can be either Scene or another Behavior.
if (__parent is Behavior behavior) Parent = behavior; /// </summary>
internal Docker Docker;
/// <summary>
/// Identifiers for Behaviors.
/// </summary>
public List<string> Tags;
/// <summary>
/// Order for when Behaviors are called on. Only applies between Components on the same Docker.
/// </summary>
public int Priority;
/// <summary>
/// To be called when the Behavior is created.
/// </summary>
/// <param name="__parent"> Docker that this spawned in this Behavior</param>
internal void Initiate(Docker __parent) {
Docker = __parent;
Create(); Create();
} }
internal void End() {
Destroy();
}
/// <summary>
/// Called when the Game is Closing; does not always happen depending on if it is Force Closed.
/// </summary>
protected internal virtual void Unload() {} protected internal virtual void Unload() {}
/// <summary>
/// Called when the Game is Loading.
/// </summary>
protected internal virtual void Load() {} protected internal virtual void Load() {}
//You know what these do
protected internal virtual void Update(GameTime __gameTime) {}
protected internal virtual void Draw(GameTime __gameTime) {}
//component/body/scene is being created or destroyed /// <summary>
/// Called every frame before Draw, it is recommended to do any Non-Drawing update logic here.
/// </summary>
protected internal virtual void Update() {}
/// <summary>
/// Called after Update when the screen is being drawn. Please only put Drawing related logic here.
/// </summary>
protected internal virtual void Draw() {}
/// <summary>
/// Called when the Component is created.
/// </summary>
protected internal virtual void Create() {} protected internal virtual void Create() {}
/// <summary>
/// Called when the Component is destroyed. Not called when the Game is closed.
/// </summary>
protected internal virtual void Destroy() {} protected internal virtual void Destroy() {}
} }

View File

@@ -0,0 +1,39 @@
using System.Collections.Generic;
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 Docker[] Dockers => __QueryDockers();
private Docker[] __QueryDockers()
{
List<Docker> returnValue = [];
Docker currentDocker = Docker;
while (!(currentDocker is Scene))
{
if (currentDocker is Behavior behavior)
{
returnValue.Add(behavior);
currentDocker = behavior.Docker;
}
}
returnValue.Add(currentDocker);
return returnValue.ToArray();
}
public Behavior[] Parents => __QueryParents();
}

View File

@@ -19,7 +19,7 @@ public abstract partial class Docker
if(behavior == null) { Debug.LogError("Failed to create component"); return null; } if(behavior == null) { Debug.LogError("Failed to create component"); return null; }
_behaviors.Add(behavior); _behaviors.Add(behavior);
behavior.Initiate(DockerScene, this); behavior.Initiate(this);
return behavior; return behavior;
}catch { Debug.LogError("Failed to create component"); return null; } }catch { Debug.LogError("Failed to create component"); return null; }
@@ -49,7 +49,7 @@ public abstract partial class Docker
if(!_behaviors.Contains(behavior)) { Debug.LogError("Body does not have a component of this type"); return; } if(!_behaviors.Contains(behavior)) { Debug.LogError("Body does not have a component of this type"); return; }
behavior.End(); behavior.Destroy();
_behaviors.Remove(behavior); _behaviors.Remove(behavior);
} }
@@ -58,7 +58,7 @@ public abstract partial class Docker
{ {
Behavior foundBehavior = Get<Generic>(); Behavior foundBehavior = Get<Generic>();
foundBehavior.End(); foundBehavior.Destroy();
_behaviors.Remove(foundBehavior); _behaviors.Remove(foundBehavior);
}catch { Debug.LogError("Removal failed"); } }catch { Debug.LogError("Removal failed"); }
} }
@@ -66,7 +66,7 @@ public abstract partial class Docker
public void DestroyAll<Generic>() where Generic : Behavior { public void DestroyAll<Generic>() where Generic : Behavior {
try { try {
foreach (Behavior component in GetAll<Generic>()) { foreach (Behavior component in GetAll<Generic>()) {
component.End(); component.Destroy();
_behaviors.Remove(component); _behaviors.Remove(component);
} }
}catch { Debug.LogError("Removal failed"); } }catch { Debug.LogError("Removal failed"); }
@@ -79,5 +79,20 @@ public abstract partial class Docker
_behaviors.Remove(behavior); _behaviors.Remove(behavior);
} }
public void Remove< 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

@@ -9,8 +9,8 @@ public abstract partial class Docker
internal virtual void ChainUpdate(GameTime __gameTime) { foreach (Behavior component in _behaviors) { component.Update(__gameTime); } } internal virtual void ChainUpdate() { foreach (Behavior component in _behaviors) { component.Update(); } }
internal virtual void ChainDraw(GameTime __gameTime) { foreach (Behavior component in _behaviors) { component.Draw(__gameTime); } } internal virtual void ChainDraw() { foreach (Behavior component in _behaviors) { component.Draw(); } }

View File

@@ -58,7 +58,7 @@ public sealed class Base : Game
/// </summary> /// </summary>
/// <remarks> Hooks are unable to receive both Update() and Draw()</remarks> /// <remarks> Hooks are unable to receive both Update() and Draw()</remarks>
protected override void Update(GameTime __gameTime) { protected override void Update(GameTime __gameTime) {
foreach(Scene scene in Awperative.LoadedScenes.ToList()) scene.ChainUpdate(__gameTime); foreach(Scene scene in Awperative.LoadedScenes.ToList()) scene.ChainUpdate();
base.Update(__gameTime); base.Update(__gameTime);
} }
@@ -71,7 +71,7 @@ public sealed class Base : Game
/// </summary> /// </summary>
/// <remarks> Hooks are unable to receive both Update() and Draw()</remarks> /// <remarks> Hooks are unable to receive both Update() and Draw()</remarks>
protected override void Draw(GameTime __gameTime) { protected override void Draw(GameTime __gameTime) {
foreach(Scene scene in Awperative.LoadedScenes.ToList()) scene.ChainDraw(__gameTime); foreach(Scene scene in Awperative.LoadedScenes.ToList()) scene.ChainDraw();
base.Draw(__gameTime); base.Draw(__gameTime);
} }