Add cursor velocity to star particles

This commit is contained in:
Opelkuh 2021-09-05 01:01:49 +02:00
parent ee4006f3d7
commit 5b1b36436f
1 changed files with 40 additions and 0 deletions

View File

@ -131,6 +131,44 @@ private class StarParticleSpewer : ParticleSpewer
public StarParticleSpewer(Texture texture, int perSecond)
: base(texture, perSecond, particle_lifetime_max)
{
Active.BindValueChanged(_ => resetVelocityCalculation());
}
private Vector2 screenPosition => ToScreenSpace(OriginPosition);
private Vector2 screenVelocity;
private const double velocity_calculation_delay = 15;
private double lastVelocityCalculation;
private Vector2 positionDifference;
private Vector2? lastPosition;
protected override void Update()
{
base.Update();
if (lastPosition != null)
{
positionDifference += (screenPosition - lastPosition.Value);
lastVelocityCalculation += Clock.ElapsedFrameTime;
}
lastPosition = screenPosition;
if (lastVelocityCalculation > velocity_calculation_delay)
{
screenVelocity = positionDifference / (float)lastVelocityCalculation;
positionDifference = Vector2.Zero;
lastVelocityCalculation = 0;
}
}
private void resetVelocityCalculation()
{
positionDifference = Vector2.Zero;
lastVelocityCalculation = 0;
lastPosition = null;
}
protected override FallingParticle SpawnParticle()
@ -170,6 +208,8 @@ protected override FallingParticle SpawnParticle()
break;
}
p.Velocity += screenVelocity * 50;
return p;
}
}