Components!

This commit is contained in:
Matt Thorson 2020-05-05 19:53:13 -07:00
parent 409a82d6b8
commit bde9a51f9e
8 changed files with 189 additions and 4 deletions

49
src/Component.bf Normal file
View File

@ -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);
}
}
}

58
src/Components/Timer.bf Normal file
View File

@ -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();
}
}
}
}
}

View File

@ -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<Component> components = new List<Component>() ~ delete _;
private HashSet<Component> toAdd = new HashSet<Component>() ~ delete _;
private HashSet<Component> toRemove = new HashSet<Component>() ~ 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>(T component) where T : Component
{
if (component.Entity == null)
toAdd.Add(component);
return component;
}
public T Remove<T>(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;

View File

@ -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();

View File

@ -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>(T v)
{
String string = scope String;
v.ToString(string);
Debug.WriteLine(string);
}
}
}