-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Features.CustomStats #130
base: dev
Are you sure you want to change the base?
Features.CustomStats #130
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="CustomHumeShieldStat.cs" company="Exiled Team"> | ||
// Copyright (c) Exiled Team. All rights reserved. | ||
// Licensed under the CC BY-SA 3.0 license. | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
namespace Exiled.API.Features.CustomStats | ||
{ | ||
using Mirror; | ||
using PlayerRoles.PlayableScps.HumeShield; | ||
using PlayerStatsSystem; | ||
using UnityEngine; | ||
using Utils.Networking; | ||
|
||
/// <summary> | ||
/// A custom version of <see cref="HumeShieldStat"/> which allows the player's max amount of HumeShield to be changed. | ||
/// </summary> | ||
public class CustomHumeShieldStat : HumeShieldStat | ||
{ | ||
/// <inheritdoc /> | ||
public override float MaxValue => CustomMaxValue == -1 ? base.MaxValue : CustomMaxValue; | ||
|
||
/// <summary> | ||
/// Gets or sets the multiplier for gaining HumeShield. | ||
/// </summary> | ||
public float ShieldRegenerationMultiplier { get; set; } = 1; | ||
|
||
/// <summary> | ||
/// Gets or sets the maximum amount of HumeShield the player can have. | ||
/// </summary> | ||
public float CustomMaxValue { get; set; } = -1; | ||
|
||
private float ShieldRegeneration => TryGetHsModule(out HumeShieldModuleBase controller) ? controller.HsRegeneration * ShieldRegenerationMultiplier : 0; | ||
|
||
/// <inheritdoc/> | ||
public override void Update() | ||
{ | ||
if (MaxValue == -1) | ||
{ | ||
base.Update(); | ||
return; | ||
} | ||
|
||
if (!NetworkServer.active) | ||
return; | ||
|
||
if (_valueDirty) | ||
{ | ||
new SyncedStatMessages.StatMessage() | ||
{ | ||
Stat = this, | ||
SyncedValue = CurValue, | ||
}.SendToHubsConditionally(CanReceive); | ||
_lastSent = CurValue; | ||
_valueDirty = false; | ||
} | ||
|
||
if (ShieldRegeneration == 0) | ||
return; | ||
|
||
float delta = ShieldRegeneration * Time.deltaTime; | ||
|
||
if (delta > 0) | ||
{ | ||
if (CurValue >= MaxValue) | ||
return; | ||
|
||
CurValue = Mathf.MoveTowards(CurValue, MaxValue, delta); | ||
return; | ||
} | ||
|
||
if (CurValue <= 0) | ||
return; | ||
|
||
CurValue += delta; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,56 @@ | ||||||
// ----------------------------------------------------------------------- | ||||||
// <copyright file="CustomStaminaStat.cs" company="Exiled Team"> | ||||||
// Copyright (c) Exiled Team. All rights reserved. | ||||||
// Licensed under the CC BY-SA 3.0 license. | ||||||
// </copyright> | ||||||
// ----------------------------------------------------------------------- | ||||||
|
||||||
namespace Exiled.API.Features.CustomStats | ||||||
{ | ||||||
using Mirror; | ||||||
using PlayerStatsSystem; | ||||||
using UnityEngine; | ||||||
|
||||||
/// <summary> | ||||||
/// A custom version of <see cref="StaminaStat"/> which allows the player's maximum amount of Stamina to be changed. | ||||||
/// </summary> | ||||||
public class CustomStaminaStat : StaminaStat | ||||||
{ | ||||||
/// <inheritdoc/> | ||||||
public override float MaxValue => CustomMaxValue == default ? base.MaxValue : CustomMaxValue; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
/// <summary> | ||||||
/// Gets or sets the maximum amount of stamina the player will have. | ||||||
/// </summary> | ||||||
public float CustomMaxValue { get; set; } | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
/// <summary> | ||||||
/// Clamps a float to fit the current stamina bar. | ||||||
/// </summary> | ||||||
/// <param name="value">The value to clamp.</param> | ||||||
/// <returns>The clamped num.</returns> | ||||||
public float Clamp(float value) => CustomMaxValue == default ? Mathf.Clamp01(value) : Mathf.Clamp(value, 0, MaxValue); | ||||||
|
||||||
/// <summary> | ||||||
/// Overiding NW Method to sync Player percentage of Stamina. | ||||||
/// </summary> | ||||||
/// <param name="writer">The writer.</param> | ||||||
public override void WriteValue(NetworkWriter writer) | ||||||
{ | ||||||
if (CustomMaxValue == default) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{ | ||||||
base.WriteValue(writer); | ||||||
return; | ||||||
} | ||||||
|
||||||
writer.WriteByte(ToByte(CurValue / CustomMaxValue)); | ||||||
} | ||||||
|
||||||
/// <summary> | ||||||
/// Overriding NW Method to sync Player percentage of Stamina. | ||||||
/// </summary> | ||||||
/// <param name="reader">The reader.</param> | ||||||
/// <returns>the float value sync to server.</returns> | ||||||
public override float ReadValue(NetworkReader reader) => CustomMaxValue == default ? base.ReadValue(reader) : CurValue * CustomMaxValue; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="FixStaminaBar.cs" company="Exiled Team"> | ||
// Copyright (c) Exiled Team. All rights reserved. | ||
// Licensed under the CC BY-SA 3.0 license. | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
namespace Exiled.Events.Patches.Fixes | ||
{ | ||
using Exiled.API.Features.CustomStats; | ||
using HarmonyLib; | ||
using Mirror; | ||
using PlayerRoles.FirstPersonControl; | ||
using UnityEngine; | ||
|
||
/// <summary> | ||
/// Fix for <see cref="CustomStaminaStat"/>. | ||
/// </summary> | ||
[HarmonyPatch(typeof(FpcStateProcessor), nameof(FpcStateProcessor.UpdateMovementState))] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am pretty sure there a way to not having to patch this event it's also in the wrong file |
||
internal static class FixStaminaBar | ||
{ | ||
#pragma warning disable SA1313 | ||
private static bool Prefix(FpcStateProcessor __instance, PlayerMovementState state, PlayerMovementState __result) | ||
#pragma warning restore SA1313 | ||
{ | ||
if (__instance._stat is not CustomStaminaStat stat) | ||
return true; | ||
|
||
float height = __instance._mod.CharacterControllerSettings.Height; | ||
float num1 = height * __instance._mod.CrouchHeightRatio; | ||
|
||
if (__instance.UpdateCrouching(state == PlayerMovementState.Crouching, num1, height) || __instance._firstUpdate) | ||
{ | ||
__instance._firstUpdate = false; | ||
float num2 = Mathf.Lerp(0.0f, (float)((height - num1) / 2.0), __instance.CrouchPercent); | ||
float num3 = Mathf.Lerp(height, num1, __instance.CrouchPercent); | ||
float radius = __instance._mod.CharController.radius; | ||
__instance._mod.CharController.height = num3; | ||
__instance._mod.CharController.center = Vector3.down * num2; | ||
__instance._camPivot.localPosition = Vector3.up * (float)((num3 / 2.0) - num2 - radius + 0.08799999952316284); | ||
} | ||
|
||
if (!NetworkServer.active || __instance._useRate == 0.0) | ||
{ | ||
__result = state; | ||
return false; | ||
} | ||
|
||
if (state == PlayerMovementState.Sprinting) | ||
{ | ||
if (stat.CurValue > 0.0 && !__instance.SprintingDisabled) | ||
{ | ||
stat.CurValue = stat.Clamp(stat.CurValue - (Time.deltaTime * __instance.ServerUseRate)); | ||
__instance._regenStopwatch.Restart(); | ||
__result = PlayerMovementState.Sprinting; | ||
return false; | ||
} | ||
|
||
state = PlayerMovementState.Walking; | ||
} | ||
|
||
if (stat.CurValue >= stat.MaxValue) | ||
{ | ||
__result = state; | ||
return false; | ||
} | ||
|
||
stat.CurValue = stat.Clamp(__instance._stat.CurValue + (__instance.ServerRegenRate * Time.deltaTime)); | ||
__result = state; | ||
return false; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool be interesting to override this value