Close we are CLOSE
This commit is contained in:
@@ -15,42 +15,79 @@ namespace Awperative;
|
||||
public abstract partial class Behavior : Docker
|
||||
{
|
||||
|
||||
public Scene Scene;
|
||||
public Behavior Parent = null;
|
||||
|
||||
|
||||
//todo tags and order
|
||||
/// <summary>
|
||||
/// Current parent of the Behavior. Can be either Scene or another Behavior.
|
||||
/// </summary>
|
||||
internal Docker Docker;
|
||||
|
||||
|
||||
|
||||
internal void Initiate(Scene __scene, Docker __parent) {
|
||||
Scene = __scene;
|
||||
if (__parent is Behavior behavior) Parent = behavior;
|
||||
/// <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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
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() {}
|
||||
|
||||
/// <summary>
|
||||
/// Called when the Game is Loading.
|
||||
/// </summary>
|
||||
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() {}
|
||||
|
||||
/// <summary>
|
||||
/// Called when the Component is destroyed. Not called when the Game is closed.
|
||||
/// </summary>
|
||||
protected internal virtual void Destroy() {}
|
||||
|
||||
|
||||
}
|
||||
39
Awperative/Kernel/Behavior/Utility.cs
Normal file
39
Awperative/Kernel/Behavior/Utility.cs
Normal 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();
|
||||
|
||||
}
|
||||
@@ -19,7 +19,7 @@ public abstract partial class Docker
|
||||
if(behavior == null) { Debug.LogError("Failed to create component"); return null; }
|
||||
|
||||
_behaviors.Add(behavior);
|
||||
behavior.Initiate(DockerScene, this);
|
||||
behavior.Initiate(this);
|
||||
return behavior;
|
||||
|
||||
}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; }
|
||||
|
||||
behavior.End();
|
||||
behavior.Destroy();
|
||||
_behaviors.Remove(behavior);
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ public abstract partial class Docker
|
||||
{
|
||||
Behavior foundBehavior = Get<Generic>();
|
||||
|
||||
foundBehavior.End();
|
||||
foundBehavior.Destroy();
|
||||
_behaviors.Remove(foundBehavior);
|
||||
}catch { Debug.LogError("Removal failed"); }
|
||||
}
|
||||
@@ -66,7 +66,7 @@ public abstract partial class Docker
|
||||
public void DestroyAll<Generic>() where Generic : Behavior {
|
||||
try {
|
||||
foreach (Behavior component in GetAll<Generic>()) {
|
||||
component.End();
|
||||
component.Destroy();
|
||||
_behaviors.Remove(component);
|
||||
}
|
||||
}catch { Debug.LogError("Removal failed"); }
|
||||
@@ -79,5 +79,20 @@ public abstract partial class Docker
|
||||
_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"); }
|
||||
}
|
||||
}
|
||||
@@ -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 ChainDraw(GameTime __gameTime) { foreach (Behavior component in _behaviors) { component.Draw(__gameTime); } }
|
||||
internal virtual void ChainUpdate() { foreach (Behavior component in _behaviors) { component.Update(); } }
|
||||
internal virtual void ChainDraw() { foreach (Behavior component in _behaviors) { component.Draw(); } }
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -58,7 +58,7 @@ public sealed class Base : Game
|
||||
/// </summary>
|
||||
/// <remarks> Hooks are unable to receive both Update() and Draw()</remarks>
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@ public sealed class Base : Game
|
||||
/// </summary>
|
||||
/// <remarks> Hooks are unable to receive both Update() and Draw()</remarks>
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user