About to remove events
This commit is contained in:
@@ -82,6 +82,16 @@ public static partial class Debug
|
||||
/// <param name="__parameters"> Names of values to debug</param>
|
||||
/// <param name="__values"> Values to debug</param>
|
||||
public static void LogError(string __message, string[] __parameters, string[] __values) => LogGeneric(__message, "ERR", __parameters, __values);
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Writes the current message to the log file.
|
||||
/// </summary>
|
||||
/// <param name="__message"> Message to debug</param>
|
||||
/// <param name="__condition"> Condition to debug </param>
|
||||
public static void AssertError(bool __condition, string __message) => AssertGeneric(__condition, __message, "ERR", [], []);
|
||||
//todo: add more asserts and overrides
|
||||
|
||||
|
||||
|
||||
@@ -102,4 +112,17 @@ public static partial class Debug
|
||||
|
||||
File.AppendAllText(LogFilePath, output);
|
||||
}
|
||||
|
||||
|
||||
public static void AssertGeneric(bool __condition, string __message, string __callSign, string[] __parameters, string[] __values) {
|
||||
if (!__condition) return;
|
||||
|
||||
string output = "\n\n" + __callSign + "- \"" + __message + "\"\n STK-" + new StackTrace();
|
||||
|
||||
for (int i = 0; i < __parameters.Length || i < __values.Length; i++)
|
||||
output += "\n " + __parameters[i] + "- " + __values[i];
|
||||
|
||||
File.AppendAllText(LogFilePath, output);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,124 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
||||
namespace Awperative;
|
||||
|
||||
public sealed partial class Body
|
||||
{
|
||||
|
||||
public Generic AddComponent<Generic>(object[] args) where Generic : Component {
|
||||
if (SingletonExists<Generic>()) { Debug.LogError("Cannot add component when singleton exists!"); return null; }
|
||||
|
||||
Generic component = (Generic) Activator.CreateInstance(typeof(Generic), args);
|
||||
|
||||
if (component == null) { Debug.LogError("Failed to create component!"); return null; }
|
||||
|
||||
_components.Add(component);
|
||||
component.Initiate(this);
|
||||
|
||||
ComponentCreatedEvent?.Invoke(this, new ComponentCreateEvent(component, this, Scene));
|
||||
|
||||
return component;
|
||||
}
|
||||
|
||||
public Generic AddComponent<Generic>() where Generic : Component {
|
||||
|
||||
if (SingletonExists<Generic>())
|
||||
throw new Exception("Cannot add component when singleton exists!");
|
||||
|
||||
Generic component = (Generic) Activator.CreateInstance(typeof(Generic));
|
||||
|
||||
if(component == null)
|
||||
throw new Exception("Failed to create component!");
|
||||
|
||||
components.Add(component);
|
||||
component.Initiate(this);
|
||||
|
||||
ComponentCreatedEvent?.Invoke(this, new ComponentCreateEvent(component, this, scene));
|
||||
|
||||
return component;
|
||||
}
|
||||
|
||||
public Generic[] GetComponents<Generic>() where Generic : Component {
|
||||
|
||||
List<Component> foundComponents = components.FindAll(x => x.GetType() == typeof(Generic));
|
||||
|
||||
if(foundComponents.Count == 0)
|
||||
throw new Exception("Scene has no components of that type!");
|
||||
|
||||
return foundComponents.ToArray() as Generic[];
|
||||
}
|
||||
|
||||
public Generic GetComponent<Generic>() where Generic : Component {
|
||||
|
||||
Component foundComponent = components.Find(x => x.GetType() == typeof(Generic));
|
||||
|
||||
if(foundComponent == null)
|
||||
throw new Exception("Scene has no components of that type!");
|
||||
|
||||
return foundComponent as Generic;
|
||||
}
|
||||
|
||||
public void RemoveComponent(Component __component) {
|
||||
__component.End();
|
||||
components.Remove(__component);
|
||||
}
|
||||
|
||||
public void RemoveComponents<Generic>() where Generic : Component {
|
||||
|
||||
Component[] foundComponents = GetComponents<Generic>();
|
||||
|
||||
if(foundComponents.Length == 0)
|
||||
throw new Exception("Scene has no components of that type!");
|
||||
|
||||
foreach (Component component in foundComponents) {
|
||||
component.End();
|
||||
components.Remove(component);
|
||||
ComponentDestroyedEvent?.Invoke(this, new ComponentDestroyEvent(component, this, scene));
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveComponent<Generic>() where Generic : Component {
|
||||
|
||||
Component foundComponent = GetComponent<Generic>();
|
||||
|
||||
if(foundComponent == null)
|
||||
throw new Exception("Scene has no components of that type!");
|
||||
|
||||
foundComponent.End();
|
||||
components.Remove(foundComponent);
|
||||
ComponentDestroyedEvent?.Invoke(this ,new ComponentDestroyEvent(foundComponent, this, scene));
|
||||
}
|
||||
|
||||
public Generic FindSingleton<Generic>() where Generic : Component
|
||||
{
|
||||
foreach (Component component in components)
|
||||
if (component.GetType() == typeof(Generic))
|
||||
if(component.EnforceSingleton)
|
||||
return (Generic) component;
|
||||
else
|
||||
throw new Exception("Component is not a singleton");
|
||||
|
||||
throw new Exception("Component not found");
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool SingletonExists<Generic>() where Generic : Component
|
||||
{
|
||||
|
||||
foreach (Component __component in components)
|
||||
if (__component.GetType() == typeof(Generic))
|
||||
if (__component.EnforceSingleton)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void RecompileComponentOrder() {
|
||||
components.Sort((a, b) => a.Priority.CompareTo(b.Priority));
|
||||
components.Reverse();
|
||||
}
|
||||
}
|
||||
28
Awperative/Kernel/Entities/Bodies/Components/Addition.cs
Normal file
28
Awperative/Kernel/Entities/Bodies/Components/Addition.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
|
||||
namespace Awperative;
|
||||
|
||||
public sealed partial class Body
|
||||
{
|
||||
|
||||
public Component AddComponent<Generic>() where Generic : Component => AddComponent<Generic>([]);
|
||||
public Component AddComponent<Generic>(object[] __args) where Generic : Component {
|
||||
|
||||
if (SingletonExists<Generic>()) { Debug.LogError("Cannot add component when singleton exists"); return null; }
|
||||
if(typeof(Generic).GetConstructor((Type[]) __args) == null) { Debug.LogError("Component does not contain a valid constructor"); return null; };
|
||||
|
||||
try {
|
||||
|
||||
Component component = (Generic)Activator.CreateInstance(typeof(Generic), __args);
|
||||
|
||||
if(component == null) { Debug.LogError("Failed to create component"); return null; };
|
||||
|
||||
_components.Add(component);
|
||||
component.Initiate(this);
|
||||
ComponentCreatedEvent?.Invoke(this, new ComponentCreateEvent(component, this, Scene));
|
||||
|
||||
return component;
|
||||
|
||||
}catch { Debug.LogError("Failed to create component"); return null; }
|
||||
}
|
||||
}
|
||||
44
Awperative/Kernel/Entities/Bodies/Components/Location.cs
Normal file
44
Awperative/Kernel/Entities/Bodies/Components/Location.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Awperative;
|
||||
|
||||
public sealed partial class Body
|
||||
{
|
||||
public Component GetComponent<Generic>() where Generic : Component => GetComponents<Generic>()[0];
|
||||
public Component[] GetComponents<Generic>() where Generic : Component {
|
||||
|
||||
List<Component> returnValue = [];
|
||||
foreach (Component 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();
|
||||
}
|
||||
|
||||
public Generic FindSingleton<Generic>() where Generic : Component
|
||||
{
|
||||
foreach (Component component in _components)
|
||||
if (component.GetType() == typeof(Generic))
|
||||
if(component.EnforceSingleton)
|
||||
return (Generic) component;
|
||||
else
|
||||
throw new Exception("Component is not a singleton");
|
||||
|
||||
throw new Exception("Component not found");
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool SingletonExists<Generic>() where Generic : Component
|
||||
{
|
||||
foreach (Component __component in _components)
|
||||
if (__component.GetType() == typeof(Generic))
|
||||
if (__component.EnforceSingleton)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
33
Awperative/Kernel/Entities/Bodies/Components/Removal.cs
Normal file
33
Awperative/Kernel/Entities/Bodies/Components/Removal.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
namespace Awperative;
|
||||
|
||||
public sealed partial class Body
|
||||
{
|
||||
public void RemoveComponent(Component __component) {
|
||||
|
||||
if(!_components.Contains(__component)) { Debug.LogError("Body does not have a component of this type"); return; }
|
||||
|
||||
__component.End();
|
||||
_components.Remove(__component);
|
||||
}
|
||||
|
||||
public void RemoveComponent<Generic>() where Generic : Component {
|
||||
try
|
||||
{
|
||||
Component foundComponent = GetComponent<Generic>();
|
||||
|
||||
foundComponent.End();
|
||||
_components.Remove(foundComponent);
|
||||
ComponentDestroyedEvent?.Invoke(this, new ComponentDestroyEvent(foundComponent, this, Scene));
|
||||
}catch { Debug.LogError("Removal failed"); }
|
||||
}
|
||||
|
||||
public void RemoveComponents<Generic>() where Generic : Component {
|
||||
try {
|
||||
foreach (Component component in GetComponents<Generic>()) {
|
||||
component.End();
|
||||
_components.Remove(component);
|
||||
ComponentDestroyedEvent?.Invoke(this, new ComponentDestroyEvent(component, this, Scene));
|
||||
}
|
||||
}catch { Debug.LogError("Removal failed"); }
|
||||
}
|
||||
}
|
||||
@@ -5,8 +5,6 @@ namespace Awperative;
|
||||
|
||||
public sealed partial class Body
|
||||
{
|
||||
//todo: add component events to scene in v5
|
||||
|
||||
public event EventHandler<ComponentCreateEvent> ComponentCreatedEvent;
|
||||
public event EventHandler<ComponentDestroyEvent> ComponentDestroyedEvent;
|
||||
}
|
||||
@@ -7,13 +7,16 @@ namespace Awperative;
|
||||
|
||||
public sealed partial class Body
|
||||
{
|
||||
public void Unload() { foreach (Component component in components.ToList()) component.Unload(); }
|
||||
|
||||
public void Load() { foreach (Component component in components.ToList()) { component.Load(); } }
|
||||
internal void Unload() { foreach (Component component in _components) component.Unload(); }
|
||||
internal void Load() { foreach (Component component in _components) { component.Load(); } }
|
||||
|
||||
public void Update(GameTime __gameTime) { foreach (Component component in components.ToList()) { component.Update(__gameTime); } }
|
||||
public void Draw(GameTime __gameTime) { foreach (Component component in components.ToList()) { component.Draw(__gameTime); } }
|
||||
|
||||
public void Destroy() { foreach(Component component in components.ToList()) component.Destroy(); }
|
||||
public void Create() { foreach (Component component in components.ToList()) component.Create(); }
|
||||
|
||||
internal void Update(GameTime __gameTime) { foreach (Component component in _components) { component.Update(__gameTime); } }
|
||||
internal void Draw(GameTime __gameTime) { foreach (Component component in _components) { component.Draw(__gameTime); } }
|
||||
|
||||
|
||||
|
||||
internal void Destroy() { foreach(Component component in _components) component.Destroy(); }
|
||||
internal void Create() { foreach (Component component in _components) component.Create(); }
|
||||
}
|
||||
@@ -33,7 +33,7 @@ public sealed partial class Scene
|
||||
List<Body> _bodies = new List<Body>();
|
||||
|
||||
foreach (Body body in bodies)
|
||||
if (body.tags.Contains(tag))
|
||||
if (body._tags.Contains(tag))
|
||||
_bodies.Add(body);
|
||||
|
||||
|
||||
@@ -45,7 +45,7 @@ public sealed partial class Scene
|
||||
|
||||
public Body GetBody(string tag) {
|
||||
foreach (Body body in bodies)
|
||||
if (body.tags.Contains(tag))
|
||||
if (body._tags.Contains(tag))
|
||||
return body;
|
||||
|
||||
throw new Exception("No Body found with the tag " + tag);
|
||||
|
||||
Reference in New Issue
Block a user