Skip to content

Commit

Permalink
Fix issue with hosting server on new save
Browse files Browse the repository at this point in the history
  • Loading branch information
Extremelyd1 committed Aug 19, 2024
1 parent 92cdc2d commit 0de18d6
Showing 1 changed file with 33 additions and 21 deletions.
54 changes: 33 additions & 21 deletions HKMP/Game/Client/ClientManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,31 @@ internal class ClientManager : IClientManager {
/// </summary>
private bool _autoConnect;

/// <summary>
/// The username that was last used to connect with.
/// </summary>
private string _username;

/// <summary>
/// Keeps track of the last updated location of the local player object.
/// </summary>
private Vector3 _lastPosition;

/// <summary>
/// Keeps track of the last updated scale of the local player object.
/// </summary>
private Vector3 _lastScale;

/// <summary>
/// The last scene that the player was in, to check whether we should be sending that we left a certain scene.
/// </summary>
private string _lastScene;

/// <summary>
/// Whether we have already determined whether we are scene host or not for the entity system.
/// </summary>
private bool _sceneHostDetermined;

#endregion

#region IClientManager properties
Expand Down Expand Up @@ -140,26 +165,6 @@ public string Username {

#endregion

/// <summary>
/// The username that was last used to connect with.
/// </summary>
private string _username;

/// <summary>
/// Keeps track of the last updated location of the local player object.
/// </summary>
private Vector3 _lastPosition;

/// <summary>
/// Keeps track of the last updated scale of the local player object.
/// </summary>
private Vector3 _lastScale;

/// <summary>
/// Whether we have already determined whether we are scene host or not for the entity system.
/// </summary>
private bool _sceneHostDetermined;

public ClientManager(
NetClient netClient,
ServerManager serverManager,
Expand Down Expand Up @@ -857,6 +862,7 @@ private void OnServerSettingsUpdated(ServerSettingsUpdate update) {
/// <param name="newScene">The new scene instance.</param>
private void OnSceneChange(Scene oldScene, Scene newScene) {
Logger.Info($"Scene changed from {oldScene.name} to {newScene.name}");
Logger.Debug($" Current scene: {UnityEngine.SceneManagement.SceneManager.GetActiveScene().name}");

// Always recycle existing players, because we changed scenes
_playerManager.RecycleAllPlayers();
Expand All @@ -875,7 +881,7 @@ private void OnSceneChange(Scene oldScene, Scene newScene) {
_sceneHostDetermined = false;

// If the old scene is a gameplay scene, we need to notify the server that we left
if (!SceneUtil.IsNonGameplayScene(oldScene.name)) {
if (!SceneUtil.IsNonGameplayScene(oldScene.name) && oldScene.name == _lastScene) {
_netClient.UpdateManager.SetLeftScene();
}
}
Expand Down Expand Up @@ -925,6 +931,12 @@ private void OnPlayerUpdate(On.HeroController.orig_Update orig, HeroController s
/// Callback method for the local player enters a scene. Used to network to the server that a scene is entered.
/// </summary>
private void OnEnterScene() {
var sceneName = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;

Logger.Debug($"OnEnterScene, scene: {sceneName}");

_lastScene = sceneName;

// Set some default values for the packet variables in case we don't have a HeroController instance
// This might happen when we are in a non-gameplay scene without the knight
var position = Vector2.Zero;
Expand Down

0 comments on commit 0de18d6

Please sign in to comment.