Finished docking
This commit is contained in:
@@ -1,45 +0,0 @@
|
||||
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 Component FindSingleton<Generic>() where Generic : Component {
|
||||
foreach (Component component in _components)
|
||||
if (component.GetType() == typeof(Generic))
|
||||
if(component.EnforceSingleton)
|
||||
return component;
|
||||
else {
|
||||
Debug.LogError("Component is not a singleton");
|
||||
return null;
|
||||
}
|
||||
|
||||
Debug.LogError("Scene does not contain a component of this type");
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
|
||||
namespace Awperative;
|
||||
|
||||
public sealed partial class Body
|
||||
public sealed partial class Body : DockerEntity
|
||||
{
|
||||
|
||||
|
||||
@@ -80,4 +81,16 @@ public sealed partial class Body
|
||||
|
||||
|
||||
|
||||
internal void Unload() { foreach (Component component in _components) component.Unload(); }
|
||||
internal void Load() { foreach (Component component in _components) { component.Load(); } }
|
||||
|
||||
|
||||
|
||||
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(); }
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
using System.Linq;
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
|
||||
namespace Awperative;
|
||||
|
||||
|
||||
public sealed partial class Body
|
||||
{
|
||||
internal void Unload() { foreach (Component component in _components) component.Unload(); }
|
||||
internal void Load() { foreach (Component component in _components) { component.Load(); } }
|
||||
|
||||
|
||||
|
||||
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(); }
|
||||
}
|
||||
@@ -2,13 +2,11 @@ using System;
|
||||
|
||||
namespace Awperative;
|
||||
|
||||
public sealed partial class Body
|
||||
public abstract partial class DockerEntity
|
||||
{
|
||||
|
||||
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 {
|
||||
@@ -8,14 +8,9 @@ namespace Awperative;
|
||||
/// <summary>
|
||||
/// Base class for all Awperative entities, manages components as a requirement because that is the job of all entities.
|
||||
/// </summary>
|
||||
internal abstract partial class DockerEntity
|
||||
public abstract partial class DockerEntity
|
||||
{
|
||||
public Scene scene;
|
||||
|
||||
public List<Component> Components => _components.ToList();
|
||||
|
||||
|
||||
|
||||
public Scene Scene;
|
||||
|
||||
internal HashSet<Component> _components;
|
||||
}
|
||||
19
Awperative/Kernel/Entities/DockerEntity/Location.cs
Normal file
19
Awperative/Kernel/Entities/DockerEntity/Location.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Awperative;
|
||||
|
||||
public abstract partial class DockerEntity
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace Awperative;
|
||||
|
||||
public sealed partial class Body
|
||||
public abstract partial class DockerEntity
|
||||
{
|
||||
public void RemoveComponent(Component __component) {
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
namespace Awperative;
|
||||
|
||||
|
||||
internal abstract partial class DockerEntity
|
||||
{
|
||||
|
||||
}
|
||||
@@ -1,134 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
||||
namespace Awperative;
|
||||
|
||||
|
||||
public sealed partial class Scene : DockerEntity
|
||||
{
|
||||
|
||||
public List<Behavior> behaviors { get; private set; } = [];
|
||||
|
||||
//todo: use extern keyword to make transform ambiguous to support potential 3D games
|
||||
|
||||
|
||||
public Generic AddBehavior<Generic>(object[] args) where Generic : Behavior {
|
||||
|
||||
if (SingletonExists<Generic>())
|
||||
throw new Exception("Cannot add behavior when singleton exists!");
|
||||
|
||||
Generic behavior = (Generic) Activator.CreateInstance(typeof(Generic), args);
|
||||
|
||||
if(behavior == null)
|
||||
throw new Exception("Failed to create behavior!");
|
||||
|
||||
behaviors.Add(behavior);
|
||||
behavior.Initiate(this);
|
||||
|
||||
BehaviorCreatedEvent?.Invoke(this, new BehaviorCreateEvent(behavior, this));
|
||||
|
||||
return behavior;
|
||||
}
|
||||
|
||||
public Generic AddBehavior<Generic>() where Generic : Behavior {
|
||||
|
||||
if (SingletonExists<Generic>())
|
||||
throw new Exception("Cannot add behavior when singleton exists!");
|
||||
|
||||
Generic behavior = (Generic) Activator.CreateInstance(typeof(Generic));
|
||||
|
||||
if(behavior == null)
|
||||
throw new Exception("Failed to create behavior!");
|
||||
|
||||
behaviors.Add(behavior);
|
||||
behavior.Initiate(this);
|
||||
|
||||
BehaviorCreatedEvent?.Invoke(this, new BehaviorCreateEvent(behavior, this));
|
||||
|
||||
return behavior;
|
||||
}
|
||||
|
||||
public Generic[] GetBehaviors<Generic>() where Generic : Behavior {
|
||||
|
||||
List<Behavior> foundBehaviors = behaviors.FindAll(x => x.GetType() == typeof(Generic));
|
||||
|
||||
if(foundBehaviors.Count == 0)
|
||||
throw new Exception("Scene has no behaviors of that type!");
|
||||
|
||||
return foundBehaviors.ToArray() as Generic[];
|
||||
}
|
||||
|
||||
public Generic GetBehavior<Generic>() where Generic : Behavior {
|
||||
|
||||
Behavior foundBehavior = behaviors.Find(x => x.GetType() == typeof(Generic));
|
||||
|
||||
if(foundBehavior == null)
|
||||
throw new Exception("Scene has no behaviors of that type!");
|
||||
|
||||
return foundBehavior as Generic;
|
||||
}
|
||||
|
||||
public void RemoveBehaviors<Generic>() where Generic : Behavior {
|
||||
|
||||
Behavior[] foundBehaviors = GetBehaviors<Generic>();
|
||||
|
||||
if(foundBehaviors.Length == 0)
|
||||
throw new Exception("Scene has no behaviors of that type!");
|
||||
|
||||
foreach (Behavior behavior in foundBehaviors) {
|
||||
behavior.End();
|
||||
behaviors.Remove(behavior);
|
||||
BehaviorDestroyedEvent?.Invoke(this, new BehaviorDestroyEvent(behavior, this));
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveBehavior<Generic>() where Generic : Behavior {
|
||||
|
||||
Behavior foundBehavior = GetBehavior<Generic>();
|
||||
|
||||
if(foundBehavior == null)
|
||||
throw new Exception("Scene has no behaviors of that type!");
|
||||
|
||||
foundBehavior.End();
|
||||
behaviors.Remove(foundBehavior);
|
||||
BehaviorDestroyedEvent?.Invoke(this ,new BehaviorDestroyEvent(foundBehavior, this));
|
||||
}
|
||||
|
||||
public void RemoveBehavior(Behavior __behavior) {
|
||||
__behavior.End();
|
||||
behaviors.Remove(__behavior);
|
||||
BehaviorDestroyedEvent?.Invoke(this, new BehaviorDestroyEvent(__behavior, this));
|
||||
}
|
||||
|
||||
public Generic FindSingleton<Generic>() where Generic : Behavior
|
||||
{
|
||||
foreach (Behavior __behavior in behaviors)
|
||||
if (__behavior.GetType() == typeof(Generic))
|
||||
if(__behavior.EnforceSingleton)
|
||||
return (Generic) __behavior;
|
||||
else
|
||||
throw new Exception("Behavior is not a singleton");
|
||||
|
||||
throw new Exception("Behavior not found");
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool SingletonExists<Generic>() where Generic : Behavior
|
||||
{
|
||||
|
||||
foreach (Behavior __behavior in behaviors)
|
||||
if (__behavior.GetType() == typeof(Generic))
|
||||
if (__behavior.EnforceSingleton)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void RecompileBehaviorOrder() {
|
||||
behaviors.Sort((a, b) => a.Priority.CompareTo(b.Priority));
|
||||
behaviors.Reverse();
|
||||
}
|
||||
}
|
||||
@@ -7,8 +7,6 @@ namespace Awperative;
|
||||
|
||||
public sealed partial class Scene : DockerEntity
|
||||
{
|
||||
public List<Body> bodies { get; private set; } = [];
|
||||
|
||||
public Body AddBody(Transform __transform) {
|
||||
Body body = new Body(this, __transform);
|
||||
bodies.Add(body);
|
||||
|
||||
@@ -1,10 +1,35 @@
|
||||
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
namespace Awperative;
|
||||
|
||||
public sealed partial class Scene : DockerEntity
|
||||
{
|
||||
|
||||
public List<Body> bodies { get; private set; } = [];
|
||||
|
||||
public void Unload() {
|
||||
foreach (Component component in _components) component.Unload();
|
||||
foreach (Body body in bodies.ToList()) body.Unload();
|
||||
}
|
||||
|
||||
public void Load() {
|
||||
foreach (Component component in _components) component.Load();
|
||||
foreach (Body body in bodies.ToList()) { body.Load(); }
|
||||
}
|
||||
|
||||
public void Update(GameTime __gameTime) {
|
||||
foreach (Component component in _components) component.Update(__gameTime);
|
||||
foreach (Body body in bodies.ToList()) { body.Update(__gameTime); }
|
||||
}
|
||||
|
||||
public void Draw(GameTime __gameTime) {
|
||||
foreach (Component component in _components) component.Draw(__gameTime);
|
||||
foreach (Body body in bodies.ToList()) { body.Draw(__gameTime); }
|
||||
}
|
||||
|
||||
//todo: add scene.destroy in v5
|
||||
}
|
||||
|
||||
@@ -6,9 +6,6 @@ namespace Awperative;
|
||||
|
||||
public sealed partial class Scene : DockerEntity
|
||||
{
|
||||
public event EventHandler<BehaviorCreateEvent> BehaviorCreatedEvent;
|
||||
public event EventHandler<BehaviorDestroyEvent> BehaviorDestroyedEvent;
|
||||
|
||||
|
||||
public event EventHandler<BodyCreateEvent> BodyCreatedEvent;
|
||||
public event EventHandler<BodyDestroyEvent> BodyDestroyedEvent;
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
using System.Linq;
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
|
||||
namespace Awperative;
|
||||
|
||||
public sealed partial class Scene : DockerEntity
|
||||
{
|
||||
|
||||
public void Unload() {
|
||||
foreach (Behavior behavior in behaviors.ToList()) behavior.Unload();
|
||||
foreach (Body body in bodies.ToList()) body.Unload();
|
||||
}
|
||||
|
||||
public void Load() {
|
||||
foreach (Behavior behavior in behaviors.ToList()) { behavior.Load(); }
|
||||
foreach (Body body in bodies.ToList()) { body.Load(); }
|
||||
}
|
||||
|
||||
public void Update(GameTime __gameTime) {
|
||||
foreach (Behavior behavior in behaviors.ToList()) { behavior.Update(__gameTime); }
|
||||
foreach (Body body in bodies.ToList()) { body.Update(__gameTime); }
|
||||
}
|
||||
|
||||
public void Draw(GameTime __gameTime) {
|
||||
foreach (Behavior behavior in behaviors.ToList()) { behavior.Draw(__gameTime); }
|
||||
foreach (Body body in bodies.ToList()) { body.Draw(__gameTime); }
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
namespace Awperative;
|
||||
|
||||
public sealed record BehaviorCreateEvent
|
||||
{
|
||||
public readonly Behavior behavior;
|
||||
public readonly Scene scene;
|
||||
|
||||
internal BehaviorCreateEvent() {}
|
||||
|
||||
internal BehaviorCreateEvent(Behavior __behavior, Scene __scene)
|
||||
{
|
||||
behavior = __behavior;
|
||||
scene = __scene;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
namespace Awperative;
|
||||
|
||||
public sealed record BehaviorDestroyEvent
|
||||
{
|
||||
public readonly Behavior behavior;
|
||||
public readonly Scene scene;
|
||||
|
||||
internal BehaviorDestroyEvent() {}
|
||||
|
||||
internal BehaviorDestroyEvent(Behavior __behavior, Scene __scene)
|
||||
{
|
||||
behavior = __behavior;
|
||||
scene = __scene;
|
||||
}
|
||||
}
|
||||
@@ -4,4 +4,16 @@ namespace Awperative;
|
||||
public class BodyComponent : Component
|
||||
{
|
||||
|
||||
|
||||
public Body Body;
|
||||
|
||||
internal override void Initiate(DockerEntity __docker) {
|
||||
Docker = __docker;
|
||||
|
||||
Body = (Body)__docker;
|
||||
Create();
|
||||
}
|
||||
|
||||
public Transform Transform => Body.transform;
|
||||
|
||||
}
|
||||
@@ -7,23 +7,27 @@ namespace Awperative;
|
||||
|
||||
/// <summary>
|
||||
/// The lowest level scripting class in Awperative. Components are scene level and provide access to all scene level methods, can be applied to any docker and inherited
|
||||
/// Sadly component does not have excessive access to specific types.
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public abstract class Component
|
||||
public abstract partial class Component
|
||||
{
|
||||
internal DockerEntity Parent;
|
||||
public Scene Scene { get; set; }
|
||||
internal DockerEntity Docker;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
internal void Initiate(DockerEntity __parent) {
|
||||
Parent = __parent;
|
||||
internal virtual void Initiate(DockerEntity __docker) {
|
||||
Docker = __docker;
|
||||
Create();
|
||||
}
|
||||
|
||||
|
||||
|
||||
internal void End() {
|
||||
internal virtual void End() {
|
||||
Destroy();
|
||||
}
|
||||
|
||||
26
Awperative/Kernel/Scripting/Component/Methods.cs
Normal file
26
Awperative/Kernel/Scripting/Component/Methods.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
namespace Awperative;
|
||||
|
||||
public abstract partial class Component
|
||||
{
|
||||
|
||||
protected Body AddBody() => Scene.AddBody();
|
||||
protected Body AddBody(Transform __transform) => Scene.AddBody(__transform);
|
||||
|
||||
protected Body GetBody(string __tag) => Scene.GetBody(__tag);
|
||||
protected Body[] GetBodies(string __tag) => Scene.GetBodies(__tag);
|
||||
|
||||
protected void DestroyBody(Body __body) => Scene.DestroyBody(__body);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public Component AddComponent<Generic>() where Generic : Component => Docker.AddComponent<Generic>();
|
||||
public Component AddComponent<Generic>(object[] __args) where Generic : Component => Docker.AddComponent<Generic>(__args);
|
||||
|
||||
public Component GetComponent<Generic>() where Generic : Component => Docker.GetComponent<Generic>();
|
||||
public Component[] GetComponents<Generic>() where Generic : Component => Docker.GetComponents<Generic>();
|
||||
|
||||
public void RemoveComponent<Generic>() where Generic : Component => Docker.RemoveComponent<Generic>();
|
||||
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
|
||||
namespace Awperative;
|
||||
|
||||
public abstract class Component
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public Scene scene;
|
||||
public Body body;
|
||||
|
||||
public bool Enabled = false;
|
||||
public bool EnforceSingleton = false;
|
||||
|
||||
//0 = default highest priority called first, lowest priority called last
|
||||
//todo: add optional parameter for priority at creation
|
||||
public int Priority {
|
||||
get => _priority;
|
||||
set { _priority = value; }
|
||||
} private int _priority = 0;
|
||||
|
||||
|
||||
protected Transform transform => body.transform;
|
||||
|
||||
protected Body AddBody() => scene.AddBody();
|
||||
protected Body AddBody(Transform __transform) => scene.AddBody(__transform);
|
||||
|
||||
protected Body GetBody(string __tag) => scene.GetBody(__tag);
|
||||
protected Body[] GetBodies(string __tag) => scene.GetBodies(__tag);
|
||||
|
||||
protected void DestroyBody(Body __body) => scene.DestroyBody(__body);
|
||||
|
||||
|
||||
|
||||
public Generic AddBehavior<Generic>() where Generic : Behavior => scene.AddBehavior<Generic>();
|
||||
public Generic AddBehavior<Generic>(object[] __args) where Generic : Behavior => scene.AddBehavior<Generic>(__args);
|
||||
|
||||
public Generic GetBehavior<Generic>() where Generic : Behavior => scene.GetBehavior<Generic>();
|
||||
public Generic[] GetBehaviors<Generic>() where Generic : Behavior => scene.GetBehaviors<Generic>();
|
||||
|
||||
|
||||
public void RemoveBehavior<Generic>() where Generic : Behavior => scene.RemoveBehavior<Generic>();
|
||||
|
||||
|
||||
|
||||
public Component AddComponent<Generic>() where Generic : Component => body.AddComponent<Generic>();
|
||||
public Component AddComponent<Generic>(object[] __args) where Generic : Component => body.AddComponent<Generic>(__args);
|
||||
|
||||
public Component GetComponent<Generic>() where Generic : Component => body.GetComponent<Generic>();
|
||||
public Component[] GetComponents<Generic>() where Generic : Component => body.GetComponents<Generic>();
|
||||
|
||||
public void RemoveComponent<Generic>() where Generic : Component => body.RemoveComponent<Generic>();
|
||||
|
||||
|
||||
|
||||
//creation logic
|
||||
internal void Initiate(Body __body)
|
||||
{
|
||||
body = __body;
|
||||
scene = __body.Scene;
|
||||
Create();
|
||||
}
|
||||
|
||||
internal void End()
|
||||
{
|
||||
Destroy();
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
namespace Awperative;
|
||||
|
||||
|
||||
public class hello : BodyComponent
|
||||
{
|
||||
public void Start() {
|
||||
|
||||
this.ExtensionTest();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user