Skip to content

Commit

Permalink
Improve player specific saves
Browse files Browse the repository at this point in the history
  • Loading branch information
Extremelyd1 committed Jul 8, 2024
1 parent a7c2376 commit 44799e2
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 132 deletions.
99 changes: 0 additions & 99 deletions HKMP/Game/Client/Save/SaveManager.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using GlobalEnums;
using Hkmp.Collection;
using Hkmp.Game.Client.Entity;
using Hkmp.Networking.Client;
Expand All @@ -21,11 +19,6 @@ namespace Hkmp.Game.Client.Save;
/// Class that manages save data synchronisation.
/// </summary>
internal class SaveManager {
/// <summary>
/// The index of the save data entry for the warp.
/// </summary>
private const ushort SaveWarpIndex = ushort.MaxValue;

/// <summary>
/// The save data instance that contains mappings for what to sync and their indices.
/// </summary>
Expand Down Expand Up @@ -823,20 +816,6 @@ private void UpdateSaveWithData(ushort index, byte[] encodedValue) {
});
}

// TODO: refactor this, remove probably
if (index == SaveWarpIndex) {
// Specific handling of warp bench data
var respawnScene = DecodeString(encodedValue, 0);
var respawnMarkerName = DecodeString(encodedValue, 2);
var mapZone = (MapZone) encodedValue[4];

pd.respawnScene = respawnScene;
pd.respawnMarkerName = respawnMarkerName;
pd.mapZone = mapZone;

MonoBehaviourUtil.Instance.StartCoroutine(WarpToBench());
}

// Decode a string from the given byte array and start index in that array using the EncodeUtil
string DecodeString(byte[] encoded, int startIndex) {
var sceneIndex = BitConverter.ToUInt16(encoded, startIndex);
Expand Down Expand Up @@ -937,14 +916,6 @@ Func<TCollection, object> valueFunc
intData => intData.value
);

// Specific handling of last bench data
var encodedBenchData = new List<byte>();
encodedBenchData.AddRange(EncodeValue(pd.respawnScene));
encodedBenchData.AddRange(EncodeValue(pd.respawnMarkerName));
encodedBenchData.Add((byte) pd.mapZone);

saveData.Add(SaveWarpIndex, encodedBenchData.ToArray());

return saveData;
}

Expand All @@ -963,74 +934,4 @@ private static int GetStringListHashCode(List<string> list) {
.Select(item => item.GetHashCode())
.Aggregate((total, nextCode) => total ^ nextCode);
}

private static IEnumerator WarpToBench() {
var gm = global::GameManager.instance;

UIManager.instance.UIClosePauseMenu();

// Collection of various redundant attempts to fix the infamous soul orb bug
HeroController.instance.TakeMPQuick(PlayerData.instance.MPCharge); // actually broadcasts the event
HeroController.instance.SetMPCharge(0);
PlayerData.instance.MPReserve = 0;
HeroController.instance.ClearMP(); // useless
PlayMakerFSM.BroadcastEvent("MP DRAIN"); // This is the main fsm path for removing soul from the orb
PlayMakerFSM.BroadcastEvent("MP LOSE"); // This is an alternate path (used for bindings and other things) that actually plays an animation?
PlayMakerFSM.BroadcastEvent("MP RESERVE DOWN");

// Set some stuff which would normally be set by LoadSave
HeroController.instance.AffectedByGravity(false);
HeroController.instance.transitionState = HeroTransitionState.EXITING_SCENE;
if (HeroController.SilentInstance != null) {
if (HeroController.instance.cState.onConveyor || HeroController.instance.cState.onConveyorV ||
HeroController.instance.cState.inConveyorZone) {
HeroController.instance.GetComponent<ConveyorMovementHero>()?.StopConveyorMove();
HeroController.instance.cState.inConveyorZone = false;
HeroController.instance.cState.onConveyor = false;
HeroController.instance.cState.onConveyorV = false;
}

HeroController.instance.cState.nearBench = false;
}

gm.cameraCtrl.FadeOut(CameraFadeType.LEVEL_TRANSITION);

yield return new WaitForSecondsRealtime(0.5f);

// Actually respawn the character
gm.SetPlayerDataBool(nameof(PlayerData.atBench), false);
// Allow the player to have control if they warp to a non-bench while diving or cdashing
if (HeroController.SilentInstance != null) {
HeroController.instance.cState.superDashing = false;
HeroController.instance.cState.spellQuake = false;
}

gm.ReadyForRespawn(false);

yield return new WaitWhile(() => gm.IsInSceneTransition);

EventRegister.SendEvent("UPDATE BLUE HEALTH"); // checks if hp is adjusted for Joni's blessing

// Revert pause menu timescale
Time.timeScale = 1f;
gm.FadeSceneIn();

// We have to set the game non-paused because TogglePauseMenu sucks and UIClosePauseMenu doesn't do it for us.
gm.isPaused = false;

// Restore various things normally handled by exiting the pause menu. None of these are necessary afaik
GameCameras.instance.ResumeCameraShake();
if (HeroController.SilentInstance != null) {
HeroController.instance.UnPause();
}

MenuButtonList.ClearAllLastSelected();

//This allows the next pause to stop the game correctly
TimeController.GenericTimeScale = 1f;

// Restores audio to normal levels. Unfortunately, some warps pop atm when music changes over
gm.actorSnapshotUnpaused.TransitionTo(0f);
gm.ui.AudioGoToGameplay(.2f);
}
}
Loading

0 comments on commit 44799e2

Please sign in to comment.