osu/osu.Game.Rulesets.Catch/Replays/CatchAutoGenerator.cs

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

119 lines
4.5 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.
2018-04-13 09:19:50 +00:00
using System;
2018-01-12 09:35:28 +00:00
using System.Linq;
2020-01-09 04:43:44 +00:00
using osu.Framework.Utils;
2018-01-12 09:35:28 +00:00
using osu.Game.Beatmaps;
2019-03-07 09:30:31 +00:00
using osu.Game.Rulesets.Catch.Beatmaps;
2018-01-12 09:35:28 +00:00
using osu.Game.Rulesets.Catch.Objects;
using osu.Game.Rulesets.Catch.UI;
2018-01-12 09:35:28 +00:00
using osu.Game.Rulesets.Replays;
2018-04-13 09:19:50 +00:00
2018-01-12 09:35:28 +00:00
namespace osu.Game.Rulesets.Catch.Replays
{
internal class CatchAutoGenerator : AutoGenerator<CatchReplayFrame>
2018-01-12 09:35:28 +00:00
{
2019-03-07 09:30:31 +00:00
public new CatchBeatmap Beatmap => (CatchBeatmap)base.Beatmap;
public CatchAutoGenerator(IBeatmap beatmap)
2018-01-12 09:35:28 +00:00
: base(beatmap)
{
}
2018-04-13 09:19:50 +00:00
protected override void GenerateFrames()
2018-01-12 09:35:28 +00:00
{
if (Beatmap.HitObjects.Count == 0)
return;
float lastPosition = CatchPlayfield.CENTER_X;
double lastTime = 0;
2018-04-13 09:19:50 +00:00
void moveToNext(PalpableCatchHitObject h)
{
float positionChange = Math.Abs(lastPosition - h.EffectiveX);
double timeAvailable = h.StartTime - lastTime;
2018-04-13 09:19:50 +00:00
if (timeAvailable < 0)
2021-01-29 02:03:23 +00:00
{
return;
2021-01-29 02:03:23 +00:00
}
2019-06-15 20:41:10 +00:00
// So we can either make it there without a dash or not.
// If positionChange is 0, we don't need to move, so speedRequired should also be 0 (could be NaN if timeAvailable is 0 too)
// The case where positionChange > 0 and timeAvailable == 0 results in PositiveInfinity which provides expected beheaviour.
2019-06-15 17:14:24 +00:00
double speedRequired = positionChange == 0 ? 0 : positionChange / timeAvailable;
2018-04-13 09:19:50 +00:00
bool dashRequired = speedRequired > Catcher.BASE_WALK_SPEED;
bool impossibleJump = speedRequired > Catcher.BASE_DASH_SPEED;
2018-04-13 09:19:50 +00:00
// todo: get correct catcher size, based on difficulty CS.
const float catcher_width_half = Catcher.BASE_SIZE * 0.3f * 0.5f;
2018-04-13 09:19:50 +00:00
if (lastPosition - catcher_width_half < h.EffectiveX && lastPosition + catcher_width_half > h.EffectiveX)
{
2020-05-05 01:31:11 +00:00
// we are already in the correct range.
lastTime = h.StartTime;
addFrame(h.StartTime, lastPosition);
return;
}
2018-04-13 09:19:50 +00:00
if (impossibleJump)
{
addFrame(h.StartTime, h.EffectiveX);
}
else if (h.HyperDash)
{
addFrame(h.StartTime - timeAvailable, lastPosition);
addFrame(h.StartTime, h.EffectiveX);
}
else if (dashRequired)
{
2020-05-05 01:31:11 +00:00
// we do a movement in two parts - the dash part then the normal part...
double timeAtNormalSpeed = positionChange / Catcher.BASE_WALK_SPEED;
double timeWeNeedToSave = timeAtNormalSpeed - timeAvailable;
double timeAtDashSpeed = timeWeNeedToSave / 2;
2018-04-13 09:19:50 +00:00
float midPosition = (float)Interpolation.Lerp(lastPosition, h.EffectiveX, (float)timeAtDashSpeed / timeAvailable);
2018-04-13 09:19:50 +00:00
2020-05-05 01:31:11 +00:00
// dash movement
addFrame(h.StartTime - timeAvailable + 1, lastPosition, true);
addFrame(h.StartTime - timeAvailable + timeAtDashSpeed, midPosition);
addFrame(h.StartTime, h.EffectiveX);
}
else
{
double timeBefore = positionChange / Catcher.BASE_WALK_SPEED;
2018-04-13 09:19:50 +00:00
addFrame(h.StartTime - timeBefore, lastPosition);
addFrame(h.StartTime, h.EffectiveX);
}
2018-04-13 09:19:50 +00:00
lastTime = h.StartTime;
lastPosition = h.EffectiveX;
}
2018-04-13 09:19:50 +00:00
2018-01-12 09:35:28 +00:00
foreach (var obj in Beatmap.HitObjects)
{
if (obj is PalpableCatchHitObject palpableObject)
2018-01-12 09:35:28 +00:00
{
moveToNext(palpableObject);
2018-01-12 09:35:28 +00:00
}
2018-04-13 09:19:50 +00:00
2018-01-12 09:35:28 +00:00
foreach (var nestedObj in obj.NestedHitObjects.Cast<CatchHitObject>())
{
if (nestedObj is PalpableCatchHitObject palpableNestedObject)
2018-01-12 09:35:28 +00:00
{
moveToNext(palpableNestedObject);
2018-01-12 09:35:28 +00:00
}
}
}
}
private void addFrame(double time, float? position = null, bool dashing = false)
{
Frames.Add(new CatchReplayFrame(time, position, dashing, LastFrame));
}
2018-01-12 09:35:28 +00:00
}
}