1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 15:47:26 +08:00
osu-lazer/osu.Game/Screens/Utility/CircleGameplay.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

243 lines
7.6 KiB
C#
Raw Normal View History

// 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.
#nullable enable
2022-06-10 16:17:09 +08:00
using System;
using System.Collections.Generic;
2022-06-10 17:27:58 +08:00
using osu.Framework.Bindables;
2022-06-10 16:17:09 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2022-06-10 16:17:09 +08:00
using osu.Framework.Graphics.Shapes;
2022-06-10 17:27:58 +08:00
using osu.Framework.Graphics.UserInterface;
2022-06-10 16:17:09 +08:00
using osu.Framework.Input.Events;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
2022-06-10 17:27:58 +08:00
using osu.Game.Overlays.Settings;
2022-06-10 16:17:09 +08:00
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Scoring;
using osuTK;
using osuTK.Graphics;
namespace osu.Game.Screens.Utility
{
2022-06-10 17:18:24 +08:00
public class CircleGameplay : CompositeDrawable
{
2022-06-10 16:17:09 +08:00
private int nextLocation;
private OsuSpriteText unstableRate = null!;
private readonly List<HitEvent> hitEvents = new List<HitEvent>();
2022-06-10 17:27:58 +08:00
private double? lastGeneratedBeatTime;
2022-06-10 17:18:24 +08:00
2022-06-10 17:27:58 +08:00
private static readonly BindableDouble beat_length = new BindableDouble(500) { MinValue = 200, MaxValue = 1000 };
private static readonly BindableDouble approach_rate_milliseconds = new BindableDouble(100) { MinValue = 50, MaxValue = 500 };
private static readonly BindableFloat spacing = new BindableFloat(0.2f) { MinValue = 0.05f, MaxValue = 0.4f };
2022-06-10 17:18:24 +08:00
2022-06-10 16:17:09 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
InternalChildren = new Drawable[]
{
2022-06-10 17:27:58 +08:00
new FillFlowContainer
{
AutoSizeAxes = Axes.Y,
Width = 400,
Spacing = new Vector2(2),
Direction = FillDirection.Vertical,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Children = new Drawable[]
{
new SettingsSlider<double>
{
LabelText = "time spacing",
Current = beat_length
},
new SettingsSlider<float>
{
LabelText = "visual spacing",
Current = spacing
},
new SettingsSlider<double>
{
LabelText = "approach time",
Current = approach_rate_milliseconds
},
}
},
2022-06-10 16:17:09 +08:00
unstableRate = new OsuSpriteText
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
2022-06-10 17:27:58 +08:00
Font = OsuFont.Default.With(size: 24),
Y = -100,
},
2022-06-10 16:17:09 +08:00
};
}
2022-06-10 17:18:24 +08:00
protected override void Update()
2022-06-10 16:17:09 +08:00
{
2022-06-10 17:18:24 +08:00
base.Update();
2022-06-10 17:27:58 +08:00
// We want to generate a few hit objects ahead of the current time (to allow them to animate).
int nextBeat = (int)(Clock.CurrentTime / beat_length.Value) + 1;
double generateUpTo = (nextBeat + 2) * beat_length.Value;
2022-06-10 17:18:24 +08:00
2022-06-10 17:27:58 +08:00
while (lastGeneratedBeatTime == null || lastGeneratedBeatTime < generateUpTo)
2022-06-10 17:18:24 +08:00
{
2022-06-10 17:27:58 +08:00
double time = ++nextBeat * beat_length.Value;
if (time <= lastGeneratedBeatTime)
continue;
newBeat(time);
lastGeneratedBeatTime = time;
2022-06-10 17:18:24 +08:00
}
}
2022-06-10 16:17:09 +08:00
2022-06-10 17:27:58 +08:00
private void newBeat(double time)
2022-06-10 17:18:24 +08:00
{
2022-06-10 16:17:09 +08:00
nextLocation++;
Vector2 location;
2022-06-10 17:27:58 +08:00
float spacingLow = 0.5f - spacing.Value;
float spacingHigh = 0.5f + spacing.Value;
2022-06-10 16:27:06 +08:00
2022-06-10 16:17:09 +08:00
switch (nextLocation % 4)
{
default:
2022-06-10 17:27:58 +08:00
location = new Vector2(spacingLow, spacingLow);
2022-06-10 16:17:09 +08:00
break;
case 1:
2022-06-10 17:27:58 +08:00
location = new Vector2(spacingHigh, spacingHigh);
2022-06-10 16:17:09 +08:00
break;
case 2:
2022-06-10 17:27:58 +08:00
location = new Vector2(spacingHigh, spacingLow);
2022-06-10 16:17:09 +08:00
break;
case 3:
2022-06-10 17:27:58 +08:00
location = new Vector2(spacingLow, spacingHigh);
2022-06-10 16:17:09 +08:00
break;
}
2022-06-10 17:27:58 +08:00
AddInternal(new SampleHitCircle(time)
2022-06-10 16:17:09 +08:00
{
RelativePositionAxes = Axes.Both,
Position = location,
Hit = hit,
});
}
private void hit(HitEvent h)
{
hitEvents.Add(h);
unstableRate.Text = $"{hitEvents.CalculateUnstableRate():N1}";
}
public class SampleHitCircle : CompositeDrawable
{
public HitEvent? HitEvent;
public Action<HitEvent>? Hit { get; set; }
public readonly double HitTime;
private readonly CircularContainer approach;
private readonly Circle circle;
private const float size = 100;
2022-06-10 16:27:06 +08:00
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos)
=> circle.ReceivePositionalInputAt(screenSpacePos);
2022-06-10 16:17:09 +08:00
public SampleHitCircle(double hitTime)
{
HitTime = hitTime;
Origin = Anchor.Centre;
AutoSizeAxes = Axes.Both;
2022-06-10 17:18:24 +08:00
AlwaysPresent = true;
2022-06-10 16:17:09 +08:00
InternalChildren = new Drawable[]
{
circle = new Circle
{
Colour = Color4.White,
Size = new Vector2(size),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
},
approach = new CircularContainer
{
BorderColour = Color4.Yellow,
Size = new Vector2(size),
Masking = true,
BorderThickness = 4,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Children = new Drawable[]
{
new Box
{
Colour = Color4.Black,
Alpha = 0,
AlwaysPresent = true,
RelativeSizeAxes = Axes.Both,
},
}
},
};
}
protected override bool OnMouseDown(MouseDownEvent e)
{
if (HitEvent != null)
return false;
if (Math.Abs(Clock.CurrentTime - HitTime) > 200)
return false;
2022-06-10 16:17:09 +08:00
approach.Expire();
circle
.FadeOut(200)
.ScaleTo(1.5f, 200);
HitEvent = new HitEvent(Clock.CurrentTime - HitTime, HitResult.Good, new HitObject
{
HitWindows = new HitWindows(),
}, null, null);
Hit?.Invoke(HitEvent.Value);
this.Delay(200).Expire();
return true;
}
protected override void Update()
{
base.Update();
if (HitEvent == null)
{
approach.Scale = new Vector2(1 + (float)MathHelper.Clamp((HitTime - Clock.CurrentTime) / approach_rate_milliseconds.Value, 0, 100));
Alpha = (float)MathHelper.Clamp((Clock.CurrentTime - HitTime + 600) / 400, 0, 1);
2022-06-10 16:17:09 +08:00
if (Clock.CurrentTime > HitTime + 200)
Expire();
}
2022-06-10 16:17:09 +08:00
}
}
}
}