mirror of
https://github.com/MaddyThorson/StrawberryBF.git
synced 2025-02-18 11:38:28 +08:00
Collision struct
This commit is contained in:
parent
654398a137
commit
9374086e97
@ -42,6 +42,11 @@ namespace Strawberry
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void RemoveSelf()
|
||||||
|
{
|
||||||
|
Scene?.Remove(this);
|
||||||
|
}
|
||||||
|
|
||||||
// ===== Position =====
|
// ===== Position =====
|
||||||
|
|
||||||
public Vector Positionf;
|
public Vector Positionf;
|
||||||
|
@ -25,10 +25,10 @@ namespace Strawberry
|
|||||||
|
|
||||||
public virtual void Squish()
|
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;
|
remainder.X += amount;
|
||||||
let move = (int)Math.Round(remainder.X);
|
let move = (int)Math.Round(remainder.X);
|
||||||
@ -41,7 +41,7 @@ namespace Strawberry
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool MoveY(float amount, Action onCollide = null)
|
public bool MoveY(float amount, Action<Collision> onCollide = null)
|
||||||
{
|
{
|
||||||
remainder.Y += amount;
|
remainder.Y += amount;
|
||||||
let move = (int)Math.Round(remainder.Y);
|
let move = (int)Math.Round(remainder.Y);
|
||||||
@ -54,16 +54,26 @@ namespace Strawberry
|
|||||||
return false;
|
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 move = amount;
|
||||||
int sign = Math.Sign(amount);
|
int sign = Math.Sign(amount);
|
||||||
while (move != 0)
|
while (move != 0)
|
||||||
{
|
{
|
||||||
if (Check<Solid>(.(sign, 0)))
|
let hit = First<Solid>(.(sign, 0));
|
||||||
|
if (hit != null)
|
||||||
{
|
{
|
||||||
ZeroRemainderX();
|
ZeroRemainderX();
|
||||||
onCollide?.Invoke();
|
|
||||||
|
let c = Collision(
|
||||||
|
Point.Right * sign,
|
||||||
|
Math.Abs(amount),
|
||||||
|
Math.Abs(amount - move),
|
||||||
|
hit,
|
||||||
|
pusher
|
||||||
|
);
|
||||||
|
|
||||||
|
onCollide?.Invoke(c);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,16 +84,26 @@ namespace Strawberry
|
|||||||
return false;
|
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 move = amount;
|
||||||
int sign = Math.Sign(amount);
|
int sign = Math.Sign(amount);
|
||||||
while (move != 0)
|
while (move != 0)
|
||||||
{
|
{
|
||||||
if (Check<Solid>(.(0, sign)))
|
let hit = First<Solid>(.(0, sign));
|
||||||
|
if (hit != null)
|
||||||
{
|
{
|
||||||
ZeroRemainderY();
|
ZeroRemainderY();
|
||||||
onCollide?.Invoke();
|
|
||||||
|
let c = Collision(
|
||||||
|
Point.Right * sign,
|
||||||
|
Math.Abs(amount),
|
||||||
|
Math.Abs(amount - move),
|
||||||
|
hit,
|
||||||
|
pusher
|
||||||
|
);
|
||||||
|
|
||||||
|
onCollide?.Invoke(c);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,7 +124,7 @@ namespace Strawberry
|
|||||||
remainder.Y = 0;
|
remainder.Y = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ZeroRemainder()
|
public void ZeroRemainders()
|
||||||
{
|
{
|
||||||
remainder = Vector.Zero;
|
remainder = Vector.Zero;
|
||||||
}
|
}
|
||||||
|
20
src/Physics/Collision.bf
Normal file
20
src/Physics/Collision.bf
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -3,7 +3,7 @@ using System.Collections;
|
|||||||
|
|
||||||
namespace Strawberry
|
namespace Strawberry
|
||||||
{
|
{
|
||||||
public abstract class Platform : Entity
|
public abstract class Geometry : Entity
|
||||||
{
|
{
|
||||||
private Vector remainder;
|
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;
|
remainder.X += amount;
|
||||||
let move = (int)Math.Round(remainder.X);
|
let move = (int)Math.Round(remainder.X);
|
||||||
if (move != 0)
|
if (move != 0)
|
||||||
{
|
{
|
||||||
remainder.X -= move;
|
remainder.X -= move;
|
||||||
MoveExactX(move, onCollide);
|
MoveExactX(move);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void MoveY(float amount, Action onCollide = null)
|
public void MoveY(float amount)
|
||||||
{
|
{
|
||||||
remainder.Y += amount;
|
remainder.Y += amount;
|
||||||
let move = (int)Math.Round(remainder.Y);
|
let move = (int)Math.Round(remainder.Y);
|
||||||
if (move != 0)
|
if (move != 0)
|
||||||
{
|
{
|
||||||
remainder.Y -= move;
|
remainder.Y -= move;
|
||||||
MoveExactY(move, onCollide);
|
MoveExactY(move);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void MoveExactX(int amount, Action onCollide = null);
|
public abstract void MoveExactX(int amount);
|
||||||
public abstract void MoveExactY(int amount, Action onCollide = null);
|
public abstract void MoveExactY(int amount);
|
||||||
public abstract List<Actor> GetRiders(List<Actor> into);
|
public abstract List<Actor> GetRiders(List<Actor> into);
|
||||||
|
|
||||||
public void ZeroRemainderX()
|
public void ZeroRemainderX()
|
||||||
@ -49,7 +49,7 @@ namespace Strawberry
|
|||||||
remainder.Y = 0;
|
remainder.Y = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ZeroRemainder()
|
public void ZeroRemainders()
|
||||||
{
|
{
|
||||||
remainder = Vector.Zero;
|
remainder = Vector.Zero;
|
||||||
}
|
}
|
@ -2,7 +2,7 @@ using System.Collections;
|
|||||||
|
|
||||||
namespace Strawberry
|
namespace Strawberry
|
||||||
{
|
{
|
||||||
public class Solid : Platform
|
public class Solid : Geometry
|
||||||
{
|
{
|
||||||
public this(int x, int y, Rect hitbox)
|
public this(int x, int y, Rect hitbox)
|
||||||
: base(x, y)
|
: base(x, y)
|
||||||
@ -23,7 +23,7 @@ namespace Strawberry
|
|||||||
return into;
|
return into;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void MoveExactX(int amount, System.Action onCollide = null)
|
public override void MoveExactX(int amount)
|
||||||
{
|
{
|
||||||
if (amount != 0)
|
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)
|
if (amount != 0)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user