2020-11-19 05:54:31 +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.
|
|
|
|
|
|
|
|
|
|
using System;
|
2020-11-19 08:22:37 +00:00
|
|
|
|
using System.Collections.Generic;
|
2020-11-19 05:54:31 +00:00
|
|
|
|
using osu.Framework.Graphics;
|
2020-11-19 08:22:37 +00:00
|
|
|
|
using osu.Framework.Graphics.Primitives;
|
2022-07-29 13:33:34 +00:00
|
|
|
|
using osu.Framework.Graphics.Rendering;
|
2020-11-19 05:54:31 +00:00
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
|
|
|
|
using osu.Framework.Graphics.Textures;
|
|
|
|
|
using osu.Framework.Utils;
|
|
|
|
|
using osuTK;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics
|
|
|
|
|
{
|
2020-11-19 09:05:41 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// An explosion of textured particles based on how osu-stable randomises the explosion pattern.
|
|
|
|
|
/// </summary>
|
2020-11-19 08:22:37 +00:00
|
|
|
|
public partial class ParticleExplosion : Sprite
|
2020-11-19 05:54:31 +00:00
|
|
|
|
{
|
2020-11-19 09:05:41 +00:00
|
|
|
|
private readonly int particleCount;
|
2020-11-19 08:22:37 +00:00
|
|
|
|
private readonly double duration;
|
|
|
|
|
private double startTime;
|
|
|
|
|
|
|
|
|
|
private readonly List<ParticlePart> parts = new List<ParticlePart>();
|
|
|
|
|
|
2020-11-19 05:54:31 +00:00
|
|
|
|
public ParticleExplosion(Texture texture, int particleCount, double duration)
|
|
|
|
|
{
|
2020-11-19 08:22:37 +00:00
|
|
|
|
Texture = texture;
|
2020-11-19 09:05:41 +00:00
|
|
|
|
this.particleCount = particleCount;
|
2020-11-19 08:22:37 +00:00
|
|
|
|
this.duration = duration;
|
|
|
|
|
Blending = BlendingParameters.Additive;
|
|
|
|
|
}
|
2020-11-19 05:54:31 +00:00
|
|
|
|
|
2020-11-19 08:22:37 +00:00
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
|
|
|
|
Restart();
|
2020-11-19 05:54:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-19 09:05:41 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Restart the animation from the current point in time.
|
|
|
|
|
/// Supports transform time offset chaining.
|
|
|
|
|
/// </summary>
|
2020-11-19 06:47:02 +00:00
|
|
|
|
public void Restart()
|
|
|
|
|
{
|
2020-11-19 08:22:37 +00:00
|
|
|
|
startTime = TransformStartTime;
|
|
|
|
|
this.FadeOutFromOne(duration);
|
|
|
|
|
|
2020-11-19 09:05:41 +00:00
|
|
|
|
parts.Clear();
|
|
|
|
|
for (int i = 0; i < particleCount; i++)
|
|
|
|
|
parts.Add(new ParticlePart(duration));
|
2020-11-19 08:22:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Update()
|
|
|
|
|
{
|
|
|
|
|
base.Update();
|
|
|
|
|
Invalidate(Invalidation.DrawNode);
|
2020-11-19 06:47:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-19 08:22:37 +00:00
|
|
|
|
protected override DrawNode CreateDrawNode() => new ParticleExplosionDrawNode(this);
|
|
|
|
|
|
|
|
|
|
private class ParticleExplosionDrawNode : SpriteDrawNode
|
2020-11-19 05:54:31 +00:00
|
|
|
|
{
|
2020-11-19 09:05:41 +00:00
|
|
|
|
private readonly List<ParticlePart> parts = new List<ParticlePart>();
|
2020-11-19 08:22:37 +00:00
|
|
|
|
|
|
|
|
|
private ParticleExplosion source => (ParticleExplosion)Source;
|
2020-11-19 05:54:31 +00:00
|
|
|
|
|
2020-11-19 08:22:37 +00:00
|
|
|
|
private double startTime;
|
|
|
|
|
private double currentTime;
|
|
|
|
|
private Vector2 sourceSize;
|
2020-11-19 06:47:02 +00:00
|
|
|
|
|
2020-11-19 08:22:37 +00:00
|
|
|
|
public ParticleExplosionDrawNode(Sprite source)
|
|
|
|
|
: base(source)
|
2020-11-19 05:54:31 +00:00
|
|
|
|
{
|
2020-11-19 08:22:37 +00:00
|
|
|
|
}
|
2020-11-19 06:47:02 +00:00
|
|
|
|
|
2020-11-19 08:22:37 +00:00
|
|
|
|
public override void ApplyState()
|
|
|
|
|
{
|
|
|
|
|
base.ApplyState();
|
2020-11-19 06:47:02 +00:00
|
|
|
|
|
2020-11-19 09:05:41 +00:00
|
|
|
|
parts.Clear();
|
|
|
|
|
parts.AddRange(source.parts);
|
|
|
|
|
|
2020-11-19 08:22:37 +00:00
|
|
|
|
sourceSize = source.Size;
|
|
|
|
|
startTime = source.startTime;
|
|
|
|
|
currentTime = source.Time.Current;
|
2020-11-19 05:54:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-29 13:33:34 +00:00
|
|
|
|
protected override void Blit(IRenderer renderer)
|
2020-11-19 05:54:31 +00:00
|
|
|
|
{
|
2021-10-27 04:04:41 +00:00
|
|
|
|
double time = currentTime - startTime;
|
2020-11-19 08:56:11 +00:00
|
|
|
|
|
2020-11-19 08:22:37 +00:00
|
|
|
|
foreach (var p in parts)
|
|
|
|
|
{
|
2020-11-19 08:56:11 +00:00
|
|
|
|
Vector2 pos = p.PositionAtTime(time);
|
|
|
|
|
float alpha = p.AlphaAtTime(time);
|
2020-11-19 08:22:37 +00:00
|
|
|
|
|
2020-11-19 08:56:11 +00:00
|
|
|
|
var rect = new RectangleF(
|
|
|
|
|
pos.X * sourceSize.X - Texture.DisplayWidth / 2,
|
|
|
|
|
pos.Y * sourceSize.Y - Texture.DisplayHeight / 2,
|
|
|
|
|
Texture.DisplayWidth,
|
|
|
|
|
Texture.DisplayHeight);
|
2020-11-19 08:22:37 +00:00
|
|
|
|
|
2020-11-19 08:56:11 +00:00
|
|
|
|
// convert to screen space.
|
2020-11-19 08:22:37 +00:00
|
|
|
|
var quad = new Quad(
|
|
|
|
|
Vector2Extensions.Transform(rect.TopLeft, DrawInfo.Matrix),
|
|
|
|
|
Vector2Extensions.Transform(rect.TopRight, DrawInfo.Matrix),
|
|
|
|
|
Vector2Extensions.Transform(rect.BottomLeft, DrawInfo.Matrix),
|
|
|
|
|
Vector2Extensions.Transform(rect.BottomRight, DrawInfo.Matrix)
|
|
|
|
|
);
|
|
|
|
|
|
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);
|
2020-11-19 08:22:37 +00:00
|
|
|
|
}
|
2020-11-19 06:47:02 +00:00
|
|
|
|
}
|
2021-09-21 05:37:09 +00:00
|
|
|
|
|
|
|
|
|
protected override bool CanDrawOpaqueInterior => false;
|
2020-11-19 08:22:37 +00:00
|
|
|
|
}
|
2020-11-19 06:47:02 +00:00
|
|
|
|
|
2020-11-19 09:05:41 +00:00
|
|
|
|
private readonly struct ParticlePart
|
2020-11-19 08:22:37 +00:00
|
|
|
|
{
|
2020-11-19 09:05:41 +00:00
|
|
|
|
private readonly double duration;
|
2020-11-19 09:46:19 +00:00
|
|
|
|
private readonly float direction;
|
2020-11-19 09:05:41 +00:00
|
|
|
|
private readonly float distance;
|
2020-11-19 08:22:37 +00:00
|
|
|
|
|
2020-11-19 08:56:11 +00:00
|
|
|
|
public ParticlePart(double availableDuration)
|
|
|
|
|
{
|
|
|
|
|
distance = RNG.NextSingle(0.5f);
|
|
|
|
|
duration = RNG.NextDouble(availableDuration / 3, availableDuration);
|
|
|
|
|
direction = RNG.NextSingle(0, MathF.PI * 2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public float AlphaAtTime(double time) => 1 - progressAtTime(time);
|
|
|
|
|
|
2020-11-19 08:22:37 +00:00
|
|
|
|
public Vector2 PositionAtTime(double time)
|
|
|
|
|
{
|
2021-10-27 04:04:41 +00:00
|
|
|
|
float travelledDistance = distance * progressAtTime(time);
|
2020-11-19 09:46:19 +00:00
|
|
|
|
return new Vector2(0.5f) + travelledDistance * new Vector2(MathF.Sin(direction), MathF.Cos(direction));
|
2020-11-19 05:54:31 +00:00
|
|
|
|
}
|
2020-11-19 08:22:37 +00:00
|
|
|
|
|
2020-11-19 08:56:11 +00:00
|
|
|
|
private float progressAtTime(double time) => (float)Math.Clamp(time / duration, 0, 1);
|
2020-11-19 05:54:31 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|