Awperative Completely Reworked!
This commit is contained in:
23
Awperative/Kernel/Docker/Addition.cs
Normal file
23
Awperative/Kernel/Docker/Addition.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
|
||||
namespace Awperative;
|
||||
|
||||
public abstract partial class Docker
|
||||
{
|
||||
|
||||
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; }
|
||||
|
||||
_components.Add(behavior);
|
||||
behavior.Initiate(this);
|
||||
return behavior;
|
||||
|
||||
}catch { Debug.LogError("Failed to create component"); return null; }
|
||||
}
|
||||
}
|
||||
14
Awperative/Kernel/Docker/Core.cs
Normal file
14
Awperative/Kernel/Docker/Core.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
|
||||
namespace Awperative;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Base class for all Awperative entities, manages components as a requirement because that is the job of all entities.
|
||||
/// </summary>
|
||||
public abstract partial class Docker
|
||||
{
|
||||
internal HashSet<Behavior> _components;
|
||||
}
|
||||
19
Awperative/Kernel/Docker/Location.cs
Normal file
19
Awperative/Kernel/Docker/Location.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Awperative;
|
||||
|
||||
public abstract partial class 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 _components)
|
||||
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();
|
||||
}
|
||||
}
|
||||
31
Awperative/Kernel/Docker/Removal.cs
Normal file
31
Awperative/Kernel/Docker/Removal.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
namespace Awperative;
|
||||
|
||||
public abstract partial class Docker
|
||||
{
|
||||
public void Remove(Behavior behavior) {
|
||||
|
||||
if(!_components.Contains(behavior)) { Debug.LogError("Body does not have a component of this type"); return; }
|
||||
|
||||
behavior.End();
|
||||
_components.Remove(behavior);
|
||||
}
|
||||
|
||||
public void Remove<Generic>() where Generic : Behavior {
|
||||
try
|
||||
{
|
||||
Behavior foundBehavior = Get<Generic>();
|
||||
|
||||
foundBehavior.End();
|
||||
_components.Remove(foundBehavior);
|
||||
}catch { Debug.LogError("Removal failed"); }
|
||||
}
|
||||
|
||||
public void RemoveAll<Generic>() where Generic : Behavior {
|
||||
try {
|
||||
foreach (Behavior component in GetAll<Generic>()) {
|
||||
component.End();
|
||||
_components.Remove(component);
|
||||
}
|
||||
}catch { Debug.LogError("Removal failed"); }
|
||||
}
|
||||
}
|
||||
19
Awperative/Kernel/Docker/Time.cs
Normal file
19
Awperative/Kernel/Docker/Time.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
namespace Awperative;
|
||||
|
||||
public abstract partial class Docker
|
||||
{
|
||||
internal virtual void ChainUnload() { foreach (Behavior component in _components) component.Unload(); }
|
||||
internal virtual void ChainLoad() { foreach (Behavior component in _components) { component.Load(); } }
|
||||
|
||||
|
||||
|
||||
internal virtual void ChainUpdate(GameTime __gameTime) { foreach (Behavior component in _components) { component.Update(__gameTime); } }
|
||||
internal virtual void ChainDraw(GameTime __gameTime) { foreach (Behavior component in _components) { component.Draw(__gameTime); } }
|
||||
|
||||
|
||||
|
||||
internal virtual void ChainDestroy() { foreach(Behavior component in _components) component.Destroy(); }
|
||||
internal virtual void ChainCreate() { foreach (Behavior component in _components) component.Create(); }
|
||||
}
|
||||
Reference in New Issue
Block a user