The sample game works again!

This commit is contained in:
Maddy Thorson
2021-12-23 17:09:38 -08:00
parent d3bf09f173
commit bae5798f07
17 changed files with 264 additions and 115 deletions

View File

@ -15,4 +15,3 @@ ReflectAlwaysInclude = "IncludeAll"
Type = "Folder"
Name = "Physics"
AutoInclude = true
Source = ["Physics.bf", "Solid.bf", "JumpThru.bf"]

View File

@ -0,0 +1,61 @@
namespace Strawberry.Sample
{
public class MovingJumpThru : Component, IUpdate
{
static public Entity Create(Point pos, int width, Point moveTo, float moveTime)
{
let e = new Entity(pos);
let hitbox = e.Add(new Hitbox(0, 0, width, 4));
let jumpThru = e.Add(new JumpThru(hitbox));
e.Add(new MovingJumpThru(jumpThru, moveTo, moveTime));
e.Add(new DrawHitbox(hitbox, .LightGray));
return e;
}
private JumpThru jumpThru;
private Point moveFrom;
private Point moveTo;
private float moveTime;
private float movingLerp = 0;
private bool movingPositive = true;
public this(JumpThru jumpThru, Point moveTo, float moveTime)
{
this.jumpThru = jumpThru;
this.moveTo = moveTo;
this.moveTime = moveTime;
}
protected override void Awake()
{
moveFrom = Entity.Position;
}
public void Update()
{
if (movingPositive)
{
movingLerp += Time.Delta / moveTime;
if (movingLerp >= 1)
{
movingLerp = 1;
movingPositive = false;
}
}
else
{
movingLerp -= Time.Delta / moveTime;
if (movingLerp <= 0)
{
movingLerp = 0;
movingPositive = true;
}
}
let target = Vector.Lerp(moveFrom, moveTo, Ease.CubeInOut(movingLerp));
jumpThru.MoveTo(target);
}
}
}

View File

@ -2,7 +2,7 @@ using System;
namespace Strawberry.Sample
{
public class Player : Component, IUpdate, IDraw
public class Player : Component, IUpdate
{
static public Entity Create(Point pos)
{
@ -10,18 +10,19 @@ namespace Strawberry.Sample
e.Add(new Player());
let hitbox = e.Add(new Hitbox(-4, -8, 16, 16));
e.Add(new Physics(hitbox));
e.Add(new Actor(hitbox));
e.Add(new DrawHitbox(hitbox, .Red));
return e;
}
public Vector Speed;
private Physics physics;
private Actor physics;
private Timer tJumpGrace;
private Timer tVarJump;
public override void Added()
protected override void Added()
{
base.Added();
@ -29,9 +30,9 @@ namespace Strawberry.Sample
tVarJump = Entity.Add(new Timer());
}
public override void Awake()
protected override void Awake()
{
physics = Entity.First<Physics>();
physics = Entity.First<Actor>();
}
public void Update()
@ -111,10 +112,5 @@ namespace Strawberry.Sample
Speed.Y = 0;
physics.ZeroRemainderY();
}
public void Draw()
{
physics.Hitbox.DebugDraw();
}
}
}

View File

@ -0,0 +1,29 @@
using System;
namespace Strawberry.Sample
{
static public class StaticGeometry
{
static public Entity CreateSolid(Point pos, Rect bounds)
{
let e = new Entity(pos);
let hitbox = e.Add(new Hitbox(bounds));
e.Add(new Solid(hitbox));
e.Add(new DrawHitbox(hitbox, .White));
return e;
}
static public Entity CreateJumpThru(Point pos, int width)
{
let e = new Entity(pos);
let hitbox = e.Add(new Hitbox(0, 0, width, 4));
e.Add(new JumpThru(hitbox));
e.Add(new DrawHitbox(hitbox, .LightGray));
return e;
}
}
}

View File

@ -7,10 +7,9 @@ namespace Strawberry.Sample
public this()
{
Add(Player.Create(.(50, 50)));
Add(new OldSolid(.(0, 168), .(0, 0, 320, 12)));
Add(new OldJumpThru(.(200, 132), 48));
Add(new MovingJumpThru(.(136, 100), 32, .(124, 140), 2f));
Add(MovingJumpThru.Create(.(136, 100), 32, .(124, 140), 2f));
Add(StaticGeometry.CreateSolid(.(0, 168), .(0, 0, 320, 12)));
Add(StaticGeometry.CreateJumpThru(.(200, 132), 48));
}
}
}

View File

@ -1,50 +0,0 @@
namespace Strawberry.Sample
{
public class MovingJumpThru : JumpThru
{
private Point moveFrom;
private Point moveTo;
private float moveTime;
private float movingLerp;
private bool movingPositive;
public this(Point pos, int width, Point moveTo, float moveTime)
: base(pos, width)
{
moveFrom = Position;
this.moveTo = moveTo;
this.moveTime = moveTime;
movingLerp = 0;
movingPositive = true;
}
public override void Update()
{
base.Update();
if (movingPositive)
{
movingLerp += Time.Delta / moveTime;
if (movingLerp >= 1)
{
movingLerp = 1;
movingPositive = false;
}
}
else
{
movingLerp -= Time.Delta / moveTime;
if (movingLerp <= 0)
{
movingLerp = 0;
movingPositive = true;
}
}
let target = Vector.Lerp(moveFrom, moveTo, Ease.CubeInOut(movingLerp));
MoveTo(target);
}
}
}

View File

@ -1,20 +1,32 @@
using System;
using System.Collections;
namespace Strawberry.Sample
{
public class Physics : Component, IHasHitbox, IUpdate
public class Actor : Component, IHasHitbox, IUpdate
{
public Hitbox Hitbox { get; private set; }
public Vector Speed;
private Vector remainder;
public Level Level => Entity.SceneAs<Level>();
public Vector ExactPosition => Entity.Position + remainder;
public this(Hitbox hitbox)
{
Hitbox = hitbox;
}
public Level Level => Entity.SceneAs<Level>();
public void Update()
{
MoveX(Speed.X * Time.Delta);
MoveY(Speed.Y * Time.Delta);
}
/*
Collision Helpers
*/
public bool Check(Level level)
{
@ -31,21 +43,19 @@ namespace Strawberry.Sample
return Hitbox.Check<Solid>(.(0, distance)) || Check(Level, .(0, distance)) || Hitbox.CheckOutside<JumpThru>(.(0, distance));
}
public virtual bool IsRiding(Solid solid)
public bool IsRiding(Solid solid)
{
return Hitbox.Check(solid, .(0, 1));
}
public virtual bool IsRiding(JumpThru jumpThru)
public bool IsRiding(JumpThru jumpThru)
{
return Hitbox.CheckOutside(jumpThru, .(0, 1));
}
public void Update()
{
MoveX(Speed.X * Time.Delta);
MoveY(Speed.Y * Time.Delta);
}
/*
Movement
*/
public bool MoveX(float amount, delegate void(Collision) onCollide = null)
{
@ -74,15 +84,15 @@ namespace Strawberry.Sample
}
[Inline]
public void MoveToX(float x)
public void MoveToX(float x, delegate void(Collision) onCollide = null)
{
MoveX(x - (Entity.X + remainder.X), null);
MoveX(x - (Entity.X + remainder.X), onCollide);
}
[Inline]
public void MoveToY(float y)
public void MoveToY(float y, delegate void(Collision) onCollide = null)
{
MoveY(y - (Entity.Y + remainder.Y), null);
MoveY(y - (Entity.Y + remainder.Y), onCollide);
}
public bool MoveExactX(int amount, delegate void(Collision) onCollide = null)
@ -116,22 +126,45 @@ namespace Strawberry.Sample
int move = amount;
int sign = Math.Sign(amount);
while (move != 0)
if (move > 0)
{
if (Check(Level, .(0, sign)) || Hitbox.Check<Solid>(.(0, sign)) || Hitbox.CheckOutside<JumpThru>(.(0, sign)))
while (move != 0)
{
let c = Collision(
Cardinals.FromPoint(Point.Down * sign),
Math.Abs(amount),
Math.Abs(amount - move)
);
if (Check(Level, .(0, sign)) || Hitbox.Check<Solid>(.(0, sign)) || Hitbox.CheckOutside<JumpThru>(.(0, sign)))
{
let c = Collision(
Cardinals.FromPoint(Point.Down * sign),
Math.Abs(amount),
Math.Abs(amount - move)
);
onCollide?.Invoke(c);
return true;
onCollide?.Invoke(c);
return true;
}
Entity.Y += sign;
move -= sign;
}
}
else
{
while (move != 0)
{
if (Check(Level, .(0, sign)) || Hitbox.Check<Solid>(.(0, sign)))
{
let c = Collision(
Cardinals.FromPoint(Point.Down * sign),
Math.Abs(amount),
Math.Abs(amount - move)
);
Entity.Y += sign;
move -= sign;
onCollide?.Invoke(c);
return true;
}
Entity.Y += sign;
move -= sign;
}
}
return false;

View File

@ -1,4 +1,5 @@
using System.Collections;
using System.Collections;
namespace Strawberry.Sample
{
@ -6,9 +7,54 @@ namespace Strawberry.Sample
{
public Hitbox Hitbox { get; private set; }
private Vector remainder;
public Vector ExactPosition => Entity.Position + remainder;
public this(Hitbox hitbox)
{
Hitbox = hitbox;
}
public void FindRiders(List<Actor> into)
{
for (let a in Scene.All<Actor>(scope .()))
if (a.IsRiding(this))
into.Add(a);
}
public void Move(Vector amount)
{
remainder += amount;
Point move = remainder.Round();
MoveExact(move);
}
public void MoveTo(Vector pos)
{
Move(pos - ExactPosition);
}
public void MoveExact(Point amount)
{
if (amount != .Zero)
{
if (Hitbox.Collidable)
{
List<Actor> riders = FindRiders(..scope .());
Hitbox.Collidable = false;
for (let r in riders)
{
r.MoveExactX(amount.X);
r.MoveExactY(amount.Y);
}
Hitbox.Collidable = true;
}
Entity.Position += amount;
}
}
}
}

View File

@ -6,9 +6,15 @@ namespace Strawberry.Sample
{
public Hitbox Hitbox { get; private set; }
private Vector remainder;
public Vector ExactPosition => Entity.Position + remainder;
public this(Hitbox hitbox)
{
Hitbox = hitbox;
}
}
}