mirror of
https://github.com/NotAKidoS/NAK_CVR_Mods.git
synced 2025-09-02 06:19:22 +00:00
i dont rememebr
This commit is contained in:
parent
374ab6c11e
commit
86828a94e2
48 changed files with 1637 additions and 841 deletions
|
@ -1,4 +1,5 @@
|
|||
using ABI_RC.Core.IO;
|
||||
using System.Collections;
|
||||
using ABI_RC.Core.IO;
|
||||
using ABI_RC.Core.Player;
|
||||
using ABI_RC.Core.Player.AvatarTracking;
|
||||
using ABI_RC.Core.UI;
|
||||
|
@ -11,14 +12,29 @@ namespace NAK.AvatarScaleMod.AvatarScaling;
|
|||
|
||||
public class AvatarScaleManager : MonoBehaviour
|
||||
{
|
||||
// Universal Scaling Limits
|
||||
public const float MinHeight = 0.1f;
|
||||
public const float MaxHeight = 10f;
|
||||
|
||||
public static AvatarScaleManager Instance;
|
||||
public static AvatarScaleManager Instance { get; private set; }
|
||||
|
||||
private LocalScaler _localAvatarScaler;
|
||||
private Dictionary<string, NetworkScaler> _networkedScalers;
|
||||
|
||||
private Coroutine _heightUpdateCoroutine;
|
||||
private readonly YieldInstruction _heightUpdateYield = new WaitForEndOfFrame();
|
||||
|
||||
#region Universal Scaling Limits
|
||||
|
||||
// ReSharper disable MemberCanBePrivate.Global
|
||||
// To match AvatarScaleTool: https://github.com/NotAKidOnSteam/AvatarScaleTool/tree/main
|
||||
public const float DefaultMinHeight = 0.25f;
|
||||
public const float DefaultMaxHeight = 2.50f;
|
||||
// ReSharper restore MemberCanBePrivate.Global
|
||||
|
||||
// Universal Scaling Limits
|
||||
public static float MinHeight { get; private set; } = DefaultMinHeight;
|
||||
public static float MaxHeight { get; private set; } = DefaultMaxHeight;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Settings
|
||||
|
||||
private bool _settingUniversalScaling;
|
||||
public bool Setting_UniversalScaling
|
||||
|
@ -27,21 +43,25 @@ public class AvatarScaleManager : MonoBehaviour
|
|||
set
|
||||
{
|
||||
if (value != _settingUniversalScaling && value == false)
|
||||
ResetHeight();
|
||||
ResetTargetHeight();
|
||||
|
||||
_settingUniversalScaling = value;
|
||||
SetTargetHeight(_lastTargetHeight); // immediate height update
|
||||
}
|
||||
}
|
||||
|
||||
public bool Setting_PersistantHeight;
|
||||
public bool Setting_AnimationClipScalingOverride;
|
||||
public bool Setting_PersistentHeight;
|
||||
private float _lastTargetHeight = -1;
|
||||
|
||||
|
||||
#region Unity Methods
|
||||
#endregion
|
||||
|
||||
#region Unity Events
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance != null)
|
||||
if (Instance != null
|
||||
&& Instance != this)
|
||||
{
|
||||
DestroyImmediate(this);
|
||||
return;
|
||||
|
@ -53,22 +73,82 @@ public class AvatarScaleManager : MonoBehaviour
|
|||
|
||||
private void Start()
|
||||
{
|
||||
_localAvatarScaler = PlayerSetup.Instance.gameObject.AddComponent<LocalScaler>();
|
||||
_localAvatarScaler.Initialize();
|
||||
|
||||
_settingUniversalScaling = ModSettings.EntryUseUniversalScaling.Value;
|
||||
Setting_AnimationClipScalingOverride = ModSettings.EntryAnimationScalingOverride.Value;
|
||||
Setting_PersistentHeight = ModSettings.EntryPersistentHeight.Value;
|
||||
_lastTargetHeight = ModSettings.EntryPersistThroughRestart.Value
|
||||
? ModSettings.EntryHiddenAvatarHeight.Value : -1f; // -1f is default
|
||||
|
||||
// listen for events
|
||||
_localAvatarScaler.OnAnimatedHeightOverride += OnAnimationHeightOverride;
|
||||
|
||||
CVRGameEventSystem.Instance.OnConnected.AddListener(OnInstanceConnected);
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
if (_heightUpdateCoroutine != null) StopCoroutine(_heightUpdateCoroutine);
|
||||
_heightUpdateCoroutine = StartCoroutine(HeightUpdateCoroutine());
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
if (_heightUpdateCoroutine != null) StopCoroutine(_heightUpdateCoroutine);
|
||||
_heightUpdateCoroutine = null;
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
_heightUpdateCoroutine = null;
|
||||
CVRGameEventSystem.Instance.OnConnected.RemoveListener(OnInstanceConnected);
|
||||
|
||||
if (_localAvatarScaler != null) Destroy(_localAvatarScaler);
|
||||
_localAvatarScaler = null;
|
||||
|
||||
foreach (NetworkScaler scaler in _networkedScalers.Values) Destroy(scaler);
|
||||
_networkedScalers.Clear();
|
||||
|
||||
if (Instance == this)
|
||||
Instance = null;
|
||||
}
|
||||
|
||||
// only update the height per scaler once per frame, to prevent spam & jitter
|
||||
// this is to ensure that the height is also set at correct time during frame, no matter when it is called
|
||||
private IEnumerator HeightUpdateCoroutine()
|
||||
{
|
||||
while (enabled)
|
||||
{
|
||||
yield return _heightUpdateYield;
|
||||
|
||||
// update local scaler
|
||||
if (_localAvatarScaler != null && _localAvatarScaler.heightNeedsUpdate)
|
||||
{
|
||||
if (_localAvatarScaler.ApplyTargetHeight())
|
||||
AvatarScaleEvents.OnLocalAvatarHeightChanged.Invoke(_localAvatarScaler);
|
||||
}
|
||||
|
||||
// update networked scalers (probably a better way to do this)
|
||||
foreach (var netScaler in _networkedScalers)
|
||||
{
|
||||
if (!netScaler.Value.heightNeedsUpdate) continue;
|
||||
if (netScaler.Value.ApplyTargetHeight())
|
||||
AvatarScaleEvents.OnRemoteAvatarHeightChanged.Invoke(netScaler.Key, netScaler.Value);
|
||||
}
|
||||
}
|
||||
|
||||
// ReSharper disable once IteratorNeverReturns
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
#region Game Events
|
||||
|
||||
public void OnInstanceConnected(string instanceId)
|
||||
{
|
||||
// TODO: need to know if this causes issues when in a reconnection loop
|
||||
SchedulerSystem.AddJob(ModNetwork.RequestHeightSync, 2f, 1f, 1);
|
||||
}
|
||||
|
||||
|
@ -81,19 +161,13 @@ public class AvatarScaleManager : MonoBehaviour
|
|||
if (playerSetup._avatar == null)
|
||||
return;
|
||||
|
||||
if (_localAvatarScaler == null)
|
||||
{
|
||||
_localAvatarScaler = playerSetup.gameObject.AddComponent<LocalScaler>();
|
||||
_localAvatarScaler.Initialize();
|
||||
}
|
||||
|
||||
_localAvatarScaler.OnAvatarInstantiated(playerSetup._avatar, playerSetup._initialAvatarHeight,
|
||||
playerSetup.initialScale);
|
||||
|
||||
if (!_settingUniversalScaling)
|
||||
return;
|
||||
|
||||
SetHeight(Setting_PersistantHeight ? _lastTargetHeight : -1f);
|
||||
SetTargetHeight(_lastTargetHeight);
|
||||
}
|
||||
|
||||
public void OnAvatarDestroyed(PlayerSetup playerSetup)
|
||||
|
@ -102,35 +176,34 @@ public class AvatarScaleManager : MonoBehaviour
|
|||
_localAvatarScaler.OnAvatarDestroyed();
|
||||
}
|
||||
|
||||
public void SetHeight(float targetHeight)
|
||||
public void SetTargetHeight(float targetHeight)
|
||||
{
|
||||
_lastTargetHeight = targetHeight; // save for persistent height
|
||||
ModSettings.EntryHiddenAvatarHeight.Value = targetHeight; // save for restart
|
||||
|
||||
if (!_settingUniversalScaling)
|
||||
return;
|
||||
|
||||
if (_localAvatarScaler == null)
|
||||
return;
|
||||
|
||||
_lastTargetHeight = targetHeight;
|
||||
|
||||
_localAvatarScaler.SetTargetHeight(targetHeight);
|
||||
ModNetwork.SendNetworkHeight(targetHeight);
|
||||
|
||||
// immediately update play space scale
|
||||
PlayerSetup.Instance.CheckUpdateAvatarScaleToPlaySpaceRelation();
|
||||
_localAvatarScaler.SetTargetHeight(_lastTargetHeight);
|
||||
_localAvatarScaler.heightNeedsUpdate = true; // only local scaler forces update
|
||||
}
|
||||
|
||||
public void ResetHeight()
|
||||
|
||||
public void ResetTargetHeight()
|
||||
{
|
||||
if (_localAvatarScaler == null)
|
||||
return;
|
||||
|
||||
if (!_localAvatarScaler.IsHeightAdjustedFromInitial())
|
||||
if (!_localAvatarScaler.IsForcingHeight())
|
||||
return;
|
||||
|
||||
// TODO: doesnt work when hitting Reset on slider in BTK UI (is it on main thread?)
|
||||
CohtmlHud.Instance.ViewDropTextImmediate("(Local) AvatarScaleMod", "Avatar Scale Reset!",
|
||||
"Universal Scaling is now disabled.");
|
||||
|
||||
SetHeight(-1f);
|
||||
SetTargetHeight(-1f);
|
||||
}
|
||||
|
||||
public float GetHeight()
|
||||
|
@ -138,10 +211,21 @@ public class AvatarScaleManager : MonoBehaviour
|
|||
if (_localAvatarScaler == null)
|
||||
return PlayerAvatarPoint.defaultAvatarHeight;
|
||||
|
||||
if (!_localAvatarScaler.IsHeightAdjustedFromInitial())
|
||||
if (!_localAvatarScaler.IsForcingHeight())
|
||||
return PlayerSetup.Instance.GetAvatarHeight();
|
||||
|
||||
return _localAvatarScaler.GetHeight();
|
||||
return _localAvatarScaler.GetTargetHeight();
|
||||
}
|
||||
|
||||
public float GetAnimationClipHeight()
|
||||
{
|
||||
if (_localAvatarScaler == null)
|
||||
return PlayerAvatarPoint.defaultAvatarHeight;
|
||||
|
||||
if (!_localAvatarScaler.IsForcingHeight())
|
||||
return PlayerSetup.Instance.GetAvatarHeight();
|
||||
|
||||
return _localAvatarScaler.GetAnimatedHeight();
|
||||
}
|
||||
|
||||
public float GetHeightForNetwork()
|
||||
|
@ -152,10 +236,10 @@ public class AvatarScaleManager : MonoBehaviour
|
|||
if (_localAvatarScaler == null)
|
||||
return -1f;
|
||||
|
||||
if (!_localAvatarScaler.IsHeightAdjustedFromInitial())
|
||||
if (!_localAvatarScaler.IsForcingHeight())
|
||||
return -1f;
|
||||
|
||||
return _localAvatarScaler.GetHeight();
|
||||
return _localAvatarScaler.GetTargetHeight();
|
||||
}
|
||||
|
||||
public float GetInitialHeight()
|
||||
|
@ -168,17 +252,23 @@ public class AvatarScaleManager : MonoBehaviour
|
|||
|
||||
public bool IsHeightAdjustedFromInitial()
|
||||
{
|
||||
return _localAvatarScaler != null && _localAvatarScaler.IsHeightAdjustedFromInitial();
|
||||
return _localAvatarScaler != null && _localAvatarScaler.IsForcingHeight();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Network Methods
|
||||
|
||||
public bool DoesNetworkHeightScalerExist(string playerId)
|
||||
=> _networkedScalers.ContainsKey(playerId);
|
||||
|
||||
public int GetNetworkHeightScalerCount()
|
||||
=> _networkedScalers.Count;
|
||||
|
||||
public float GetNetworkHeight(string playerId)
|
||||
{
|
||||
if (_networkedScalers.TryGetValue(playerId, out NetworkScaler scaler))
|
||||
if (scaler.IsHeightAdjustedFromInitial()) return scaler.GetHeight();
|
||||
if (scaler.IsForcingHeight()) return scaler.GetTargetHeight();
|
||||
|
||||
//doesn't have mod or has no custom height, get from player avatar directly
|
||||
CVRPlayerEntity playerEntity = CVRPlayerManager.Instance.NetworkPlayers.Find((players) => players.Uuid == playerId);
|
||||
|
@ -266,4 +356,40 @@ public class AvatarScaleManager : MonoBehaviour
|
|||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Manager Methods
|
||||
|
||||
// sometimes fun to play with via UE
|
||||
public void SetUniversalScalingLimit(float min, float max)
|
||||
{
|
||||
const float HardCodedMinLimit = 0.01f;
|
||||
const float HardCodedMaxLimit = 100f;
|
||||
|
||||
MinHeight = Mathf.Clamp(min, HardCodedMinLimit, HardCodedMaxLimit);
|
||||
MaxHeight = Mathf.Clamp(max, HardCodedMinLimit, HardCodedMaxLimit);
|
||||
|
||||
AvatarScaleMod.Logger.Msg($"Universal Scaling Limits changed: {min} - {max}");
|
||||
AvatarScaleMod.Logger.Warning("This will not network to other users unless they also have the same limits set!");
|
||||
}
|
||||
|
||||
public void ResetUniversalScalingLimit()
|
||||
{
|
||||
MinHeight = DefaultMinHeight;
|
||||
MaxHeight = DefaultMaxHeight;
|
||||
|
||||
AvatarScaleMod.Logger.Msg("Universal Scaling Limits reset to default!");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Event Listeners
|
||||
|
||||
private static void OnAnimationHeightOverride(BaseScaler scaler)
|
||||
{
|
||||
AvatarScaleMod.Logger.Msg("AnimationClip-based avatar scaling detected. Disabling Universal Scaling.");
|
||||
CohtmlHud.Instance.ViewDropTextImmediate("(Local) AvatarScaleMod", "Avatar Scale Changed!",
|
||||
"Universal Scaling is now disabled in favor of built-in avatar scaling.");
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue