Skip to content

Commit

Permalink
timer for ILs
Browse files Browse the repository at this point in the history
  • Loading branch information
jakzo committed Apr 22, 2024
1 parent bf487c1 commit aa94727
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions projects/Bonelab/SpeedrunTimer/src/Mod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public override void OnInitializeMelon() {
PrefCategory = MelonPreferences.CreateCategory(PREF_CATEGORY_ID);
SplitsTimer.OnInitialize();
SaveDeleteImprovements.OnInitialize();
TimeTrialDisplayFix.OnInitialize();

LevelHooks.OnLoad += _timer.OnLoadingScreen;
LevelHooks.OnLevelStart += _timer.OnLevelStart;
Expand Down
76 changes: 76 additions & 0 deletions projects/Bonelab/SpeedrunTimer/src/TimeTrialDisplayFix.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using MelonLoader;
using HarmonyLib;
using UnityEngine;
using TMPro;
using SLZ.Marrow.Warehouse;
using SLZ.Bonelab;
using Sst.Utilities;
using System.Collections.Generic;

namespace Sst.SpeedrunTimer {
class TimeTrialDisplayFix : MonoBehaviour {
private static HashSet<string> TIME_TRIAL_LEVELS = new HashSet<string>() {
Utilities.Levels.Barcodes.STREET_PUNCHER,
Utilities.Levels.Barcodes.SPRINT_BRIDGE,
};

private static TimeTrial_GameController _controller;
private static BoneLeaderManager _leaderboardManager;
private static TextMeshPro _tmpLabel;

public static void OnInitialize() { LevelHooks.OnLevelStart += OnLevelStart; }

private static void OnLevelStart(LevelCrate level) {
Dbg.Log($"TimeTrialDisplayFix OnLevelStart");

if (!TIME_TRIAL_LEVELS.Contains(level.Barcode.ID))
return;

Dbg.Log($"TimeTrialDisplayFix TIME_TRIAL_LEVELS");

_controller = GameObject.FindObjectOfType<TimeTrial_GameController>();
if (_controller == null)
return;

_leaderboardManager = GameObject.FindObjectOfType<BoneLeaderManager>();
if (_leaderboardManager == null)
return;

Dbg.Log($"TimeTrialDisplayFix DisplayTimeOnFinish");

_controller.onSessionEnd.AddListener(new Action(OnSessionEnd));
}

public static void OnSessionEnd() {
if (_controller == null || _leaderboardManager == null)
return;

Dbg.Log($"TimeTrialDisplayFix OnSessionEnd: {_controller.tsTimerString}");

var leaderboardTitle = _leaderboardManager.text_TitleBoard;
var leaderboardCanvas = leaderboardTitle.transform.parent;

var label = new GameObject("TimeTrialDisplayFix_Label");
label.transform.SetParent(leaderboardCanvas, false);
label.transform.localPosition = new Vector3(0f, 0f, 3f);

_tmpLabel = label.AddComponent<TextMeshPro>();
_tmpLabel.alignment = TextAlignmentOptions.Center;
_tmpLabel.font = leaderboardTitle.font;
_tmpLabel.fontSize = 4f;
_tmpLabel.rectTransform.sizeDelta = new Vector2(10f, 1f);
}

[HarmonyPatch(typeof(BaseGameController),
nameof(BaseGameController.UpdateTimeDisplay))]
class BaseGameController_UpdateTimeDisplay_Patch {
[HarmonyPostfix()]
internal static void Postfix(BaseGameController __instance) {
if (__instance == _controller && _tmpLabel != null) {
_tmpLabel.SetText($"Time: {_controller.tsTimerString}");
}
}
}
}
}

0 comments on commit aa94727

Please sign in to comment.