Skip to content

Commit

Permalink
Merge pull request #3 from bobofromkirby64/practice-timer
Browse files Browse the repository at this point in the history
Add alternative "practice" timer
  • Loading branch information
psyGamer authored Feb 5, 2024
2 parents 1a934d2 + 73cc8e7 commit 78eb276
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
8 changes: 7 additions & 1 deletion Source/Actors/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private bool InFeatherState

private bool InBubble
=> stateMachine.State == States.Bubble;

public bool IsStrawberryCounterVisible
=> stateMachine.State == States.StrawbGet;

Expand All @@ -187,6 +187,10 @@ public bool IsAbleToPause
&& stateMachine.State != States.StrawbGet
&& stateMachine.State != States.Cassette
&& stateMachine.State != States.Dead;

public bool IsSpeedrunPracticeTimePaused
=> stateMachine.State == States.StrawbGet
|| stateMachine.State == States.Cassette;

public Player()
{
Expand Down Expand Up @@ -258,6 +262,8 @@ public override void Added()
GetCameraTarget(out var orig, out var target, out _);
World.Camera.LookAt = target;
World.Camera.Position = orig;

Save.CurrentRecord.ResetSpeedrunPracticeTime();
}

public override void Update()
Expand Down
25 changes: 25 additions & 0 deletions Source/Save.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class LevelRecord
public Dictionary<string, int> Flags { get; set; } = [];
public int Deaths { get; set; } = 0;
public TimeSpan Time { get; set; } = new();
public TimeSpan SpeedrunPracticeTime { get; set; } = new();

public int GetFlag(string name, int defaultValue = 0)
=> Flags.TryGetValue(name, out int value) ? value : defaultValue;
Expand All @@ -29,6 +30,9 @@ public int SetFlag(string name, int value = 1)

public int IncFlag(string name)
=> Flags[name] = GetFlag(name) + 1;

public void ResetSpeedrunPracticeTime()
=> SpeedrunPracticeTime = new TimeSpan();
}

public static Save Instance = new();
Expand Down Expand Up @@ -58,6 +62,15 @@ public int IncFlag(string name)
/// </summary>
public bool SpeedrunTimer { get; set; } = false;

/// <summary>
/// If the Speeedrun Timer should display in "practice" mode rather than show the total playtime
/// When practice mode is enabled:
/// - Increment the timer while the player has control
/// - Reset the timer on respawn and after entering a cassette room
/// - Pause the timer when collecting a strawberry and when entering a cassette room
/// </summary>
public bool SpeedrunPracticeTimer { get; set; } = false;

/// <summary>
/// 0-10 Music volume level
/// </summary>
Expand Down Expand Up @@ -127,6 +140,18 @@ public void ToggleTimer()
{
SpeedrunTimer = !SpeedrunTimer;
}

public void ToggleSpeedrunPracticeTime()
{
SpeedrunPracticeTimer = !SpeedrunPracticeTimer;
}

public TimeSpan GetCurrentDisplayTime()
{
return SpeedrunPracticeTimer
? CurrentRecord.SpeedrunPracticeTime
: CurrentRecord.Time;
}

public void SetMusicVolume(int value)
{
Expand Down
42 changes: 40 additions & 2 deletions Source/Scenes/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ public enum EntryReasons { Entered, Returned, Respawned }
private int strawbCounterWas;

private bool IsInEndingArea => Get<Player>() is {} player && Overlaps<EndingArea>(player.Position);

private bool IsSpeedrunPracticeTimerPaused => Get<Player>() is { } player && player.IsSpeedrunPracticeTimePaused;

private bool IsSpeedrunPracticeTimerRunning
{
get
{
if (Game.Instance.IsMidTransition) return false;
if (Get<Player>() is not { } player) return true;
return player.IsAbleToPause;
}
}

private bool IsPauseEnabled
{
get
Expand Down Expand Up @@ -83,6 +96,8 @@ public World(EntryInfo entry)
optionsMenu.Add(new Menu.Toggle("Fullscreen", Save.Instance.ToggleFullscreen, () => Save.Instance.Fullscreen));
optionsMenu.Add(new Menu.Toggle("Z-Guide", Save.Instance.ToggleZGuide, () => Save.Instance.ZGuide));
optionsMenu.Add(new Menu.Toggle("Timer", Save.Instance.ToggleTimer, () => Save.Instance.SpeedrunTimer));
optionsMenu.Add(new Menu.Toggle("Speedrun Practice Timer", Save.Instance.ToggleSpeedrunPracticeTime, () => Save.Instance.SpeedrunPracticeTimer));

optionsMenu.Add(new Menu.Spacer());
optionsMenu.Add(new Menu.Slider("BGM", 0, 10, () => Save.Instance.MusicVolume, Save.Instance.SetMusicVolume));
optionsMenu.Add(new Menu.Slider("SFX", 0, 10, () => Save.Instance.SfxVolume, Save.Instance.SetSfxVolume));
Expand Down Expand Up @@ -276,7 +291,7 @@ public override void Update()
// update audio
Audio.SetListener(Camera);

// increment playtime (if not in the ending area)
// increment total playtime (if not in the ending area)
if (!IsInEndingArea)
{
Save.CurrentRecord.Time += TimeSpan.FromSeconds(Time.Delta);
Expand All @@ -287,6 +302,12 @@ public override void Update()
Game.Instance.Music.Set("at_baddy", 1);
}

// increment practice timer
if (IsSpeedrunPracticeTimerRunning)
{
Save.CurrentRecord.SpeedrunPracticeTime += TimeSpan.FromSeconds(Time.Delta);
}

// handle strawb counter
{
// wiggle when gained
Expand Down Expand Up @@ -784,7 +805,24 @@ public override void Render(Target target)
var at = bounds.TopLeft + new Vec2(4, 8);
if (IsInEndingArea || Save.Instance.SpeedrunTimer)
{
UI.Timer(batch, Save.CurrentRecord.Time, at, 0.0f, IsInEndingArea ? Color.CornflowerBlue : Color.White);
Color timerDisplayColor;
if (IsInEndingArea)
{
timerDisplayColor = Color.CornflowerBlue;
}
else if (Save.Instance.SpeedrunPracticeTimer && IsSpeedrunPracticeTimerPaused)
{
timerDisplayColor = Color.Yellow;
}
else if (Save.Instance.SpeedrunPracticeTimer && !IsSpeedrunPracticeTimerRunning)
{
timerDisplayColor = Color.Gray;
}
else
{
timerDisplayColor = Color.White;
}
UI.Timer(batch, Save.Instance.GetCurrentDisplayTime(), at, 0.0f, timerDisplayColor);
at.Y += UI.IconSize + 4;
}

Expand Down

0 comments on commit 78eb276

Please sign in to comment.