namespace GamePort.Game;
///
/// Objective pad timing from EXE406F50/407070. Layout offset belongs to the
/// separate gadget UI layout; O controls the animation timer, not that offset.
/// Current rendering has one selected objective rather than the full44 flags.
///
public sealed class ObjectivePanel
{
public const float SlideInSeconds = .7f; //48D974.
public const float SlideScale = 1.4285714626312256f; //48D970, original float reciprocal.
public const float SlideDistance = 100; //48D96C.
public const float HoldUntilSeconds = 7; //Integer4EC498.
public const float ExitDuration = 1.7f; //48D940.
public const float HiddenInitialTime = 20; //4018A8/423EC6.
public static float ExitEnd => HoldUntilSeconds + ExitDuration;
private bool objectiveKeyHeld;
private bool openingSoundPlayed;
private bool renderAllowed;
public int CurrentObjective { get; private set; }
public float Elapsed { get; private set; } = HiddenInitialTime;
public float LayoutOffset { get; }
public bool PadVisible => renderAllowed && CurrentObjective > 0 && Elapsed < ExitEnd;
public bool ContentVisible => PadVisible && Elapsed >= SlideInSeconds && Elapsed < HoldUntilSeconds;
public float SlideOffset => Elapsed < SlideInSeconds ? (1 - Elapsed * SlideScale) * SlideDistance
: Elapsed < HoldUntilSeconds ? 0 : (Elapsed - HoldUntilSeconds) * SlideScale * SlideDistance;
public float YOffset => LayoutOffset + (PadVisible ? SlideOffset : 0);
public event Action? SoundEffect;
/// Loaded selector starts hidden; source initializes its independent timer to20.
public ObjectivePanel(int initialObjective = 0, float layoutOffset = -69)
{
if (initialObjective is < 0 or > 43) throw new ArgumentOutOfRangeException(nameof(initialObjective));
if (!float.IsFinite(layoutOffset) || layoutOffset is < -480 or > 480)
throw new ArgumentOutOfRangeException(nameof(layoutOffset));
CurrentObjective = initialObjective;
LayoutOffset = layoutOffset;
}
/// Source406F50: positive assignments reset timing, even for the same selector.
public void Select(int objective)
{
if (objective <= 0) return;
if (objective > 43) throw new ArgumentOutOfRangeException(nameof(objective));
CurrentObjective = objective;
Show();
}
/// Also used after source practice No404D97 resets the panel timer to0.
public void Show() => Elapsed = 0;
/// 407142..407198 edge latch. O hides an opening/stationary panel, otherwise opens it.
public void Key(bool held)
{
if (held && !objectiveKeyHeld)
Elapsed = Elapsed >= 0 && Elapsed < HoldUntilSeconds ? HoldUntilSeconds : 0;
objectiveKeyHeld = held;
}
///
/// UI clock4043D9..4043F9 advances only with a non-null controlled entity.
/// Modal4044D7..404504 forces exit time and hides pad/content. Pause/focus policy
/// is explicit: caller supplies0 elapsed while its UI clock is paused.
///
public void Step(float seconds, bool hasControlledActor = true, bool modalActive = false)
{
if (!float.IsFinite(seconds) || seconds < 0) throw new ArgumentOutOfRangeException(nameof(seconds));
if (hasControlledActor)
{
var next = Elapsed + seconds;
if (!float.IsFinite(next)) throw new ArgumentOutOfRangeException(nameof(seconds));
Elapsed = next;
}
if (modalActive) Elapsed = ExitEnd;
renderAllowed = hasControlledActor && !modalActive;
if (!PadVisible) return;
if (Elapsed < SlideInSeconds)
{
if (openingSoundPlayed) return;
openingSoundPlayed = true;
SoundEffect?.Invoke(216); //4070BA..4070CA.
}
else if (Elapsed < HoldUntilSeconds) openingSoundPlayed = false;
}
}