Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions HKMP/Api/Client/IClientManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,14 @@ public interface IClientManager {
/// Event that is called when another player leaves the local scene.
/// </summary>
event Action<IClientPlayer> PlayerLeaveSceneEvent;

/// <summary>
/// Event that is called when HKMP modifies the game's time scale.
Comment thread
dplochcoder marked this conversation as resolved.
/// </summary>
delegate void SetTimeScale(float timeScale);
Comment thread
dplochcoder marked this conversation as resolved.

/// <summary>
/// Event that is called when HKMP modifies the game's time scale.
/// </summary>
event SetTimeScale OnSetTimeScale;
Comment thread
dplochcoder marked this conversation as resolved.
}
16 changes: 13 additions & 3 deletions HKMP/Game/Client/ClientManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ internal class ClientManager : IClientManager {
/// </summary>
private readonly EntityManager _entityManager;

/// <summary>
/// The pause manager instance.
/// </summary>
private readonly PauseManager _pauseManager;

/// <summary>
/// The client addon manager instance.
/// </summary>
Expand Down Expand Up @@ -128,6 +133,9 @@ public string Username {
/// <inheritdoc />
public event Action<IClientPlayer> PlayerLeaveSceneEvent;

/// <inheritdoc />
public event IClientManager.SetTimeScale OnSetTimeScale;
Comment thread
dplochcoder marked this conversation as resolved.

/// <inheritdoc />
public Team Team => _playerManager.LocalPlayerTeam;

Expand Down Expand Up @@ -180,7 +188,9 @@ ModSettings modSettings

_entityManager = new EntityManager(netClient);

new PauseManager(netClient).RegisterHooks();
_pauseManager = new PauseManager(netClient, timeScale => OnSetTimeScale?.Invoke(timeScale));
_pauseManager.RegisterHooks();

new FsmPatcher().RegisterHooks();

_commandManager = new ClientCommandManager();
Expand Down Expand Up @@ -327,7 +337,7 @@ private void InternalDisconnect() {

// Check whether the game is in the pause menu and reset timescale to 0 in that case
if (UIManager.instance.uiState.Equals(UIState.PAUSED)) {
PauseManager.SetTimeScale(0);
_pauseManager.SetTimeScale(0);
}

try {
Expand Down Expand Up @@ -485,7 +495,7 @@ private void OnClientConnect(LoginResponse loginResponse) {

// Since we are probably in the pause menu when we connect, set the timescale so the game
// is running while paused
PauseManager.SetTimeScale(1.0f);
_pauseManager.SetTimeScale(1.0f);

UiManager.InternalChatBox.AddMessage("You are connected to the server");
}
Expand Down
15 changes: 12 additions & 3 deletions HKMP/Game/Client/PauseManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections;
using System.Reflection;
using GlobalEnums;
using Hkmp.Api.Client;
using Hkmp.Networking.Client;
using Modding;
using UnityEngine;
Expand All @@ -15,9 +16,15 @@ internal class PauseManager {
/// The net client instance.
/// </summary>
private readonly NetClient _netClient;

/// <summary>
/// Hook for time scale changes.
/// </summary>
private readonly IClientManager.SetTimeScale _onSetTimeScale;

public PauseManager(NetClient netClient) {
public PauseManager(NetClient netClient, IClientManager.SetTimeScale onSetTimeScale) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes more sense to expose the event for the time scale changing in this class instead. Now we are just juggling a delegate around just to be able to call the event in the ClientManager class.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a more invasive change, since PauseManager is an internal class and is not currently exposed at all through the public API. I can make this change if you prefer but I thought it was cleaner this way to stick to the existing API surface.

_netClient = netClient;
_onSetTimeScale = onSetTimeScale;
}

/// <summary>
Expand Down Expand Up @@ -194,7 +201,9 @@ private static void ImmediateUnpauseIfPaused() {
/// Sets the time scale similarly to the method GameManager#SetTimeScale.
/// </summary>
/// <param name="timeScale">The new time scale.</param>
public static void SetTimeScale(float timeScale) {
TimeController.GenericTimeScale = timeScale > 0.00999999977648258 ? timeScale : 0.0f;
public void SetTimeScale(float timeScale) {
timeScale = timeScale > 0.00999999977648258 ? timeScale : 0.0f;
TimeController.GenericTimeScale = timeScale;
_onSetTimeScale(timeScale);
}
}