Browse Source

refactor: 🎨 add the DoubleInTheVariableRnge

dev
shangfengh 2 years ago
parent
commit
6a0b78698d
4 changed files with 321 additions and 62 deletions
  1. +12
    -13
      logic/GameClass/GameObj/Character/Character.BuffManager.cs
  2. +2
    -25
      logic/GameClass/GameObj/Character/Character.cs
  3. +2
    -2
      logic/Preparation/Interface/ICharacter.cs
  4. +305
    -22
      logic/Preparation/Utility/SafeValue/InTheRange.cs

+ 12
- 13
logic/GameClass/GameObj/Character/Character.BuffManager.cs View File

@@ -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;
}

/// <summary>
/// 清除所有buff


+ 2
- 25
logic/GameClass/GameObj/Character/Character.cs View File

@@ -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();


+ 2
- 2
logic/Preparation/Interface/ICharacter.cs View File

@@ -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; }


+ 305
- 22
logic/Preparation/Utility/SafeValue/InTheRange.cs View File

@@ -8,16 +8,16 @@ namespace Preparation.Utility
/// <summary>
/// 一个保证在[0,maxValue]的可变int,支持可变的maxValue(请确保大于0)
/// </summary>
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
/// <summary>
/// 默认使Value=maxValue
/// </summary>
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; }

/// <summary>
@@ -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;
}
}
/// <summary>
@@ -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;
}
}
/// <returns>返回实际改变量</returns>
@@ -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;
}
}

/// <summary>
/// 一个保证在[0,maxValue]的可变long,支持可变的maxValue(请确保大于0)
/// </summary>
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
/// <summary>
/// 默认使Value=maxValue
/// </summary>
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; }

/// <summary>
@@ -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;
}
}
/// <summary>
@@ -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;
}
}
/// <returns>返回实际改变量</returns>
@@ -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;
}
}

/// <summary>
/// 一个保证在[0,maxValue]的可变double,支持可变的maxValue(请确保大于0)
/// </summary>
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;
}
/// <summary>
/// 默认使Value=maxValue
/// </summary>
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; }

/// <summary>
/// 若maxValue<=0则maxValue设为0并返回False
/// </summary>
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;
}
/// <summary>
/// 应当保证该maxValue>=0
/// </summary>
public void SetPositiveMaxV(double maxValue)
{
lock (vLock)
{
maxV = maxValue;
if (v > maxValue) v = maxValue;
}
}
/// <summary>
/// 如果当前值大于maxValue,则更新maxValue失败
/// </summary>
public bool TrySetMaxV(double maxValue)
{
lock (vLock)
{
if (v > maxValue) return false;
maxV = maxValue;
return true;
}
}

/// <summary>
/// 应当保证该value>=0
/// </summary>
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;
}
}
/// <returns>返回实际改变量</returns>
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;
}
}
/// <summary>
/// 应当保证增加值大于0
/// </summary>
/// <returns>返回实际改变量</returns>
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;
}
}
/// <summary>
/// 应当保证乘数大于0
/// </summary>
public void MulPositiveV(double mulPositiveV)
{
lock (vLock)
{
if (v > maxV / mulPositiveV) v = maxV; //避免溢出
else v *= mulPositiveV;
}
}
/// <returns>返回实际改变量</returns>
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;
}
}
/// <summary>
/// 应当保证该减少值大于0
/// </summary>
/// <returns>返回实际改变量</returns>
public double SubPositiveV(double subPositiveV)
{
lock (vLock)
{
subPositiveV = Math.Min(subPositiveV, v);
v -= subPositiveV;
}
return subPositiveV;
}

/// <summary>
/// 试图加到满,如果无法加到maxValue则不加并返回-1
/// </summary>
/// <returns>返回实际改变量</returns>
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;
}
}
}

Loading…
Cancel
Save