1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-24 09:27:24 +08:00
osu-lazer/osu.Game.Rulesets.Osu/Skinning/Legacy/LegacyCursorParticles.cs

206 lines
6.7 KiB
C#
Raw Normal View History

2021-09-05 03:49:05 +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.
2021-09-19 09:19:16 +08:00
using System;
2021-09-05 03:49:05 +08:00
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input;
2021-09-05 03:49:05 +08:00
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events;
2021-09-05 03:49:05 +08:00
using osu.Framework.Utils;
using osu.Game.Graphics;
using osu.Game.Rulesets.Osu.Objects.Drawables;
using osu.Game.Rulesets.Osu.UI;
using osu.Game.Screens.Play;
using osu.Game.Skinning;
using osuTK;
2021-09-14 06:36:01 +08:00
using osuTK.Graphics;
2021-09-05 03:49:05 +08:00
namespace osu.Game.Rulesets.Osu.Skinning.Legacy
{
2021-09-19 09:19:16 +08:00
public class LegacyCursorParticles : CompositeDrawable, IKeyBindingHandler<OsuAction>, IRequireHighFrequencyMousePosition
2021-09-05 03:49:05 +08:00
{
2021-09-19 09:19:16 +08:00
private const int particle_lifetime_min = 300;
private const int particle_lifetime_max = 1000;
private const float particle_gravity = 240;
2021-09-13 05:53:41 +08:00
public bool Active => breakSpewer?.Active.Value == true || kiaiSpewer?.Active.Value == true;
2021-09-19 09:19:16 +08:00
private Vector2 cursorVelocity;
private ParticleSpewer breakSpewer;
private ParticleSpewer kiaiSpewer;
2021-09-05 03:49:05 +08:00
[Resolved(canBeNull: true)]
private Player player { get; set; }
[Resolved(canBeNull: true)]
private OsuPlayfield osuPlayfield { get; set; }
[BackgroundDependencyLoader]
2021-09-13 05:53:41 +08:00
private void load(ISkinSource skin, OsuColour colours)
2021-09-05 03:49:05 +08:00
{
var texture = skin.GetTexture("star2");
2021-09-14 06:36:01 +08:00
var starBreakAdditive = skin.GetConfig<OsuSkinColour, Color4>(OsuSkinColour.StarBreakAdditive)?.Value ?? new Color4(255, 182, 193, 255);
2021-09-16 03:22:37 +08:00
if (texture != null)
{
// stable "magic ratio". see OsuPlayfieldAdjustmentContainer for full explanation.
texture.ScaleAdjust *= 1.6f;
}
2021-09-19 09:19:16 +08:00
RelativeSizeAxes = Axes.Both;
Anchor = Anchor.Centre;
2021-09-05 03:49:05 +08:00
InternalChildren = new[]
{
2021-09-19 09:19:16 +08:00
breakSpewer = new ParticleSpewer(texture, 20, particle_lifetime_max)
2021-09-05 03:49:05 +08:00
{
2021-09-19 09:19:16 +08:00
RelativeSizeAxes = Axes.Both,
Size = Vector2.One,
2021-09-14 06:36:01 +08:00
Colour = starBreakAdditive,
2021-09-19 09:19:16 +08:00
ParticleGravity = particle_gravity,
CreateParticle = createBreakParticle,
2021-09-05 03:49:05 +08:00
},
2021-09-19 09:19:16 +08:00
kiaiSpewer = new ParticleSpewer(texture, 60, particle_lifetime_max)
2021-09-05 03:49:05 +08:00
{
2021-09-19 09:19:16 +08:00
RelativeSizeAxes = Axes.Both,
Size = Vector2.One,
2021-09-14 06:36:01 +08:00
Colour = starBreakAdditive,
2021-09-19 09:19:16 +08:00
ParticleGravity = particle_gravity,
CreateParticle = createParticle,
2021-09-05 03:49:05 +08:00
},
};
if (player != null)
{
breakSpewer.Active.BindTarget = player.IsBreakTime;
}
}
protected override void Update()
{
if (osuPlayfield == null) return;
// find active kiai slider or spinner.
var kiaiHitObject = osuPlayfield.HitObjectContainer.AliveObjects.FirstOrDefault(h =>
h.HitObject.Kiai &&
(
(h is DrawableSlider slider && slider.Tracking.Value) ||
(h is DrawableSpinner spinner && spinner.RotationTracker.Tracking)
)
);
kiaiSpewer.Active.Value = kiaiHitObject != null;
}
2021-09-19 09:19:16 +08:00
private Vector2? cursorScreenPosition;
private const double max_velocity_frame_length = 15;
private double velocityFrameLength;
private Vector2 totalPosDifference;
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => true;
protected override bool OnMouseMove(MouseMoveEvent e)
{
if (cursorScreenPosition == null)
{
cursorScreenPosition = e.ScreenSpaceMousePosition;
return base.OnMouseMove(e);
}
// calculate cursor velocity.
totalPosDifference += e.ScreenSpaceMousePosition - cursorScreenPosition.Value;
cursorScreenPosition = e.ScreenSpaceMousePosition;
velocityFrameLength += Math.Abs(Clock.ElapsedFrameTime);
if (velocityFrameLength > max_velocity_frame_length)
{
cursorVelocity = totalPosDifference / (float)velocityFrameLength;
totalPosDifference = Vector2.Zero;
velocityFrameLength = 0;
}
return base.OnMouseMove(e);
}
2021-09-05 03:49:05 +08:00
public bool OnPressed(OsuAction action)
{
handleInput(action, true);
return false;
}
public void OnReleased(OsuAction action)
{
handleInput(action, false);
}
private bool leftPressed;
private bool rightPressed;
private void handleInput(OsuAction action, bool pressed)
{
switch (action)
{
case OsuAction.LeftButton:
leftPressed = pressed;
break;
case OsuAction.RightButton:
rightPressed = pressed;
break;
}
}
2021-09-19 09:19:16 +08:00
private ParticleSpewer.FallingParticle? createParticle()
2021-09-05 03:49:05 +08:00
{
2021-09-19 09:19:16 +08:00
if (!cursorScreenPosition.HasValue) return null;
2021-09-05 03:49:05 +08:00
2021-09-19 09:19:16 +08:00
return new ParticleSpewer.FallingParticle
2021-09-05 03:49:05 +08:00
{
2021-09-19 09:19:16 +08:00
StartPosition = ToLocalSpace(cursorScreenPosition.Value),
Duration = RNG.NextSingle(particle_lifetime_min, particle_lifetime_max),
StartAngle = (float)(RNG.NextDouble() * 4 - 2),
EndAngle = RNG.NextSingle(-2f, 2f),
EndScale = RNG.NextSingle(2f),
Velocity = cursorVelocity * 40,
};
}
2021-09-05 07:01:49 +08:00
2021-09-19 09:19:16 +08:00
private ParticleSpewer.FallingParticle? createBreakParticle()
{
var baseParticle = createParticle();
if (!baseParticle.HasValue) return null;
2021-09-05 07:01:49 +08:00
2021-09-19 09:19:16 +08:00
var p = baseParticle.Value;
2021-09-14 06:16:42 +08:00
2021-09-19 09:19:16 +08:00
if (leftPressed && rightPressed)
2021-09-05 07:01:49 +08:00
{
2021-09-19 09:19:16 +08:00
p.Velocity += new Vector2(
RNG.NextSingle(-460f, 460f),
RNG.NextSingle(-160f, 160f)
);
2021-09-05 07:01:49 +08:00
}
2021-09-19 09:19:16 +08:00
else if (leftPressed)
2021-09-05 07:01:49 +08:00
{
2021-09-19 09:19:16 +08:00
p.Velocity += new Vector2(
RNG.NextSingle(-460f, 0),
RNG.NextSingle(-40f, 40f)
);
2021-09-05 03:49:05 +08:00
}
2021-09-19 09:19:16 +08:00
else if (rightPressed)
2021-09-05 03:49:05 +08:00
{
2021-09-19 09:19:16 +08:00
p.Velocity += new Vector2(
RNG.NextSingle(0, 460f),
RNG.NextSingle(-40f, 40f)
);
2021-09-05 03:49:05 +08:00
}
2021-09-19 09:19:16 +08:00
return p;
2021-09-05 03:49:05 +08:00
}
}
}