Skip to content
This repository has been archived by the owner on Jan 6, 2022. It is now read-only.

Commit

Permalink
Heat mod hud text
Browse files Browse the repository at this point in the history
  • Loading branch information
REHERC committed Dec 29, 2020
1 parent 8194f23 commit 5ba11cf
Show file tree
Hide file tree
Showing 7 changed files with 267 additions and 49 deletions.
5 changes: 4 additions & 1 deletion Distance.Heat/Distance.Heat.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,16 @@
<Compile Include="Enums\ActivationMode.cs" />
<Compile Include="Enums\DisplayMode.cs" />
<Compile Include="Enums\DisplayUnits.cs" />
<Compile Include="GameStateLogic.cs" />
<Compile Include="Entry.cs" />
<Compile Include="Flags.cs" />
<Compile Include="Harmony\Assembly-CSharp\SpeedrunTimerLogic\OnEnable.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Scripts\HeatTextLogic.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>SET MODNAME=Distance Heat
Expand Down
19 changes: 8 additions & 11 deletions Distance.Heat/Entry.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma warning disable IDE0052
using Centrifuge.Distance.Game;
using Centrifuge.Distance.Game;
using Centrifuge.Distance.GUI.Controls;
using Centrifuge.Distance.GUI.Data;
using Distance.Heat.Enums;
Expand All @@ -25,19 +24,17 @@ public class Mod : MonoBehaviour

public ConfigurationLogic Config { get; private set; }

private GameStateLogic GameState_ { get; set; }

public void Initialize(IManager manager)
{
DontDestroyOnLoad(this);

Instance = this;
Manager = manager;

Flags.SubscribeEvents();

Logger = LogManager.GetForCurrentAssembly();
Config = gameObject.AddComponent<ConfigurationLogic>();

GameState_ = gameObject.AddComponent<GameStateLogic>();

RuntimePatcher.AutoPatch();

CreateSettingsMenu();
Expand Down Expand Up @@ -73,17 +70,17 @@ private void CreateSettingsMenu()

private Dictionary<string, T> MapEnumToListBox<T>() where T : Enum
{
var ret = new Dictionary<string, T>();
var result = new Dictionary<string, T>();

var keys = Enum.GetNames(typeof(T));
var values = (T[])Enum.GetValues(typeof(T));

for (var i = 0; i < keys.Length; i++)
for (var index = 0; index < keys.Length; index++)
{
ret.Add(SplitCamelCase(keys[i]), values[i]);
result.Add(SplitCamelCase(keys[index]), values[index]);
}

return ret;
return result;
}

public string SplitCamelCase(string str)
Expand Down
92 changes: 92 additions & 0 deletions Distance.Heat/Flags.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using Events.Game;
using Events.GameMode;
using Events.Level;
using Events.LevelEditor;
using Events.RaceEnd;
using System.Linq;

namespace Distance.Heat
{
public static class Flags
{
public static bool IsModeStarted { get; private set; }

public static bool IsPaused { get; private set; }

public static bool IsMenuOpen => G.Sys.MenuPanelManager_?.panelStack_?.Count > 0;

public static bool IsLoading => G.Sys.GameManager_.BlackFade_.currentState_ != BlackFadeLogic.FadeState.Idle && !G.Sys.GameManager_.IsLevelLoaded_;

public static bool IsReplayMode => G.Sys.ReplayManager_.IsReplayMode_;

public static GameModeID[] NonPlayModeIDS { get; private set; }

public static bool CurrentModeIsPlayable => !NonPlayModeIDS.Contains(G.Sys.GameManager_.ModeID_);

static Flags()
{
NonPlayModeIDS = new GameModeID[]
{
GameModeID.Count,
GameModeID.MainMenu,
GameModeID.None,
};
}

public static bool CanDisplayHudElements
{
get
{
return IsModeStarted && !IsLoading && CurrentModeIsPlayable && !IsPaused && !IsMenuOpen && !IsReplayMode;
}
}

internal static void SubscribeEvents()
{
PauseToggled.Subscribe(OnGamePauseToggled);
ModeInitialized.Subscribe(OnGameModeInitialized);
ModeStarted.Subscribe(OnGameModeModeStarted);
LocalCarHitFinish.Subscribe(OnRaceEndLocalCarHitFinish);
EnterPlayMode.Subscribe(OnLevelEditorEnterPlayMode);
EnterEditorMode.Subscribe(OnLevelEditorEnterEditorMode);
UninitializeOptimizations.Subscribe(OnLevelUninitializeOptimizations);
}

private static void OnLevelUninitializeOptimizations(UninitializeOptimizations.Data data)
{
IsModeStarted = false;
}

private static void OnGameModeModeStarted(ModeStarted.Data data)
{
IsPaused = false;
IsModeStarted = true;
}

private static void OnLevelEditorEnterEditorMode(EnterEditorMode.Data data)
{
IsPaused = false;
IsModeStarted = false;
}

private static void OnLevelEditorEnterPlayMode(EnterPlayMode.Data data)
{
IsModeStarted = true;
}

private static void OnRaceEndLocalCarHitFinish(LocalCarHitFinish.Data data)
{
IsModeStarted = false;
}

private static void OnGameModeInitialized(ModeInitialized.Data data)
{
IsPaused = false;
}

private static void OnGamePauseToggled(PauseToggled.Data data)
{
IsPaused = data.paused_;
}
}
}
36 changes: 0 additions & 36 deletions Distance.Heat/GameStateLogic.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Distance.Heat.Scripts;
using HarmonyLib;
using System;

namespace Distance.Heat.Harmony
{
[HarmonyPatch(typeof(SpeedrunTimerLogic), "OnEnable")]
internal static class SpeedrunTimerLogic__OnEnable
{
[HarmonyPostfix]
internal static void Postfix(SpeedrunTimerLogic __instance)
{
try
{
HeatTextLogic.Create(__instance.gameObject);
}
catch (Exception e)
{
Mod.Instance.Logger.Exception(e);
}
}
}
}
140 changes: 140 additions & 0 deletions Distance.Heat/Scripts/HeatTextLogic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
using Centrifuge.Distance;
using Centrifuge.Distance.Game;
using System.Collections;
using UnityEngine;

namespace Distance.Heat.Scripts
{
public class HeatTextLogic : MonoBehaviour
{
internal const float MaximumOpacity = 0.7f;

internal static HeatTextLogic Instance { get; set; } = null;
internal static bool creatingInstance = false;

internal UILabel label;
internal UIWidget widget;
internal UIPanel panel;

internal static void Create(GameObject speedrunTimerLogic = null)
{
if (!Instance && !creatingInstance)
{
creatingInstance = true;
GameObject alphaVersionAnchorBlueprint = null;

if (speedrunTimerLogic)
{
alphaVersionAnchorBlueprint = speedrunTimerLogic.Parent();
}
else
{
alphaVersionAnchorBlueprint = GameObject.Find("Anchor : Speedrun Timer");
}

if (alphaVersionAnchorBlueprint)
{
GameObject centrifugeInfoAnchor = Instantiate(alphaVersionAnchorBlueprint, alphaVersionAnchorBlueprint.transform.parent);

centrifugeInfoAnchor.SetActive(true);
centrifugeInfoAnchor.name = "Anchor : Heat Display";

centrifugeInfoAnchor.ForEachChildObjectDepthFirstRecursive((obj) =>
{
obj.SetActive(true);
obj.RemoveComponents<SpeedrunTimerLogic>();
});

UILabel label = centrifugeInfoAnchor.GetComponentInChildren<UILabel>();

Instance = label.gameObject.AddComponent<HeatTextLogic>();
}

creatingInstance = false;
}
}

internal void Start()
{
GameObject anchorObject = gameObject.Parent();
GameObject panelObject = anchorObject.Parent();

label = GetComponent<UILabel>();
widget = anchorObject.GetComponent<UIWidget>();
panel = panelObject.GetComponent<UIPanel>();

widget.alpha = 0;

AdjustPosition();
}

internal void Update()
{
Mathf.Clamp(Vehicle.HeatLevel, 0, 1);

string percent = Mathf.RoundToInt(100 * Mathf.Clamp(Vehicle.HeatLevel, 0, 1)).ToString();
while (percent.Length < 3)
{
percent = $"0{percent}";
}


label.text = $"{percent} %";

Visible = CanDisplay;
}

private Coroutine _transitionCoroutine = null;

private bool _visible = true;
internal bool Visible
{
get => _visible;
set
{
if (value != _visible)
{
if (_transitionCoroutine != null)
{
StopCoroutine(_transitionCoroutine);
}

_transitionCoroutine = StartCoroutine(Transition(value));
}

_visible = value;
}
}

internal bool CanDisplay => Flags.CanDisplayHudElements;

internal void AdjustPosition()
{
label.SetAnchor(panel.gameObject, 21, 19, -19, -17);
label.pivot = UIWidget.Pivot.TopLeft;
}

internal IEnumerator Transition(bool visible, float duration = 0.2f)
{
float target = visible ? MaximumOpacity : 0.0f;
float current = widget.alpha;

for (float time = 0.0f; time < duration; time += Timex.deltaTime_)
{
if (!Centrifuge.Distance.Game.Options.General.MenuAnimations)
{
break;
}

float value = MathUtil.Map(time, 0, duration, current, target);
widget.alpha = value;

yield return null;
}

widget.alpha = target;

yield break;
}
}
}
1 change: 0 additions & 1 deletion Distance.NitronicHUD/Flags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ private static void OnRaceEndLocalCarHitFinish(LocalCarHitFinish.Data data)
private static void OnGameModeInitialized(ModeInitialized.Data data)
{
IsPaused = false;
//IsModeStarted = true;
}

private static void OnGamePauseToggled(PauseToggled.Data data)
Expand Down

0 comments on commit 5ba11cf

Please sign in to comment.