1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-30 07:52:56 +08:00
osu-lazer/osu.Game/Graphics/ParticleSpewer.cs

206 lines
7.4 KiB
C#
Raw Normal View History

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;
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
{
public abstract class ParticleSpewer : Sprite
2021-08-24 06:15:16 +08:00
{
private readonly FallingParticle[] particles;
private int currentIndex;
private double lastParticleAdded;
private readonly double cooldown;
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>
public readonly BindableBool Active = new BindableBool();
2021-08-24 06:15:16 +08:00
public override bool IsPresent => base.IsPresent && hasActiveParticles;
2021-08-24 06:15:16 +08:00
protected virtual bool CanSpawnParticles => true;
protected virtual float ParticleGravity => 0;
2021-09-19 09:19:16 +08:00
private bool hasActiveParticles => Active.Value || (lastParticleAdded + maxDuration) > Time.Current;
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
cooldown = 1000f / perSecond;
2021-09-19 09:19:16 +08:00
this.maxDuration = maxDuration;
2021-08-24 06:15:16 +08:00
}
protected override void Update()
{
base.Update();
if (Active.Value && CanSpawnParticles && Math.Abs(Time.Current - lastParticleAdded) > cooldown)
2021-08-24 06:15:16 +08:00
{
var newParticle = CreateParticle();
newParticle.StartTime = (float)Time.Current;
particles[currentIndex] = newParticle;
currentIndex = (currentIndex + 1) % particles.Length;
lastParticleAdded = Time.Current;
2021-08-24 06:15:16 +08:00
}
Invalidate(Invalidation.DrawNode);
}
/// <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);
# region DrawNode
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-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
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)
{
if (p.Duration == 0)
continue;
float timeSinceStart = currentTime - p.StartTime;
2021-08-24 06:15:16 +08:00
// ignore particles from the future.
// these can appear when seeking in replays.
if (timeSinceStart < 0) continue;
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);
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)
{
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;
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
}
#endregion
protected struct FallingParticle
2021-08-24 06:15:16 +08:00
{
public float StartTime;
public Vector2 StartPosition;
2021-08-24 06:15:16 +08:00
public Vector2 Velocity;
public float Duration;
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
public Vector2 PositionAtTime(float timeSinceStart, float gravity, float maxDuration)
2021-08-24 06:15:16 +08:00
{
float progress = progressAtTime(timeSinceStart);
var currentGravity = new Vector2(0, gravity * Duration / maxDuration * progress);
2021-08-24 06:15:16 +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);
}
}
}