Collision struct

This commit is contained in:
Matt Thorson 2020-05-05 17:43:33 -07:00
parent 654398a137
commit 9374086e97
5 changed files with 66 additions and 21 deletions

View File

@ -42,6 +42,11 @@ namespace Strawberry
}
public void RemoveSelf()
{
Scene?.Remove(this);
}
// ===== Position =====
public Vector Positionf;

View File

@ -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<Collision> 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<Collision> 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<Collision> onCollide = null, Geometry pusher = null)
{
int move = amount;
int sign = Math.Sign(amount);
while (move != 0)
{
if (Check<Solid>(.(sign, 0)))
let hit = First<Solid>(.(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<Collision> onCollide = null, Geometry pusher = null)
{
int move = amount;
int sign = Math.Sign(amount);
while (move != 0)
{
if (Check<Solid>(.(0, sign)))
let hit = First<Solid>(.(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;
}

20
src/Physics/Collision.bf Normal file
View File

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

View File

@ -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<Actor> GetRiders(List<Actor> into);
public void ZeroRemainderX()
@ -49,7 +49,7 @@ namespace Strawberry
remainder.Y = 0;
}
public void ZeroRemainder()
public void ZeroRemainders()
{
remainder = Vector.Zero;
}

View File

@ -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)
{