From 6a0b78698d17efb81e5e7bf1780eda2eb1972563 Mon Sep 17 00:00:00 2001 From: shangfengh <3495281661@qq.com> Date: Wed, 11 Oct 2023 19:29:45 +0800 Subject: [PATCH] refactor: :art: add the DoubleInTheVariableRnge --- .../Character/Character.BuffManager.cs | 25 +- .../GameClass/GameObj/Character/Character.cs | 27 +- logic/Preparation/Interface/ICharacter.cs | 4 +- .../Utility/SafeValue/InTheRange.cs | 327 ++++++++++++++++-- 4 files changed, 321 insertions(+), 62 deletions(-) diff --git a/logic/GameClass/GameObj/Character/Character.BuffManager.cs b/logic/GameClass/GameObj/Character/Character.BuffManager.cs index 4979e42..cc6ffa8 100644 --- a/logic/GameClass/GameObj/Character/Character.BuffManager.cs +++ b/logic/GameClass/GameObj/Character/Character.BuffManager.cs @@ -177,19 +177,6 @@ namespace GameClass.GameObj return false; } - public bool TryDeleteInvisible() - { - if (HasInvisible) - { - lock (buffListLock[(int)BuffType.Invisible]) - { - buffList[(int)BuffType.Invisible].RemoveFirst(); - } - return true; - } - return false; - } - public void AddClairaudience(int shieldTime) => AddBuff(0, shieldTime, BuffType.Clairaudience, () => { }); public bool HasClairaudience @@ -215,6 +202,18 @@ namespace GameClass.GameObj } } } + public bool TryDeleteInvisible() + { + if (HasInvisible) + { + lock (buffListLock[(int)BuffType.Invisible]) + { + buffList[(int)BuffType.Invisible].RemoveFirst(); + } + return true; + } + return false; + } /// /// 清除所有buff diff --git a/logic/GameClass/GameObj/Character/Character.cs b/logic/GameClass/GameObj/Character/Character.cs index 94a1da0..2b6a9e1 100644 --- a/logic/GameClass/GameObj/Character/Character.cs +++ b/logic/GameClass/GameObj/Character/Character.cs @@ -110,32 +110,9 @@ namespace GameClass.GameObj public int SpeedOfOpenChest => speedOfOpenChest; #endregion #region 血量相关的基本属性及方法 - public LongWithVariableRange HP { get; } + public LongInTheVariableRange HP { get; } - private readonly object vampireLock = new(); - public object VampireLock => vampire; - - private double vampire = 0; // 回血率:0-1之间 - public double Vampire - { - get - { - lock (vampireLock) - return vampire; - } - set - { - lock (vampireLock) - { - if (value > 1) - vampire = 1; - else if (value < 0) - vampire = 0; - else - vampire = value; - } - } - } + public DoubleInTheVariableRange Vampire { get; } = new DoubleInTheVariableRange(0, 1); public double OriVampire { get; protected set; } private readonly object treatLock = new(); diff --git a/logic/Preparation/Interface/ICharacter.cs b/logic/Preparation/Interface/ICharacter.cs index 4211ada..a6e6a5d 100644 --- a/logic/Preparation/Interface/ICharacter.cs +++ b/logic/Preparation/Interface/ICharacter.cs @@ -5,10 +5,10 @@ namespace Preparation.Interface public interface ICharacter : IMoveable { public AtomicLong TeamID { get; } - public LongWithVariableRange HP { get; } + public LongInTheVariableRange HP { get; } public long Score { get; } public void AddScore(long add); - public double Vampire { get; } + public DoubleInTheVariableRange Vampire { get; } public PlayerStateType PlayerState { get; } public BulletType BulletOfPlayer { get; set; } public CharacterType CharacterType { get; } diff --git a/logic/Preparation/Utility/SafeValue/InTheRange.cs b/logic/Preparation/Utility/SafeValue/InTheRange.cs index 46acd4c..c2d9c9b 100644 --- a/logic/Preparation/Utility/SafeValue/InTheRange.cs +++ b/logic/Preparation/Utility/SafeValue/InTheRange.cs @@ -8,16 +8,16 @@ namespace Preparation.Utility /// /// 一个保证在[0,maxValue]的可变int,支持可变的maxValue(请确保大于0) /// - public class IntWithVariableRange + public class IntInTheVariableRange { private int v; private int maxV; private readonly object vLock = new(); - public IntWithVariableRange(int value, int maxValue) + public IntInTheVariableRange(int value, int maxValue) { if (maxValue < 0) { - Debugger.Output("Warning:Try to set IntWithVariableRange.maxValue to " + maxValue.ToString() + "."); + Debugger.Output("Warning:Try to set IntInTheVariableRange.maxValue to " + maxValue.ToString() + "."); maxValue = 0; } v = value < maxValue ? value : maxValue; @@ -26,16 +26,16 @@ namespace Preparation.Utility /// /// 默认使Value=maxValue /// - public IntWithVariableRange(int maxValue) + public IntInTheVariableRange(int maxValue) { if (maxValue < 0) { - Debugger.Output("Warning:Try to set IntWithVariableRange.maxValue to " + maxValue.ToString() + "."); + Debugger.Output("Warning:Try to set IntInTheVariableRange.maxValue to " + maxValue.ToString() + "."); maxValue = 0; } v = this.maxV = maxValue; } - public IntWithVariableRange() + public IntInTheVariableRange() { v = this.maxV = int.MaxValue; } @@ -48,7 +48,7 @@ namespace Preparation.Utility } } public int GetValue() { lock (vLock) return v; } - public static implicit operator int(IntWithVariableRange aint) => aint.GetValue(); + public static implicit operator int(IntInTheVariableRange aint) => aint.GetValue(); public int GetMaxV() { lock (vLock) return maxV; } /// @@ -153,8 +153,8 @@ namespace Preparation.Utility } lock (vLock) { - v *= mulV; - if (v > maxV) v = maxV; + if (v > maxV / mulV) v = maxV; //避免溢出 + else v *= mulV; } } /// @@ -164,8 +164,8 @@ namespace Preparation.Utility { lock (vLock) { - v *= mulPositiveV; - if (v > maxV) v = maxV; + if (v > maxV / mulPositiveV) v = maxV; //避免溢出 + else v *= mulPositiveV; } } /// 返回实际改变量 @@ -211,21 +211,46 @@ namespace Preparation.Utility return -1; } } + + public bool Set0IfNotMax() + { + lock (vLock) + { + if (v < maxV) + { + v = 0; + return true; + } + } + return false; + } + public bool Set0IfMax() + { + lock (vLock) + { + if (v == maxV) + { + v = 0; + return true; + } + } + return false; + } } /// /// 一个保证在[0,maxValue]的可变long,支持可变的maxValue(请确保大于0) /// - public class LongWithVariableRange + public class LongInTheVariableRange { private long v; private long maxV; private readonly object vLock = new(); - public LongWithVariableRange(long value, long maxValue) + public LongInTheVariableRange(long value, long maxValue) { if (maxValue < 0) { - Debugger.Output("Warning:Try to set SafaValues.LongWithVariableRange.maxValue to " + maxValue.ToString() + "."); + Debugger.Output("Warning:Try to set SafaValues.LongInTheVariableRange.maxValue to " + maxValue.ToString() + "."); maxValue = 0; } v = value < maxValue ? value : maxValue; @@ -234,16 +259,16 @@ namespace Preparation.Utility /// /// 默认使Value=maxValue /// - public LongWithVariableRange(long maxValue) + public LongInTheVariableRange(long maxValue) { if (maxValue < 0) { - Debugger.Output("Warning:Try to set SafaValues.LongWithVariableRange.maxValue to " + maxValue.ToString() + "."); + Debugger.Output("Warning:Try to set SafaValues.LongInTheVariableRange.maxValue to " + maxValue.ToString() + "."); maxValue = 0; } v = this.maxV = maxValue; } - public LongWithVariableRange() + public LongInTheVariableRange() { v = this.maxV = long.MaxValue; } @@ -256,7 +281,7 @@ namespace Preparation.Utility } } public long GetValue() { lock (vLock) return v; } - public static implicit operator long(LongWithVariableRange aint) => aint.GetValue(); + public static implicit operator long(LongInTheVariableRange aint) => aint.GetValue(); public long GetMaxV() { lock (vLock) return maxV; } /// @@ -361,8 +386,8 @@ namespace Preparation.Utility } lock (vLock) { - v *= mulV; - if (v > maxV) v = maxV; + if (v > maxV / mulV) v = maxV; //避免溢出 + else v *= mulV; } } /// @@ -372,8 +397,8 @@ namespace Preparation.Utility { lock (vLock) { - v *= mulPositiveV; - if (v > maxV) v = maxV; + if (v > maxV / mulPositiveV) v = maxV; //避免溢出 + else v *= mulPositiveV; } } /// 返回实际改变量 @@ -419,5 +444,263 @@ namespace Preparation.Utility return -1; } } + + public bool Set0IfNotMax() + { + lock (vLock) + { + if (v < maxV) + { + v = 0; + return true; + } + } + return false; + } + public bool Set0IfMax() + { + lock (vLock) + { + if (v == maxV) + { + v = 0; + return true; + } + } + return false; + } + } + + /// + /// 一个保证在[0,maxValue]的可变double,支持可变的maxValue(请确保大于0) + /// + public class DoubleInTheVariableRange + { + private double v; + private double maxV; + private readonly object vLock = new(); + public DoubleInTheVariableRange(double value, double maxValue) + { + if (maxValue < 0) + { + Debugger.Output("Warning:Try to set DoubleInTheVariableRange.maxValue to " + maxValue.ToString() + "."); + maxValue = 0; + } + v = value < maxValue ? value : maxValue; + this.maxV = maxValue; + } + /// + /// 默认使Value=maxValue + /// + public DoubleInTheVariableRange(double maxValue) + { + if (maxValue < 0) + { + Debugger.Output("Warning:Try to set DoubleInTheVariableRange.maxValue to " + maxValue.ToString() + "."); + maxValue = 0; + } + v = this.maxV = maxValue; + } + public DoubleInTheVariableRange() + { + v = this.maxV = double.MaxValue; + } + + public override string ToString() + { + lock (vLock) + { + return "value:" + v.ToString() + " ,maxValue:" + maxV.ToString(); + } + } + public double GetValue() { lock (vLock) return v; } + public static implicit operator double(DoubleInTheVariableRange adouble) => adouble.GetValue(); + public double GetMaxV() { lock (vLock) return maxV; } + + /// + /// 若maxValue<=0则maxValue设为0并返回False + /// + public bool SetMaxV(double maxValue) + { + if (maxValue <= 0) + { + lock (vLock) + { + v = maxV = 0; + return false; + } + } + lock (vLock) + { + maxV = maxValue; + if (v > maxValue) v = maxValue; + } + return true; + } + /// + /// 应当保证该maxValue>=0 + /// + public void SetPositiveMaxV(double maxValue) + { + lock (vLock) + { + maxV = maxValue; + if (v > maxValue) v = maxValue; + } + } + /// + /// 如果当前值大于maxValue,则更新maxValue失败 + /// + public bool TrySetMaxV(double maxValue) + { + lock (vLock) + { + if (v > maxValue) return false; + maxV = maxValue; + return true; + } + } + + /// + /// 应当保证该value>=0 + /// + public double SetPositiveV(double value) + { + lock (vLock) + { + return v = (value > maxV) ? maxV : value; + } + } + public double SetV(double value) + { + if (value <= 0) + { + lock (vLock) + { + return v = 0; + } + } + lock (vLock) + { + return v = (value > maxV) ? maxV : value; + } + } + /// 返回实际改变量 + public double AddV(double addV) + { + lock (vLock) + { + double previousV = v; + v += addV; + if (v < 0) v = 0; + if (v > maxV) v = maxV; + return v - previousV; + } + } + /// + /// 应当保证增加值大于0 + /// + /// 返回实际改变量 + public double AddPositiveV(double addPositiveV) + { + lock (vLock) + { + addPositiveV = Math.Min(addPositiveV, maxV - v); + v += addPositiveV; + } + return addPositiveV; + } + public void MulV(double mulV) + { + if (mulV <= 0) + { + lock (vLock) v = 0; + return; + } + lock (vLock) + { + if (v > maxV / mulV) v = maxV; //避免溢出 + else v *= mulV; + } + } + /// + /// 应当保证乘数大于0 + /// + public void MulPositiveV(double mulPositiveV) + { + lock (vLock) + { + if (v > maxV / mulPositiveV) v = maxV; //避免溢出 + else v *= mulPositiveV; + } + } + /// 返回实际改变量 + public double SubV(double subV) + { + lock (vLock) + { + double previousV = v; + v -= subV; + if (v < 0) v = 0; + if (v > maxV) v = maxV; + return v - previousV; + } + } + /// + /// 应当保证该减少值大于0 + /// + /// 返回实际改变量 + public double SubPositiveV(double subPositiveV) + { + lock (vLock) + { + subPositiveV = Math.Min(subPositiveV, v); + v -= subPositiveV; + } + return subPositiveV; + } + + /// + /// 试图加到满,如果无法加到maxValue则不加并返回-1 + /// + /// 返回实际改变量 + public double TryAddToMaxV(double addV) + { + lock (vLock) + { + if (maxV - v <= addV) + { + addV = maxV - v; + v = maxV; + return addV; + } + return -1; + } + } + + public bool Set0IfNotMax() + { + lock (vLock) + { + if (v < maxV) + { + v = 0; + return true; + } + } + return false; + } + public bool Set0IfMax() + { + lock (vLock) + { + if (v == maxV) + { + v = 0; + return true; + } + } + return false; + } } } \ No newline at end of file