diff --git a/src/Components/Collision/Hitbox.bf b/src/Components/Collision/Hitbox.bf index 14830a8..de75e71 100644 --- a/src/Components/Collision/Hitbox.bf +++ b/src/Components/Collision/Hitbox.bf @@ -8,6 +8,16 @@ namespace Strawberry public bool Collidable = true; public Rect Rect; + public this(Rect rect) + { + Rect = rect; + } + + public this(int x, int y, int width, int height) + { + Rect = .(x, y, width, height); + } + public void DebugDraw() { Game.Batcher.Rect(SceneHitbox, .Red); @@ -116,52 +126,62 @@ namespace Strawberry Single Collisions */ + [Inline] public bool Check(Point point) { return SceneHitbox.Contains(point); } + [Inline] public bool Check(Rect rect) { return SceneHitbox.Intersects(rect); } + [Inline] public bool Check(Grid grid) { return grid != null && grid.Check(SceneHitbox); } + [Inline] public bool Check(Grid grid, Point offset) { return grid != null && grid.Check(SceneHitbox + offset); } + [Inline] public bool Check(Hitbox other) { return other.Collidable && SceneHitbox.Intersects(other.SceneHitbox); } + [Inline] public bool Check(Hitbox other, Point offset) { return other.Collidable && (SceneHitbox + offset).Intersects(other.SceneHitbox); } + [Inline] public bool CheckOutside(Hitbox other, Point offset) { return other.Collidable && !SceneHitbox.Intersects(other.SceneHitbox) && (SceneHitbox + offset).Intersects(other.SceneHitbox); } - public bool Check(T other) where T : Component, IHasHitbox + [Inline] + public bool Check(IHasHitbox other) { return Check(other.Hitbox); } - public bool Check(T other, Point offset) where T : Component, IHasHitbox + [Inline] + public bool Check(IHasHitbox other, Point offset) { return Check(other.Hitbox, offset); } - public bool CheckOutside(T other, Point offset) where T : Component, IHasHitbox + [Inline] + public bool CheckOutside(IHasHitbox other, Point offset) { return CheckOutside(other.Hitbox, offset); } diff --git a/src/Components/Interfaces/IDebugDraw.bf b/src/Components/Interfaces/IDebugDraw.bf index 1656a03..69e09d3 100644 --- a/src/Components/Interfaces/IDebugDraw.bf +++ b/src/Components/Interfaces/IDebugDraw.bf @@ -1,6 +1,7 @@ +using System; + namespace Strawberry { - [ComponentInterface] public interface IDebugDraw { public void DebugDraw(); diff --git a/src/Components/Interfaces/IDraw.bf b/src/Components/Interfaces/IDraw.bf index e89a4d2..eb39044 100644 --- a/src/Components/Interfaces/IDraw.bf +++ b/src/Components/Interfaces/IDraw.bf @@ -1,6 +1,7 @@ +using System; + namespace Strawberry { - [ComponentInterface] public interface IDraw { public void Draw(); diff --git a/src/Components/Interfaces/IHasHitbox.bf b/src/Components/Interfaces/IHasHitbox.bf index cc86eb2..7f574f9 100644 --- a/src/Components/Interfaces/IHasHitbox.bf +++ b/src/Components/Interfaces/IHasHitbox.bf @@ -1,6 +1,7 @@ +using System; + namespace Strawberry { - [ComponentInterface] public interface IHasHitbox { public Hitbox Hitbox { get; } diff --git a/src/Components/Interfaces/ILateUpdate.bf b/src/Components/Interfaces/ILateUpdate.bf index 01bf170..2edf56d 100644 --- a/src/Components/Interfaces/ILateUpdate.bf +++ b/src/Components/Interfaces/ILateUpdate.bf @@ -1,6 +1,7 @@ +using System; + namespace Strawberry { - [ComponentInterface] public interface ILateUpdate { public void LateUpdate(); diff --git a/src/Components/Interfaces/IUpdate.bf b/src/Components/Interfaces/IUpdate.bf index b04fdac..611c34c 100644 --- a/src/Components/Interfaces/IUpdate.bf +++ b/src/Components/Interfaces/IUpdate.bf @@ -1,6 +1,7 @@ +using System; + namespace Strawberry { - [ComponentInterface] public interface IUpdate { public void Update(); diff --git a/src/Core/ComponentInterfaceAttribute.bf b/src/Core/ComponentInterfaceAttribute.bf deleted file mode 100644 index 613ad4b..0000000 --- a/src/Core/ComponentInterfaceAttribute.bf +++ /dev/null @@ -1,9 +0,0 @@ -using System; - -namespace Strawberry -{ - public struct ComponentInterfaceAttribute : Attribute - { - - } -} diff --git a/src/Core/Scene.bf b/src/Core/Scene.bf index 9d5a302..bcf7458 100644 --- a/src/Core/Scene.bf +++ b/src/Core/Scene.bf @@ -7,18 +7,13 @@ namespace Strawberry { public float TimeStarted { get; private set; } - private List entities; - private HashSet toRemove; - private HashSet toAdd; - private Dictionary> componentTracker; + private List entities = new .() ~ delete _; + private HashSet toRemove = new .() ~ delete _; + private HashSet toAdd = new .() ~ delete _; + private Dictionary> componentTracker = new .() ~ DeleteDictionaryAndValues!(_); public this() { - entities = new List(); - toAdd = new HashSet(); - toRemove = new HashSet(); - - componentTracker = new Dictionary>(); for (let type in Tracker.AssignmentLists.Keys) componentTracker.Add(type, new List()); for (let type in Tracker.Interfaces) @@ -30,18 +25,10 @@ namespace Strawberry for (var e in entities) if (e.DeleteOnRemove) delete e; - delete entities; for (var e in toAdd) if (e.DeleteOnRemove) delete e; - delete toAdd; - - delete toRemove; - - for (let list in componentTracker.Values) - delete list; - delete componentTracker; } public virtual void Started() @@ -51,16 +38,20 @@ namespace Strawberry public virtual void Update() { + ForEach(scope (u) => u.Update()); + ForEach(scope (u) => u.LateUpdate()); UpdateLists(); - - } public virtual void Draw() { - + ForEach(scope (d) => d.Draw()); } + /* + Entities + */ + public Entity Add(Entity e) { if (e.Scene == null) @@ -106,7 +97,9 @@ namespace Strawberry e.[Friend]AwakeCheck(); } - // Tracking + /* + Tracking + */ private void TrackComponent(Component c) { @@ -120,7 +113,9 @@ namespace Strawberry componentTracker[t].Remove(c); } - // Time + /* + Time + */ public float TimeElapsed => Time.Elapsed - TimeStarted; public float PreviousTimeElapsed => Time.PreviousElapsed - TimeStarted; @@ -263,5 +258,21 @@ namespace Strawberry into.Add(c as T); return into; } + + public void ForEach(delegate void(T) action) where T : interface + { + List list; + if (componentTracker.TryGetValue(typeof(T), out list)) + for (let c in list) + action(c as T); + } + + public void ForEach(delegate void(T) action) where T : Component + { + List list; + if (componentTracker.TryGetValue(typeof(T), out list)) + for (T c in list) + action(c); + } } } diff --git a/src/Static/Tracker.bf b/src/Static/Tracker.bf index 9ca1491..99b7685 100644 --- a/src/Static/Tracker.bf +++ b/src/Static/Tracker.bf @@ -1,18 +1,45 @@ using System.Collections; using System; +using System.Reflection; + namespace Strawberry { static public class Tracker { - static public Dictionary> AssignmentLists = new .() ~ DeleteDictionaryAndValues!(_); + static public List Components = new .() ~ delete _; static public List Interfaces = new .() ~ delete _; + static public Dictionary> AssignmentLists = new .() ~ DeleteDictionaryAndValues!(_); static private void BuildAssignmentLists() { + // Find all component types + for (let type in Type.Types) + if (type != typeof(Component) && type.IsSubtypeOf(typeof(Component))) + Components.Add(type); + // Find all interfaces with ComponentInterfaceAttribute - for (let type in Type.Enumerator()) - if (type.IsInterface && type.HasCustomAttribute()) - Interfaces.Add(type); + for (let type in Type.Types) + { + if (type.IsInterface) + { + bool foundUse = false; + + Find: for (let c in Components) + { + for (let i in c.Interfaces) + { + if (i == type) + { + foundUse = true; + break Find; + } + } + } + + if (foundUse) + Interfaces.Add(type); + } + } /* For each Type that extends Component, we build a list of all the tracked Interfaces it implements. @@ -20,18 +47,54 @@ namespace Strawberry This allows us to retrieve Components by their type or by any of their implemented interface types. */ - for (let type in Type.Enumerator()) + for (let type in Components) { - if (type != typeof(Component) && type.IsSubtypeOf(typeof(Component))) - { - let list = new List(); - list.Add(type); - for (let check in Interfaces) - if (type.IsSubtypeOf(check)) - list.Add(check); + let list = new List(); + list.Add(type); + for (let int in type.Interfaces) + if (Interfaces.Contains(int)) + list.Add(int); - AssignmentLists.Add(type, list); + AssignmentLists.Add(type, list); + } + + Calc.Log(scope => GetTrackedInterfacesInfo); + Calc.Log(scope => GetTrackedTypesInfo); + } + + static public void GetTrackedInterfacesInfo(String buffer) + { + buffer.Append("Tracked Interfaces:\n"); + for (int i = 0; i < Interfaces.Count; i++) + { + buffer.Append("-"); + Interfaces[i].GetName(buffer); + if (i < Interfaces.Count - 1) + buffer.Append('\n'); + } + } + + static public void GetTrackedTypesInfo(String buffer) + { + buffer.Append("Tracked Types:\n"); + + int index = 0; + for (let kv in AssignmentLists) + { + buffer.Append("-"); + kv.key.GetName(buffer); + buffer.Append(" as "); + + for (int i = 0; i < kv.value.Count; i++) + { + kv.value[i].GetName(buffer); + if (i < kv.value.Count - 1) + buffer.Append(", "); } + + index++; + if (index < AssignmentLists.Count) + buffer.Append('\n'); } } }