diff --git a/src/Component.bf b/src/Component.bf new file mode 100644 index 0000000..59f8afe --- /dev/null +++ b/src/Component.bf @@ -0,0 +1,49 @@ +using System; + +namespace Strawberry +{ + public abstract class Component + { + public Entity Entity { get; private set; } + + public bool Active; + public bool Visible; + + public this(bool active, bool visible) + { + Active = active; + Visible = visible; + } + + public void Added(Entity entity) + { + Entity = entity; + } + + public void Removed() + { + Entity = null; + } + + public virtual void Started() + { + + } + + public virtual void Update() + { + + } + + public virtual void Draw() + { + + } + + [Inline] + public void RemoveSelf() + { + Entity?.Remove(this); + } + } +} diff --git a/src/Components/Timer.bf b/src/Components/Timer.bf new file mode 100644 index 0000000..879de3d --- /dev/null +++ b/src/Components/Timer.bf @@ -0,0 +1,58 @@ +using System; +using System.Diagnostics; + +namespace Strawberry +{ + public class Timer : Component + { + private float value; + + public Action OnComplete ~ delete _; + public bool DestroyOnComplete; + + public this() + : base(false, false) + { + + } + + public this(float value, Action onComplete, bool destroyOnComplete = false) + : base(false, false) + { + Value = value; + OnComplete = onComplete; + DestroyOnComplete = destroyOnComplete; + } + + public float Value + { + get + { + return value; + } + + set + { + this.value = Math.Max(0, value); + Active = (this.value > 0); + } + } + + public override void Update() + { + if (value > 0) + { + value -= Time.Delta; + if (value <= 0) + { + value = 0; + Active = false; + + OnComplete?.Invoke(); + if (DestroyOnComplete) + RemoveSelf(); + } + } + } + } +} diff --git a/src/Entity.bf b/src/Entity.bf index fce064e..c20980d 100644 --- a/src/Entity.bf +++ b/src/Entity.bf @@ -3,7 +3,7 @@ using System.Collections; namespace Strawberry { - public class Entity + public abstract class Entity { public Scene Scene { get; private set; } public int Depth; @@ -12,11 +12,23 @@ namespace Strawberry public bool Collidable = true; public bool DeleteOnRemove = true; + private List components = new List() ~ delete _; + private HashSet toAdd = new HashSet() ~ delete _; + private HashSet toRemove = new HashSet() ~ delete _; + public this(Point position) { Positionf = position; } + public ~this() + { + for (var c in components) + delete c; + for (var c in toAdd) + delete c; + } + public void Added(Scene scene) { Scene = scene; @@ -29,17 +41,22 @@ namespace Strawberry public virtual void Started() { - + for (var c in components) + c.Started(); } public virtual void Update() { - + for (var c in components) + if (c.Active) + c.Update(); } public virtual void Draw() { - + for (var c in components) + if (c.Visible) + c.Draw(); } [Inline] @@ -48,6 +65,48 @@ namespace Strawberry Scene?.Remove(this); } + // ===== Components ===== + + public T Add(T component) where T : Component + { + if (component.Entity == null) + toAdd.Add(component); + return component; + } + + public T Remove(T component) where T : Component + { + if (component.Entity == this) + toRemove.Add(component); + return component; + } + + public void UpdateLists() + { + if (toRemove.Count > 0) + { + for (var c in toRemove) + { + components.Remove(c); + c.Removed(); + delete c; + } + + toRemove.Clear(); + } + + if (toAdd.Count > 0) + { + for (var c in toAdd) + { + components.Add(c); + c.Added(this); + } + + toAdd.Clear(); + } + } + // ===== Position ===== public Vector Positionf; diff --git a/src/Scene.bf b/src/Scene.bf index b87703c..e447be2 100644 --- a/src/Scene.bf +++ b/src/Scene.bf @@ -89,7 +89,13 @@ namespace Strawberry entities.Add(e); e.Added(this); } + } + for (var e in entities) + e.UpdateLists(); + + if (toAdd.Count > 0) + { for (var e in toAdd) e.Started(); diff --git a/src/Static/Calc.bf b/src/Static/Calc.bf index 106fb48..219169b 100644 --- a/src/Static/Calc.bf +++ b/src/Static/Calc.bf @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; namespace Strawberry { @@ -9,5 +10,17 @@ namespace Strawberry { return value > target ? Math.Max(value - maxDelta, target) : Math.Min(value + maxDelta, target); } + + static public void Log() + { + Debug.WriteLine("***"); + } + + static public void Log(T v) + { + String string = scope String; + v.ToString(string); + Debug.WriteLine(string); + } } } diff --git a/src/Numerics/Point.bf b/src/Utils/Point.bf similarity index 100% rename from src/Numerics/Point.bf rename to src/Utils/Point.bf diff --git a/src/Numerics/Rect.bf b/src/Utils/Rect.bf similarity index 100% rename from src/Numerics/Rect.bf rename to src/Utils/Rect.bf diff --git a/src/Numerics/Vector.bf b/src/Utils/Vector.bf similarity index 100% rename from src/Numerics/Vector.bf rename to src/Utils/Vector.bf