feat: added ball trail
This commit is contained in:
55
Shared/Behaviours/BallTrail.cs
Normal file
55
Shared/Behaviours/BallTrail.cs
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
using Engine.Core;
|
||||||
|
using Engine.Physics2D;
|
||||||
|
using Engine.Systems.Graphics;
|
||||||
|
using Engine.Systems.Tween;
|
||||||
|
|
||||||
|
namespace Pong.Behaviours;
|
||||||
|
|
||||||
|
public class BallTrail : Behaviour2D, IPostPhysicsUpdate, IDrawableTriangle
|
||||||
|
{
|
||||||
|
private float width = 10f;
|
||||||
|
private ColorRGBA colorBase = new ColorRGBA(255, 255, 255);
|
||||||
|
private int trailTickPeriod = 20;
|
||||||
|
private IEasing widthCurve = EaseInCirc.Instance;
|
||||||
|
|
||||||
|
private readonly LinkedList<Vector2D> trailPoints = new();
|
||||||
|
|
||||||
|
public void PostPhysicsUpdate(float delta)
|
||||||
|
{
|
||||||
|
trailPoints.AddFirst(Transform.Position);
|
||||||
|
if (trailPoints.Count > trailTickPeriod)
|
||||||
|
trailPoints.RemoveLast();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Draw(ITriangleBatch triangleBatch)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < trailPoints.Count - 2; i++)
|
||||||
|
{
|
||||||
|
Line2D line = new(trailPoints.ElementAt(i), trailPoints.ElementAt(i + 1));
|
||||||
|
Vector2D perpendicularDirection = line.Direction.Perpendicular();
|
||||||
|
|
||||||
|
float t = widthCurve.Evaluate(1f - ((float)i / trailPoints.Count));
|
||||||
|
float tPlus = widthCurve.Evaluate(1f - ((float)(i + 1) / trailPoints.Count));
|
||||||
|
|
||||||
|
float firstWidth = t * width;
|
||||||
|
float endWidth = tPlus * width;
|
||||||
|
|
||||||
|
triangleBatch.Draw(
|
||||||
|
new(line.From + perpendicularDirection * firstWidth,
|
||||||
|
line.From - perpendicularDirection * firstWidth,
|
||||||
|
line.To + perpendicularDirection * endWidth),
|
||||||
|
colorBase
|
||||||
|
);
|
||||||
|
|
||||||
|
triangleBatch.Draw(
|
||||||
|
new(line.From - perpendicularDirection * firstWidth,
|
||||||
|
line.To - perpendicularDirection * endWidth,
|
||||||
|
line.To + perpendicularDirection * endWidth),
|
||||||
|
colorBase
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -81,6 +81,7 @@ public static class PongUniverse
|
|||||||
.BehaviourController.AddBehaviour<Transform2D>().SetTransform(position: new Vector2D(0, 0f), scale: new Vector2D(10f, 10f))
|
.BehaviourController.AddBehaviour<Transform2D>().SetTransform(position: new Vector2D(0, 0f), scale: new Vector2D(10f, 10f))
|
||||||
.BehaviourController.AddBehaviour<DrawableColliderCircle>(new Circle(Vector2D.Zero, 1f))
|
.BehaviourController.AddBehaviour<DrawableColliderCircle>(new Circle(Vector2D.Zero, 1f))
|
||||||
.BehaviourController.AddBehaviour<Ball>()
|
.BehaviourController.AddBehaviour<Ball>()
|
||||||
|
.BehaviourController.AddBehaviour<BallTrail>()
|
||||||
.BehaviourController.AddBehaviour<RigidBody2D>();
|
.BehaviourController.AddBehaviour<RigidBody2D>();
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|||||||
Reference in New Issue
Block a user