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;
|
2023-07-19 04:38:04 +00:00
|
|
|
|
using System.Diagnostics;
|
2021-09-04 18:50:30 +00:00
|
|
|
|
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
|
|
|
|
{
|
2021-09-19 12:00:56 +00:00
|
|
|
|
public abstract partial class ParticleSpewer : Sprite
|
2021-08-23 22:15:16 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly FallingParticle[] particles;
|
|
|
|
|
private int currentIndex;
|
2023-07-18 10:52:45 +00:00
|
|
|
|
private double? lastParticleAdded;
|
2021-08-23 22:15:16 +00:00
|
|
|
|
|
2023-07-18 10:52:45 +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>
|
2021-09-04 18:50:30 +00:00
|
|
|
|
public readonly BindableBool Active = new BindableBool();
|
2021-08-23 22:15:16 +00:00
|
|
|
|
|
2021-09-16 07:54:22 +00:00
|
|
|
|
public override bool IsPresent => base.IsPresent && hasActiveParticles;
|
2021-08-23 22:15:16 +00:00
|
|
|
|
|
2021-09-19 12:00:56 +00:00
|
|
|
|
protected virtual bool CanSpawnParticles => true;
|
|
|
|
|
protected virtual float ParticleGravity => 0;
|
2021-09-16 07:54:22 +00:00
|
|
|
|
|
2021-09-19 01:19:16 +00:00
|
|
|
|
private bool hasActiveParticles => Active.Value || (lastParticleAdded + maxDuration) > Time.Current;
|
|
|
|
|
|
2021-09-19 12:00:56 +00:00
|
|
|
|
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
|
|
|
|
|
2023-07-18 10:52:45 +00:00
|
|
|
|
timeBetweenSpawns = 1000f / perSecond;
|
2021-09-19 01:19:16 +00:00
|
|
|
|
this.maxDuration = maxDuration;
|
2021-08-23 22:15:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 05:44:05 +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();
|
|
|
|
|
|
2023-07-18 10:52:45 +00:00
|
|
|
|
Invalidate(Invalidation.DrawNode);
|
|
|
|
|
|
|
|
|
|
if (!Active.Value || !CanSpawnParticles)
|
|
|
|
|
return;
|
|
|
|
|
|
2023-07-19 04:38:04 +00:00
|
|
|
|
if (lastParticleAdded == null)
|
2021-08-23 22:15:16 +00:00
|
|
|
|
{
|
2023-07-19 04:38:04 +00:00
|
|
|
|
lastParticleAdded = Time.Current;
|
|
|
|
|
spawnParticle();
|
2023-07-19 05:10:27 +00:00
|
|
|
|
return;
|
2023-07-19 04:38:04 +00:00
|
|
|
|
}
|
2023-07-18 10:52:45 +00:00
|
|
|
|
|
2023-07-19 05:10:27 +00:00
|
|
|
|
double timeElapsed = Time.Current - lastParticleAdded.Value;
|
2021-09-16 07:50:03 +00:00
|
|
|
|
|
2023-07-19 04:38:04 +00:00
|
|
|
|
// Avoid spawning too many particles if a long amount of time has passed.
|
2023-07-19 05:10:27 +00:00
|
|
|
|
if (Math.Abs(timeElapsed) > maxDuration)
|
2023-07-19 04:38:04 +00:00
|
|
|
|
{
|
|
|
|
|
lastParticleAdded = Time.Current;
|
|
|
|
|
spawnParticle();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-09-09 21:18:19 +00:00
|
|
|
|
|
2023-07-19 04:38:04 +00:00
|
|
|
|
Debug.Assert(lastParticleAdded != null);
|
2021-08-23 22:15:16 +00:00
|
|
|
|
|
2023-07-19 04:38:04 +00:00
|
|
|
|
for (int i = 0; i < timeElapsed / timeBetweenSpawns; i++)
|
|
|
|
|
{
|
2023-07-18 10:52:45 +00:00
|
|
|
|
lastParticleAdded += timeBetweenSpawns;
|
2023-07-19 04:38:04 +00:00
|
|
|
|
spawnParticle();
|
2023-07-18 10:52:45 +00:00
|
|
|
|
}
|
2021-08-23 22:15:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-19 04:38:04 +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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-19 12:00:56 +00:00
|
|
|
|
/// <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-29 19:41:47 +00:00
|
|
|
|
|
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-09-18 21:45:58 +00:00
|
|
|
|
|
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
|
|
|
|
|
2021-09-19 12:49:09 +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)
|
|
|
|
|
{
|
2022-04-09 13:29:55 +00:00
|
|
|
|
if (p.Duration == 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
2021-10-27 04:04:41 +00:00
|
|
|
|
float timeSinceStart = currentTime - p.StartTime;
|
2021-08-23 22:15:16 +00:00
|
|
|
|
|
2021-08-29 19:41:47 +00:00
|
|
|
|
// ignore particles from the future.
|
|
|
|
|
// these can appear when seeking in replays.
|
|
|
|
|
if (timeSinceStart < 0) continue;
|
|
|
|
|
|
2021-10-27 04:04:41 +00:00
|
|
|
|
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);
|
2021-10-27 04:04:41 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
2021-10-27 04:04:41 +00:00
|
|
|
|
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;
|
|
|
|
|
|
2021-09-09 21:18:19 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2021-08-29 19:41:47 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2021-09-19 12:00:56 +00:00
|
|
|
|
protected struct FallingParticle
|
2021-08-23 22:15:16 +00:00
|
|
|
|
{
|
|
|
|
|
public float StartTime;
|
2021-08-29 19:41:47 +00:00
|
|
|
|
public Vector2 StartPosition;
|
2021-08-23 22:15:16 +00:00
|
|
|
|
public Vector2 Velocity;
|
|
|
|
|
public float Duration;
|
2021-09-05 15:08:00 +00:00
|
|
|
|
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
|
|
|
|
|
2021-09-18 21:45:58 +00:00
|
|
|
|
public Vector2 PositionAtTime(float timeSinceStart, float gravity, float maxDuration)
|
2021-08-23 22:15:16 +00:00
|
|
|
|
{
|
2021-10-27 04:04:41 +00:00
|
|
|
|
float progress = progressAtTime(timeSinceStart);
|
2021-09-18 21:45:58 +00:00
|
|
|
|
var currentGravity = new Vector2(0, gravity * Duration / maxDuration * progress);
|
2021-08-23 22:15:16 +00:00
|
|
|
|
|
2021-09-18 21:45:58 +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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|