From 9374086e97b8edd34772c1ecf8da714c2ae2bcb9 Mon Sep 17 00:00:00 2001 From: Matt Thorson Date: Tue, 5 May 2020 17:43:33 -0700 Subject: [PATCH] Collision struct --- src/Entity.bf | 5 +++ src/Physics/Actor.bf | 40 ++++++++++++++++++------ src/Physics/Collision.bf | 20 ++++++++++++ src/Physics/{Platform.bf => Geometry.bf} | 16 +++++----- src/Physics/Solid.bf | 6 ++-- 5 files changed, 66 insertions(+), 21 deletions(-) create mode 100644 src/Physics/Collision.bf rename src/Physics/{Platform.bf => Geometry.bf} (61%) diff --git a/src/Entity.bf b/src/Entity.bf index 499e74a..df8efef 100644 --- a/src/Entity.bf +++ b/src/Entity.bf @@ -42,6 +42,11 @@ namespace Strawberry } + public void RemoveSelf() + { + Scene?.Remove(this); + } + // ===== Position ===== public Vector Positionf; diff --git a/src/Physics/Actor.bf b/src/Physics/Actor.bf index 7bf51b7..dc724ad 100644 --- a/src/Physics/Actor.bf +++ b/src/Physics/Actor.bf @@ -25,10 +25,10 @@ namespace Strawberry public virtual void Squish() { - + Scene.Remove(this); } - public bool MoveX(float amount, Action onCollide = null) + public bool MoveX(float amount, Action onCollide = null) { remainder.X += amount; let move = (int)Math.Round(remainder.X); @@ -41,7 +41,7 @@ namespace Strawberry return false; } - public bool MoveY(float amount, Action onCollide = null) + public bool MoveY(float amount, Action onCollide = null) { remainder.Y += amount; let move = (int)Math.Round(remainder.Y); @@ -54,16 +54,26 @@ namespace Strawberry return false; } - public bool MoveExactX(int amount, Action onCollide = null) + public bool MoveExactX(int amount, Action onCollide = null, Geometry pusher = null) { int move = amount; int sign = Math.Sign(amount); while (move != 0) { - if (Check(.(sign, 0))) + let hit = First(.(sign, 0)); + if (hit != null) { ZeroRemainderX(); - onCollide?.Invoke(); + + let c = Collision( + Point.Right * sign, + Math.Abs(amount), + Math.Abs(amount - move), + hit, + pusher + ); + + onCollide?.Invoke(c); return true; } @@ -74,16 +84,26 @@ namespace Strawberry return false; } - public bool MoveExactY(int amount, Action onCollide = null) + public bool MoveExactY(int amount, Action onCollide = null, Geometry pusher = null) { int move = amount; int sign = Math.Sign(amount); while (move != 0) { - if (Check(.(0, sign))) + let hit = First(.(0, sign)); + if (hit != null) { ZeroRemainderY(); - onCollide?.Invoke(); + + let c = Collision( + Point.Right * sign, + Math.Abs(amount), + Math.Abs(amount - move), + hit, + pusher + ); + + onCollide?.Invoke(c); return true; } @@ -104,7 +124,7 @@ namespace Strawberry remainder.Y = 0; } - public void ZeroRemainder() + public void ZeroRemainders() { remainder = Vector.Zero; } diff --git a/src/Physics/Collision.bf b/src/Physics/Collision.bf new file mode 100644 index 0000000..2df53dd --- /dev/null +++ b/src/Physics/Collision.bf @@ -0,0 +1,20 @@ +namespace Strawberry +{ + public struct Collision + { + public Point Direction; + public int Magnitude; + public int Completed; + public Geometry Stopper; + public Geometry Pusher; + + public this(Point direction, int magnitude, int completed, Geometry stopper, Geometry pusher) + { + Direction = direction; + Magnitude = magnitude; + Completed = completed; + Stopper = stopper; + Pusher = pusher; + } + } +} diff --git a/src/Physics/Platform.bf b/src/Physics/Geometry.bf similarity index 61% rename from src/Physics/Platform.bf rename to src/Physics/Geometry.bf index facfb68..64bd0a0 100644 --- a/src/Physics/Platform.bf +++ b/src/Physics/Geometry.bf @@ -3,7 +3,7 @@ using System.Collections; namespace Strawberry { - public abstract class Platform : Entity + public abstract class Geometry : Entity { private Vector remainder; @@ -13,30 +13,30 @@ namespace Strawberry } - public void MoveX(float amount, Action onCollide = null) + public void MoveX(float amount) { remainder.X += amount; let move = (int)Math.Round(remainder.X); if (move != 0) { remainder.X -= move; - MoveExactX(move, onCollide); + MoveExactX(move); } } - public void MoveY(float amount, Action onCollide = null) + public void MoveY(float amount) { remainder.Y += amount; let move = (int)Math.Round(remainder.Y); if (move != 0) { remainder.Y -= move; - MoveExactY(move, onCollide); + MoveExactY(move); } } - public abstract void MoveExactX(int amount, Action onCollide = null); - public abstract void MoveExactY(int amount, Action onCollide = null); + public abstract void MoveExactX(int amount); + public abstract void MoveExactY(int amount); public abstract List GetRiders(List into); public void ZeroRemainderX() @@ -49,7 +49,7 @@ namespace Strawberry remainder.Y = 0; } - public void ZeroRemainder() + public void ZeroRemainders() { remainder = Vector.Zero; } diff --git a/src/Physics/Solid.bf b/src/Physics/Solid.bf index f67ceb6..a67c0b3 100644 --- a/src/Physics/Solid.bf +++ b/src/Physics/Solid.bf @@ -2,7 +2,7 @@ using System.Collections; namespace Strawberry { - public class Solid : Platform + public class Solid : Geometry { public this(int x, int y, Rect hitbox) : base(x, y) @@ -23,7 +23,7 @@ namespace Strawberry return into; } - public override void MoveExactX(int amount, System.Action onCollide = null) + public override void MoveExactX(int amount) { if (amount != 0) { @@ -31,7 +31,7 @@ namespace Strawberry } } - public override void MoveExactY(int amount, System.Action onCollide = null) + public override void MoveExactY(int amount) { if (amount != 0) {