osu/osu.Game/Graphics/ParticleSpewer.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

248 lines
8.5 KiB
C#
Raw Normal View History

2021-08-23 22:15:16 +00:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2022-06-17 07:37:17 +00:00
#nullable disable
2021-08-23 22:15:16 +00:00
using System;
using System.Diagnostics;
using osu.Framework.Bindables;
2021-08-23 22:15:16 +00:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Primitives;
2022-07-29 13:33:34 +00:00
using osu.Framework.Graphics.Rendering;
2021-08-23 22:15:16 +00:00
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Textures;
2021-09-19 13:06:15 +00:00
using osu.Framework.Utils;
2021-08-23 22:15:16 +00:00
using osuTK;
2021-09-12 22:35:13 +00:00
namespace osu.Game.Graphics
2021-08-23 22:15:16 +00:00
{
public abstract partial class ParticleSpewer : Sprite
2021-08-23 22:15:16 +00:00
{
private readonly FallingParticle[] particles;
private int currentIndex;
private double? lastParticleAdded;
2021-08-23 22:15:16 +00:00
private readonly double timeBetweenSpawns;
2021-09-19 01:19:16 +00:00
private readonly double maxDuration;
2021-08-23 22:15:16 +00:00
/// <summary>
/// Determines whether particles are being spawned.
/// </summary>
public readonly BindableBool Active = new BindableBool();
2021-08-23 22:15:16 +00:00
public override bool IsPresent => base.IsPresent && hasActiveParticles;
2021-08-23 22:15:16 +00:00
protected virtual bool CanSpawnParticles => true;
protected virtual float ParticleGravity => 0;
2021-09-19 01:19:16 +00:00
private bool hasActiveParticles => Active.Value || (lastParticleAdded + maxDuration) > Time.Current;
protected ParticleSpewer(Texture texture, int perSecond, double maxDuration)
2021-08-23 22:15:16 +00:00
{
Texture = texture;
Blending = BlendingParameters.Additive;
2021-09-19 01:19:16 +00:00
particles = new FallingParticle[perSecond * (int)Math.Ceiling(maxDuration / 1000)];
2021-08-23 22:15:16 +00:00
timeBetweenSpawns = 1000f / perSecond;
2021-09-19 01:19:16 +00:00
this.maxDuration = maxDuration;
2021-08-23 22:15:16 +00:00
}
protected override void LoadComplete()
{
base.LoadComplete();
Active.BindValueChanged(active =>
{
// ensure that particles can be spawned immediately after the spewer becomes active.
if (active.NewValue)
lastParticleAdded = null;
});
}
2021-08-23 22:15:16 +00:00
protected override void Update()
{
base.Update();
Invalidate(Invalidation.DrawNode);
if (!Active.Value || !CanSpawnParticles)
return;
if (lastParticleAdded == null)
2021-08-23 22:15:16 +00:00
{
lastParticleAdded = Time.Current;
spawnParticle();
return;
}
double timeElapsed = Time.Current - lastParticleAdded.Value;
// Avoid spawning too many particles if a long amount of time has passed.
if (Math.Abs(timeElapsed) > maxDuration)
{
lastParticleAdded = Time.Current;
spawnParticle();
return;
}
Debug.Assert(lastParticleAdded != null);
2021-08-23 22:15:16 +00:00
for (int i = 0; i < timeElapsed / timeBetweenSpawns; i++)
{
lastParticleAdded += timeBetweenSpawns;
spawnParticle();
}
2021-08-23 22:15:16 +00:00
}
private void spawnParticle()
{
Debug.Assert(lastParticleAdded != null);
var newParticle = CreateParticle();
newParticle.StartTime = (float)lastParticleAdded.Value;
particles[currentIndex] = newParticle;
currentIndex = (currentIndex + 1) % particles.Length;
}
/// <summary>
/// Called each time a new particle should be spawned.
/// </summary>
protected virtual FallingParticle CreateParticle() => new FallingParticle();
2021-08-23 22:15:16 +00:00
protected override DrawNode CreateDrawNode() => new ParticleSpewerDrawNode(this);
2023-07-19 04:52:43 +00:00
#region DrawNode
2021-08-23 22:15:16 +00:00
private class ParticleSpewerDrawNode : SpriteDrawNode
{
private readonly FallingParticle[] particles;
protected new ParticleSpewer Source => (ParticleSpewer)base.Source;
2021-09-19 01:19:16 +00:00
private readonly float maxDuration;
2021-08-23 22:15:16 +00:00
private float currentTime;
private float gravity;
2021-09-18 20:54:12 +00:00
private Axes relativePositionAxes;
private Vector2 sourceSize;
2021-08-23 22:15:16 +00:00
public ParticleSpewerDrawNode(ParticleSpewer source)
2021-08-23 22:15:16 +00:00
: base(source)
{
particles = new FallingParticle[Source.particles.Length];
2021-09-19 01:19:16 +00:00
maxDuration = (float)Source.maxDuration;
2021-08-23 22:15:16 +00:00
}
public override void ApplyState()
{
base.ApplyState();
Source.particles.CopyTo(particles, 0);
currentTime = (float)Source.Time.Current;
gravity = Source.ParticleGravity;
2021-09-18 20:54:12 +00:00
relativePositionAxes = Source.RelativePositionAxes;
sourceSize = Source.DrawSize;
2021-08-23 22:15:16 +00:00
}
2022-07-29 13:33:34 +00:00
protected override void Blit(IRenderer renderer)
2021-08-23 22:15:16 +00:00
{
foreach (var p in particles)
{
if (p.Duration == 0)
continue;
float timeSinceStart = currentTime - p.StartTime;
2021-08-23 22:15:16 +00:00
// ignore particles from the future.
// these can appear when seeking in replays.
if (timeSinceStart < 0) continue;
float alpha = p.AlphaAtTime(timeSinceStart);
2021-08-23 22:15:16 +00:00
if (alpha <= 0) continue;
2021-09-19 01:19:16 +00:00
var pos = p.PositionAtTime(timeSinceStart, gravity, maxDuration);
float scale = p.ScaleAtTime(timeSinceStart);
float angle = p.AngleAtTime(timeSinceStart);
2021-08-23 22:15:16 +00:00
2021-09-18 20:54:12 +00:00
var rect = createDrawRect(pos, scale);
2021-08-23 22:15:16 +00:00
var quad = new Quad(
2021-09-13 22:16:42 +00:00
transformPosition(rect.TopLeft, rect.Centre, angle),
transformPosition(rect.TopRight, rect.Centre, angle),
transformPosition(rect.BottomLeft, rect.Centre, angle),
transformPosition(rect.BottomRight, rect.Centre, angle)
2021-08-23 22:15:16 +00:00
);
2022-08-02 10:50:57 +00:00
renderer.DrawQuad(Texture, quad, DrawColourInfo.Colour.MultiplyAlpha(alpha),
2022-07-29 13:33:34 +00:00
inflationPercentage: new Vector2(InflationAmount.X / DrawRectangle.Width, InflationAmount.Y / DrawRectangle.Height),
textureCoords: TextureCoords);
2021-08-23 22:15:16 +00:00
}
}
2021-09-18 20:54:12 +00:00
private RectangleF createDrawRect(Vector2 position, float scale)
{
float width = Texture.DisplayWidth * scale;
float height = Texture.DisplayHeight * scale;
2021-09-18 20:54:12 +00:00
2024-07-02 15:19:04 +00:00
if (relativePositionAxes.HasFlag(Axes.X))
2021-09-18 20:54:12 +00:00
position.X *= sourceSize.X;
2024-07-02 15:19:04 +00:00
if (relativePositionAxes.HasFlag(Axes.Y))
2021-09-18 20:54:12 +00:00
position.Y *= sourceSize.Y;
return new RectangleF(
position.X - width / 2,
position.Y - height / 2,
width,
height);
}
2021-09-13 22:16:42 +00:00
private Vector2 transformPosition(Vector2 pos, Vector2 centre, float angle)
2021-08-23 22:15:16 +00:00
{
float cos = MathF.Cos(angle);
float sin = MathF.Sin(angle);
float x = centre.X + (pos.X - centre.X) * cos + (pos.Y - centre.Y) * sin;
float y = centre.Y + (pos.Y - centre.Y) * cos - (pos.X - centre.X) * sin;
return Vector2Extensions.Transform(new Vector2(x, y), DrawInfo.Matrix);
2021-08-23 22:15:16 +00:00
}
2021-09-21 05:25:44 +00:00
protected override bool CanDrawOpaqueInterior => false;
2021-08-23 22:15:16 +00:00
}
#endregion
protected struct FallingParticle
2021-08-23 22:15:16 +00:00
{
public float StartTime;
public Vector2 StartPosition;
2021-08-23 22:15:16 +00:00
public Vector2 Velocity;
public float Duration;
public float StartAngle;
public float EndAngle;
2021-08-23 22:15:16 +00:00
public float EndScale;
public float AlphaAtTime(float timeSinceStart) => 1 - progressAtTime(timeSinceStart);
2021-09-19 13:06:15 +00:00
public float ScaleAtTime(float timeSinceStart) => (float)Interpolation.Lerp(1, EndScale, progressAtTime(timeSinceStart));
2021-08-23 22:15:16 +00:00
2021-09-19 13:06:15 +00:00
public float AngleAtTime(float timeSinceStart) => (float)Interpolation.Lerp(StartAngle, EndAngle, progressAtTime(timeSinceStart));
2021-08-23 22:15:16 +00:00
public Vector2 PositionAtTime(float timeSinceStart, float gravity, float maxDuration)
2021-08-23 22:15:16 +00:00
{
float progress = progressAtTime(timeSinceStart);
var currentGravity = new Vector2(0, gravity * Duration / maxDuration * progress);
2021-08-23 22:15:16 +00:00
return StartPosition + (Velocity + currentGravity) * timeSinceStart / maxDuration;
2021-08-23 22:15:16 +00:00
}
private float progressAtTime(float timeSinceStart) => Math.Clamp(timeSinceStart / Duration, 0, 1);
}
}
}