From 610ef2308fff06fea25c0d0ea4ed35190031a0a3 Mon Sep 17 00:00:00 2001 From: Matt Thorson Date: Sat, 19 Sep 2020 22:46:45 -0700 Subject: [PATCH] Entity and Component Ended() --- src/Components/Logic/OnCollide.bf | 10 --- src/Components/Logic/StateMachine.bf | 5 -- src/Components/Logic/Timer.bf | 10 --- src/Components/Logic/Tween.bf | 10 --- src/Core/Component.bf | 7 +- src/Core/Entity.bf | 47 +++++++++++ src/Core/Scene.bf | 22 ++++++ src/Physics/Grid.bf | 112 +++++++++++++++++++++++++++ src/Struct/Cardinals.bf | 5 ++ src/Struct/Vector.bf | 5 +- 10 files changed, 194 insertions(+), 39 deletions(-) diff --git a/src/Components/Logic/OnCollide.bf b/src/Components/Logic/OnCollide.bf index e1a7936..f6cee9d 100644 --- a/src/Components/Logic/OnCollide.bf +++ b/src/Components/Logic/OnCollide.bf @@ -17,11 +17,6 @@ namespace Strawberry delete Action; } - public override void Started() - { - - } - public override void Update() { if (Action != null) @@ -32,10 +27,5 @@ namespace Strawberry break; } } - - public override void Draw() - { - - } } } diff --git a/src/Components/Logic/StateMachine.bf b/src/Components/Logic/StateMachine.bf index 3b56fd2..9cb060d 100644 --- a/src/Components/Logic/StateMachine.bf +++ b/src/Components/Logic/StateMachine.bf @@ -35,11 +35,6 @@ namespace Strawberry CallUpdate(); } - public override void Draw() - { - - } - public void Add(TIndex state, delegate void() enter = null, delegate TIndex() update = null, delegate void() exit = null) { let s = new State(); diff --git a/src/Components/Logic/Timer.bf b/src/Components/Logic/Timer.bf index e4190f5..76cd254 100644 --- a/src/Components/Logic/Timer.bf +++ b/src/Components/Logic/Timer.bf @@ -31,11 +31,6 @@ namespace Strawberry RemoveOnComplete = destroyOnComplete; } - public override void Started() - { - - } - public override void Update() { if (value > 0) @@ -53,11 +48,6 @@ namespace Strawberry } } - public override void Draw() - { - - } - public float Value { [Inline] diff --git a/src/Components/Logic/Tween.bf b/src/Components/Logic/Tween.bf index 00b89dd..6790cb5 100644 --- a/src/Components/Logic/Tween.bf +++ b/src/Components/Logic/Tween.bf @@ -27,11 +27,6 @@ namespace Strawberry Active = true; } - public override void Started() - { - - } - public override void Update() { T = Math.Min(T + Time.Delta, 1); @@ -45,10 +40,5 @@ namespace Strawberry RemoveSelf(); } } - - public override void Draw() - { - - } } } diff --git a/src/Core/Component.bf b/src/Core/Component.bf index 7ebb9a6..814e7fc 100644 --- a/src/Core/Component.bf +++ b/src/Core/Component.bf @@ -25,9 +25,10 @@ namespace Strawberry Entity = null; } - public abstract void Started(); - public abstract void Update(); - public abstract void Draw(); + public virtual void Started() { } + public virtual void Ended() { } + public virtual void Update() { } + public virtual void Draw() { } [Inline] public void RemoveSelf() diff --git a/src/Core/Entity.bf b/src/Core/Entity.bf index e5a3202..8182e46 100644 --- a/src/Core/Entity.bf +++ b/src/Core/Entity.bf @@ -36,6 +36,7 @@ namespace Strawberry private void Removed() { + Ended(); Scene = null; } @@ -45,6 +46,12 @@ namespace Strawberry c.Started(); } + public virtual void Ended() + { + for (var c in components) + c.Ended(); + } + public virtual void Update() { for (var c in components) @@ -382,6 +389,46 @@ namespace Strawberry return null; } + public T LeftmostOutside(Point offset) where T : Entity + { + T ret = null; + for (var e in Scene.All(scope List())) + if (CheckOutside(e, offset) && (ret == null || e.Left < ret.Left)) + ret = e; + + return ret; + } + + public T RightmostOutside(Point offset) where T : Entity + { + T ret = null; + for (var e in Scene.All(scope List())) + if (CheckOutside(e, offset) && (ret == null || e.Right > ret.Right)) + ret = e; + + return ret; + } + + public T TopmostOutside(Point offset) where T : Entity + { + T ret = null; + for (var e in Scene.All(scope List())) + if (CheckOutside(e, offset) && (ret == null || e.Top < ret.Top)) + ret = e; + + return ret; + } + + public T BottommostOutside(Point offset) where T : Entity + { + T ret = null; + for (var e in Scene.All(scope List())) + if (CheckOutside(e, offset) && (ret == null || e.Bottom > ret.Bottom)) + ret = e; + + return ret; + } + public List All(List into) where T : Entity { for (var e in Scene.All(scope List())) diff --git a/src/Core/Scene.bf b/src/Core/Scene.bf index 5236b84..646f1f8 100644 --- a/src/Core/Scene.bf +++ b/src/Core/Scene.bf @@ -250,5 +250,27 @@ namespace Strawberry return componentTracker[typeof(T)].Count; } + public List All(List into) where T : Component + { + for (let c in componentTracker[typeof(T)]) + into.Add(c as T); + return into; + } + + public List All(Point point, List into) where T : Component + { + for (let c in componentTracker[typeof(T)]) + if (c.Entity.Check(point)) + into.Add(c as T); + return into; + } + + public List All(Rect rect, List into) where T : Component + { + for (let c in componentTracker[typeof(T)]) + if (c.Entity.Check(rect)) + into.Add(c as T); + return into; + } } } diff --git a/src/Physics/Grid.bf b/src/Physics/Grid.bf index b13941e..a179d26 100644 --- a/src/Physics/Grid.bf +++ b/src/Physics/Grid.bf @@ -172,6 +172,118 @@ namespace Strawberry return false; } + public bool CheckTop(Rect rect, out int top) + { + Point from = .( + (int)Math.Floor((rect.X - Offset.X) / (float)CellSize.X), + (int)Math.Floor((rect.Y - Offset.Y) / (float)CellSize.Y) + ); + Point to = .( + (int)Math.Ceiling((rect.Right - Offset.X) / (float)CellSize.X), + (int)Math.Ceiling((rect.Bottom - Offset.Y) / (float)CellSize.Y) + ); + + for (int y = from.Y; y < to.Y; y++) + { + for (int x = from.X; x < to.X; x++) + { + let p = Point(x, y); + if (IsInBounds(p) && this[p] != '0') + { + top = Offset.Y + y * CellSize.Y; + return true; + } + } + } + + top = 0; + return false; + } + + public bool CheckBottom(Rect rect, out int bottom) + { + Point from = .( + (int)Math.Floor((rect.X - Offset.X) / (float)CellSize.X), + (int)Math.Floor((rect.Y - Offset.Y) / (float)CellSize.Y) + ); + Point to = .( + (int)Math.Ceiling((rect.Right - Offset.X) / (float)CellSize.X), + (int)Math.Ceiling((rect.Bottom - Offset.Y) / (float)CellSize.Y) + ); + + for (int y = to.Y - 1; y >= from.Y; y--) + { + for (int x = from.X; x < to.X; x++) + { + let p = Point(x, y); + if (IsInBounds(p) && this[p] != '0') + { + bottom = Offset.Y + (y + 1) * CellSize.Y; + return true; + } + } + } + + bottom = 0; + return false; + } + + public bool CheckLeft(Rect rect, out int left) + { + Point from = .( + (int)Math.Floor((rect.X - Offset.X) / (float)CellSize.X), + (int)Math.Floor((rect.Y - Offset.Y) / (float)CellSize.Y) + ); + Point to = .( + (int)Math.Ceiling((rect.Right - Offset.X) / (float)CellSize.X), + (int)Math.Ceiling((rect.Bottom - Offset.Y) / (float)CellSize.Y) + ); + + for (int x = from.X; x < to.X; x++) + { + for (int y = from.Y; y < to.Y; y++) + { + let p = Point(x, y); + if (IsInBounds(p) && this[p] != '0') + { + left = Offset.X + x * CellSize.X; + return true; + } + } + } + + left = 0; + return false; + } + + public bool CheckRight(Rect rect, out int right) + { + Point from = .( + (int)Math.Floor((rect.X - Offset.X) / (float)CellSize.X), + (int)Math.Floor((rect.Y - Offset.Y) / (float)CellSize.Y) + ); + Point to = .( + (int)Math.Ceiling((rect.Right - Offset.X) / (float)CellSize.X), + (int)Math.Ceiling((rect.Bottom - Offset.Y) / (float)CellSize.Y) + ); + + for (int x = to.X - 1; x >= from.X; x--) + { + for (int y = from.Y; y < to.Y; y++) + { + let p = Point(x, y); + if (IsInBounds(p) && this[p] != '0') + { + right = Offset.X + (x + 1) * CellSize.X; + return true; + } + } + } + + right = 0; + return false; + } + public bool Check(Point point) { Point check = (point - Offset) / CellSize; diff --git a/src/Struct/Cardinals.bf b/src/Struct/Cardinals.bf index c16af23..c243752 100644 --- a/src/Struct/Cardinals.bf +++ b/src/Struct/Cardinals.bf @@ -92,6 +92,11 @@ namespace Strawberry } } + static public Cardinals operator -(Cardinals c) + { + return c.Opposite(); + } + static public implicit operator Cardinals(Facings f) { if (f == Facings.Right) diff --git a/src/Struct/Vector.bf b/src/Struct/Vector.bf index 2d7ebd2..14186db 100644 --- a/src/Struct/Vector.bf +++ b/src/Struct/Vector.bf @@ -35,7 +35,10 @@ namespace Strawberry public Vector Normalized() { - return this / Length; + if (X == 0 && Y == 0) + return this; + else + return this / Length; } public float Length => Math.Sqrt(LengthSquared);