2021-08-24 06:15:16 +08: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 15:37:17 +08:00
|
|
|
|
#nullable disable
|
|
|
|
|
|
2021-08-24 06:15:16 +08:00
|
|
|
|
using System;
|
2023-07-19 12:38:04 +08:00
|
|
|
|
using System.Diagnostics;
|
2021-09-05 02:50:30 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2021-09-19 04:54:12 +08:00
|
|
|
|
using osu.Framework.Extensions.EnumExtensions;
|
2021-08-24 06:15:16 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Primitives;
|
2022-07-29 21:33:34 +08:00
|
|
|
|
using osu.Framework.Graphics.Rendering;
|
2021-08-24 06:15:16 +08:00
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
|
|
|
|
using osu.Framework.Graphics.Textures;
|
2021-09-19 21:06:15 +08:00
|
|
|
|
using osu.Framework.Utils;
|
2021-08-24 06:15:16 +08:00
|
|
|
|
using osuTK;
|
|
|
|
|
|
2021-09-13 06:35:13 +08:00
|
|
|
|
namespace osu.Game.Graphics
|
2021-08-24 06:15:16 +08:00
|
|
|
|
{
|
2021-09-19 20:00:56 +08:00
|
|
|
|
public abstract partial class ParticleSpewer : Sprite
|
2021-08-24 06:15:16 +08:00
|
|
|
|
{
|
|
|
|
|
private readonly FallingParticle[] particles;
|
|
|
|
|
private int currentIndex;
|
2023-07-18 18:52:45 +08:00
|
|
|
|
private double? lastParticleAdded;
|
2021-08-24 06:15:16 +08:00
|
|
|
|
|
2023-07-18 18:52:45 +08:00
|
|
|
|
private readonly double timeBetweenSpawns;
|
2021-09-19 09:19:16 +08:00
|
|
|
|
private readonly double maxDuration;
|
2021-08-24 06:15:16 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines whether particles are being spawned.
|
|
|
|
|
/// </summary>
|
2021-09-05 02:50:30 +08:00
|
|
|
|
public readonly BindableBool Active = new BindableBool();
|
2021-08-24 06:15:16 +08:00
|
|
|
|
|
2021-09-16 15:54:22 +08:00
|
|
|
|
public override bool IsPresent => base.IsPresent && hasActiveParticles;
|
2021-08-24 06:15:16 +08:00
|
|
|
|
|
2021-09-19 20:00:56 +08:00
|
|
|
|
protected virtual bool CanSpawnParticles => true;
|
|
|
|
|
protected virtual float ParticleGravity => 0;
|
2021-09-16 15:54:22 +08:00
|
|
|
|
|
2021-09-19 09:19:16 +08:00
|
|
|
|
private bool hasActiveParticles => Active.Value || (lastParticleAdded + maxDuration) > Time.Current;
|
|
|
|
|
|
2021-09-19 20:00:56 +08:00
|
|
|
|
protected ParticleSpewer(Texture texture, int perSecond, double maxDuration)
|
2021-08-24 06:15:16 +08:00
|
|
|
|
{
|
|
|
|
|
Texture = texture;
|
|
|
|
|
Blending = BlendingParameters.Additive;
|
|
|
|
|
|
2021-09-19 09:19:16 +08:00
|
|
|
|
particles = new FallingParticle[perSecond * (int)Math.Ceiling(maxDuration / 1000)];
|
2021-08-24 06:15:16 +08:00
|
|
|
|
|
2023-07-18 18:52:45 +08:00
|
|
|
|
timeBetweenSpawns = 1000f / perSecond;
|
2021-09-19 09:19:16 +08:00
|
|
|
|
this.maxDuration = maxDuration;
|
2021-08-24 06:15:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 13:44:05 +08: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-24 06:15:16 +08:00
|
|
|
|
protected override void Update()
|
|
|
|
|
{
|
|
|
|
|
base.Update();
|
|
|
|
|
|
2023-07-18 18:52:45 +08:00
|
|
|
|
Invalidate(Invalidation.DrawNode);
|
|
|
|
|
|
|
|
|
|
if (!Active.Value || !CanSpawnParticles)
|
|
|
|
|
return;
|
|
|
|
|
|
2023-07-19 12:38:04 +08:00
|
|
|
|
if (lastParticleAdded == null)
|
2021-08-24 06:15:16 +08:00
|
|
|
|
{
|
2023-07-19 12:38:04 +08:00
|
|
|
|
lastParticleAdded = Time.Current;
|
|
|
|
|
spawnParticle();
|
2023-07-19 13:10:27 +08:00
|
|
|
|
return;
|
2023-07-19 12:38:04 +08:00
|
|
|
|
}
|
2023-07-18 18:52:45 +08:00
|
|
|
|
|
2023-07-19 13:10:27 +08:00
|
|
|
|
double timeElapsed = Time.Current - lastParticleAdded.Value;
|
2021-09-16 15:50:03 +08:00
|
|
|
|
|
2023-07-19 12:38:04 +08:00
|
|
|
|
// Avoid spawning too many particles if a long amount of time has passed.
|
2023-07-19 13:10:27 +08:00
|
|
|
|
if (Math.Abs(timeElapsed) > maxDuration)
|
2023-07-19 12:38:04 +08:00
|
|
|
|
{
|
|
|
|
|
lastParticleAdded = Time.Current;
|
|
|
|
|
spawnParticle();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-09-10 05:18:19 +08:00
|
|
|
|
|
2023-07-19 12:38:04 +08:00
|
|
|
|
Debug.Assert(lastParticleAdded != null);
|
2021-08-24 06:15:16 +08:00
|
|
|
|
|
2023-07-19 12:38:04 +08:00
|
|
|
|
for (int i = 0; i < timeElapsed / timeBetweenSpawns; i++)
|
|
|
|
|
{
|
2023-07-18 18:52:45 +08:00
|
|
|
|
lastParticleAdded += timeBetweenSpawns;
|
2023-07-19 12:38:04 +08:00
|
|
|
|
spawnParticle();
|
2023-07-18 18:52:45 +08:00
|
|
|
|
}
|
2021-08-24 06:15:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-19 12:38:04 +08: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 20:00:56 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Called each time a new particle should be spawned.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual FallingParticle CreateParticle() => new FallingParticle();
|
|
|
|
|
|
2021-08-24 06:15:16 +08:00
|
|
|
|
protected override DrawNode CreateDrawNode() => new ParticleSpewerDrawNode(this);
|
|
|
|
|
|
2023-07-19 12:52:43 +08:00
|
|
|
|
#region DrawNode
|
2021-08-30 03:41:47 +08:00
|
|
|
|
|
2021-08-24 06:15:16 +08:00
|
|
|
|
private class ParticleSpewerDrawNode : SpriteDrawNode
|
|
|
|
|
{
|
|
|
|
|
private readonly FallingParticle[] particles;
|
|
|
|
|
|
|
|
|
|
protected new ParticleSpewer Source => (ParticleSpewer)base.Source;
|
|
|
|
|
|
2021-09-19 09:19:16 +08:00
|
|
|
|
private readonly float maxDuration;
|
2021-09-19 05:45:58 +08:00
|
|
|
|
|
2021-08-24 06:15:16 +08:00
|
|
|
|
private float currentTime;
|
|
|
|
|
private float gravity;
|
2021-09-19 04:54:12 +08:00
|
|
|
|
private Axes relativePositionAxes;
|
|
|
|
|
private Vector2 sourceSize;
|
2021-08-24 06:15:16 +08:00
|
|
|
|
|
2021-09-19 20:49:09 +08:00
|
|
|
|
public ParticleSpewerDrawNode(ParticleSpewer source)
|
2021-08-24 06:15:16 +08:00
|
|
|
|
: base(source)
|
|
|
|
|
{
|
|
|
|
|
particles = new FallingParticle[Source.particles.Length];
|
2021-09-19 09:19:16 +08:00
|
|
|
|
maxDuration = (float)Source.maxDuration;
|
2021-08-24 06:15:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void ApplyState()
|
|
|
|
|
{
|
|
|
|
|
base.ApplyState();
|
|
|
|
|
|
|
|
|
|
Source.particles.CopyTo(particles, 0);
|
|
|
|
|
|
|
|
|
|
currentTime = (float)Source.Time.Current;
|
|
|
|
|
gravity = Source.ParticleGravity;
|
2021-09-19 04:54:12 +08:00
|
|
|
|
relativePositionAxes = Source.RelativePositionAxes;
|
|
|
|
|
sourceSize = Source.DrawSize;
|
2021-08-24 06:15:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-29 21:33:34 +08:00
|
|
|
|
protected override void Blit(IRenderer renderer)
|
2021-08-24 06:15:16 +08:00
|
|
|
|
{
|
|
|
|
|
foreach (var p in particles)
|
|
|
|
|
{
|
2022-04-09 21:29:55 +08:00
|
|
|
|
if (p.Duration == 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
2021-10-27 12:04:41 +08:00
|
|
|
|
float timeSinceStart = currentTime - p.StartTime;
|
2021-08-24 06:15:16 +08:00
|
|
|
|
|
2021-08-30 03:41:47 +08:00
|
|
|
|
// ignore particles from the future.
|
|
|
|
|
// these can appear when seeking in replays.
|
|
|
|
|
if (timeSinceStart < 0) continue;
|
|
|
|
|
|
2021-10-27 12:04:41 +08:00
|
|
|
|
float alpha = p.AlphaAtTime(timeSinceStart);
|
2021-08-24 06:15:16 +08:00
|
|
|
|
if (alpha <= 0) continue;
|
|
|
|
|
|
2021-09-19 09:19:16 +08:00
|
|
|
|
var pos = p.PositionAtTime(timeSinceStart, gravity, maxDuration);
|
2021-10-27 12:04:41 +08:00
|
|
|
|
float scale = p.ScaleAtTime(timeSinceStart);
|
|
|
|
|
float angle = p.AngleAtTime(timeSinceStart);
|
2021-08-24 06:15:16 +08:00
|
|
|
|
|
2021-09-19 04:54:12 +08:00
|
|
|
|
var rect = createDrawRect(pos, scale);
|
2021-08-24 06:15:16 +08:00
|
|
|
|
|
|
|
|
|
var quad = new Quad(
|
2021-09-14 06:16:42 +08: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-24 06:15:16 +08:00
|
|
|
|
);
|
|
|
|
|
|
2022-08-02 18:50:57 +08:00
|
|
|
|
renderer.DrawQuad(Texture, quad, DrawColourInfo.Colour.MultiplyAlpha(alpha),
|
2022-07-29 21:33:34 +08:00
|
|
|
|
inflationPercentage: new Vector2(InflationAmount.X / DrawRectangle.Width, InflationAmount.Y / DrawRectangle.Height),
|
|
|
|
|
textureCoords: TextureCoords);
|
2021-08-24 06:15:16 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-19 04:54:12 +08:00
|
|
|
|
private RectangleF createDrawRect(Vector2 position, float scale)
|
|
|
|
|
{
|
2021-10-27 12:04:41 +08:00
|
|
|
|
float width = Texture.DisplayWidth * scale;
|
|
|
|
|
float height = Texture.DisplayHeight * scale;
|
2021-09-19 04:54:12 +08:00
|
|
|
|
|
|
|
|
|
if (relativePositionAxes.HasFlagFast(Axes.X))
|
|
|
|
|
position.X *= sourceSize.X;
|
|
|
|
|
if (relativePositionAxes.HasFlagFast(Axes.Y))
|
|
|
|
|
position.Y *= sourceSize.Y;
|
|
|
|
|
|
|
|
|
|
return new RectangleF(
|
|
|
|
|
position.X - width / 2,
|
|
|
|
|
position.Y - height / 2,
|
|
|
|
|
width,
|
|
|
|
|
height);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-14 06:16:42 +08:00
|
|
|
|
private Vector2 transformPosition(Vector2 pos, Vector2 centre, float angle)
|
2021-08-24 06:15:16 +08: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-10 05:18:19 +08:00
|
|
|
|
return Vector2Extensions.Transform(new Vector2(x, y), DrawInfo.Matrix);
|
2021-08-24 06:15:16 +08:00
|
|
|
|
}
|
2021-09-21 13:25:44 +08:00
|
|
|
|
|
|
|
|
|
protected override bool CanDrawOpaqueInterior => false;
|
2021-08-24 06:15:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-30 03:41:47 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
2021-09-19 20:00:56 +08:00
|
|
|
|
protected struct FallingParticle
|
2021-08-24 06:15:16 +08:00
|
|
|
|
{
|
|
|
|
|
public float StartTime;
|
2021-08-30 03:41:47 +08:00
|
|
|
|
public Vector2 StartPosition;
|
2021-08-24 06:15:16 +08:00
|
|
|
|
public Vector2 Velocity;
|
|
|
|
|
public float Duration;
|
2021-09-05 23:08:00 +08:00
|
|
|
|
public float StartAngle;
|
|
|
|
|
public float EndAngle;
|
2021-08-24 06:15:16 +08:00
|
|
|
|
public float EndScale;
|
|
|
|
|
|
|
|
|
|
public float AlphaAtTime(float timeSinceStart) => 1 - progressAtTime(timeSinceStart);
|
|
|
|
|
|
2021-09-19 21:06:15 +08:00
|
|
|
|
public float ScaleAtTime(float timeSinceStart) => (float)Interpolation.Lerp(1, EndScale, progressAtTime(timeSinceStart));
|
2021-08-24 06:15:16 +08:00
|
|
|
|
|
2021-09-19 21:06:15 +08:00
|
|
|
|
public float AngleAtTime(float timeSinceStart) => (float)Interpolation.Lerp(StartAngle, EndAngle, progressAtTime(timeSinceStart));
|
2021-08-24 06:15:16 +08:00
|
|
|
|
|
2021-09-19 05:45:58 +08:00
|
|
|
|
public Vector2 PositionAtTime(float timeSinceStart, float gravity, float maxDuration)
|
2021-08-24 06:15:16 +08:00
|
|
|
|
{
|
2021-10-27 12:04:41 +08:00
|
|
|
|
float progress = progressAtTime(timeSinceStart);
|
2021-09-19 05:45:58 +08:00
|
|
|
|
var currentGravity = new Vector2(0, gravity * Duration / maxDuration * progress);
|
2021-08-24 06:15:16 +08:00
|
|
|
|
|
2021-09-19 05:45:58 +08:00
|
|
|
|
return StartPosition + (Velocity + currentGravity) * timeSinceStart / maxDuration;
|
2021-08-24 06:15:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private float progressAtTime(float timeSinceStart) => Math.Clamp(timeSinceStart / Duration, 0, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|