1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 12:37:29 +08:00
osu-lazer/osu.Game.Rulesets.Osu/Mods/OsuModAlternate.cs

99 lines
3.3 KiB
C#
Raw Normal View History

2022-01-29 20:38:12 +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.
using System;
using System.Linq;
using osu.Framework.Bindables;
2022-01-29 20:38:12 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events;
2022-01-29 20:38:12 +08:00
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Osu.Objects;
using osu.Game.Rulesets.Scoring;
2022-01-29 20:38:12 +08:00
using osu.Game.Rulesets.UI;
using osu.Game.Screens.Play;
2022-01-29 20:38:12 +08:00
namespace osu.Game.Rulesets.Osu.Mods
{
public class OsuModAlternate : Mod, IApplicableToDrawableRuleset<OsuHitObject>, IApplicableToPlayer, IUpdatableByPlayfield
2022-01-29 20:38:12 +08:00
{
public override string Name => @"Alternate";
public override string Acronym => @"AL";
public override string Description => @"Don't use the same key twice in a row!";
public override double ScoreMultiplier => 1.0;
public override Type[] IncompatibleMods => new[] { typeof(ModAutoplay) };
public override ModType Type => ModType.Conversion;
public override IconUsage? Icon => FontAwesome.Solid.Keyboard;
private bool introEnded;
private double earliestStartTime;
private IBindable<bool> isBreakTime;
2022-01-29 20:38:12 +08:00
private const double flash_duration = 1000;
private OsuAction? lastActionPressed;
private DrawableRuleset<OsuHitObject> ruleset;
public void ApplyToDrawableRuleset(DrawableRuleset<OsuHitObject> drawableRuleset)
2022-01-29 20:38:12 +08:00
{
ruleset = drawableRuleset;
drawableRuleset.KeyBindingInputManager.Add(new InputInterceptor(this));
var firstHitObject = ruleset.Objects.FirstOrDefault();
earliestStartTime = (firstHitObject?.StartTime ?? 0) - (firstHitObject?.HitWindows.WindowFor(HitResult.Meh) ?? 0);
2022-01-29 20:38:12 +08:00
}
public void ApplyToPlayer(Player player)
2022-01-29 20:38:12 +08:00
{
isBreakTime = player.IsBreakTime.GetBoundCopy();
isBreakTime.ValueChanged += e =>
{
if (e.NewValue)
lastActionPressed = null;
};
2022-01-29 20:38:12 +08:00
}
private bool checkCorrectAction(OsuAction action)
2022-01-29 20:38:12 +08:00
{
if (isBreakTime.Value)
return true;
if (!introEnded)
return true;
if (lastActionPressed != action)
2022-01-29 20:38:12 +08:00
{
// User alternated correctly
lastActionPressed = action;
2022-01-29 20:38:12 +08:00
return true;
}
ruleset.Cursor.FlashColour(Colour4.Red, flash_duration, Easing.OutQuint);
2022-01-29 20:38:12 +08:00
return false;
}
public void Update(Playfield playfield)
{
if (!introEnded)
introEnded = playfield.Clock.CurrentTime > earliestStartTime;
}
private class InputInterceptor : Component, IKeyBindingHandler<OsuAction>
{
private readonly OsuModAlternate mod;
public InputInterceptor(OsuModAlternate mod)
{
this.mod = mod;
}
public bool OnPressed(KeyBindingPressEvent<OsuAction> e)
// if the pressed action is incorrect, block it from reaching gameplay.
=> !mod.checkCorrectAction(e.Action);
public void OnReleased(KeyBindingReleaseEvent<OsuAction> e)
{
}
}
2022-01-29 20:38:12 +08:00
}
}