Browse Source

refactor: 🎨 add the struct AtomicBool

dev
shangfengh 2 years ago
parent
commit
5a31714a6c
7 changed files with 36 additions and 16 deletions
  1. +2
    -9
      logic/GameClass/GameObj/GameObj.cs
  2. +1
    -1
      logic/Gaming/AttackManager.cs
  3. +3
    -3
      logic/Gaming/CharacterManager.cs
  4. +1
    -0
      logic/Preparation/Interface/IGameObj.cs
  5. +0
    -1
      logic/Preparation/Interface/IMoveable.cs
  6. +29
    -1
      logic/Preparation/Utility/LockedValue.cs
  7. +0
    -1
      logic/Preparation/Utility/XY.cs

+ 2
- 9
logic/GameClass/GameObj/GameObj.cs View File

@@ -32,17 +32,10 @@ namespace GameClass.GameObj

public abstract ShapeType Shape { get; }

protected int isRemoved = 0;
public bool IsRemoved
{
get
{
return (Interlocked.CompareExchange(ref isRemoved, 0, 0) == 1);
}
}
public AtomicBool IsRemoved { get; } = new AtomicBool(false);
public virtual bool TryToRemove()
{
return Interlocked.Exchange(ref isRemoved, 1) == 0;
return IsRemoved.TrySet(true);
}

public int Radius { get; }


+ 1
- 1
logic/Gaming/AttackManager.cs View File

@@ -67,7 +67,7 @@ namespace Gaming
break;
case GameObjType.Generator:
if (bullet.CanBeBombed(GameObjType.Generator))
((Generator)objBeingShot).Repair(-bullet.AP.Get() * GameData.factorDamageGenerator, (Character)bullet.Parent!);
((Generator)objBeingShot).Repair(-bullet.AP * GameData.factorDamageGenerator, (Character)bullet.Parent!);
break;
case GameObjType.Door:
if (bullet.CanBeBombed(GameObjType.Door))


+ 3
- 3
logic/Gaming/CharacterManager.cs View File

@@ -326,7 +326,7 @@ namespace Gaming
{
if (bullet.HasSpear)
{
long subHp = student.SubHp(bullet.AP.Get());
long subHp = student.SubHp(bullet.AP);
#if DEBUG
Debugger.Output(this, "is being shot! Now his hp is" + student.HP.ToString());
#endif
@@ -340,14 +340,14 @@ namespace Gaming
long subHp;
if (bullet.HasSpear)
{
subHp = student.SubHp(bullet.AP.Get() + GameData.ApSpearAdd);
subHp = student.SubHp(bullet.AP + GameData.ApSpearAdd);
#if DEBUG
Debugger.Output(this, "is being shot with Spear! Now his hp is" + student.HP.ToString());
#endif
}
else
{
subHp = student.SubHp(bullet.AP.Get());
subHp = student.SubHp(bullet.AP);
#if DEBUG
Debugger.Output(this, "is being shot! Now his hp is" + student.HP.ToString());
#endif


+ 1
- 0
logic/Preparation/Interface/IGameObj.cs View File

@@ -8,6 +8,7 @@ namespace Preparation.Interface
public long ID { get; }
public XY Position { get; } // if Square, Pos equals the center
public bool IsRigid { get; }
public AtomicBool IsRemoved { get; }
public ShapeType Shape { get; }
public bool CanMove { get; }
public int Radius { get; } // if Square, Radius equals half length of one side


+ 0
- 1
logic/Preparation/Interface/IMoveable.cs View File

@@ -10,7 +10,6 @@ namespace Preparation.Interface
object ActionLock { get; }
public int MoveSpeed { get; }
public bool IsMoving { get; set; }
public bool IsRemoved { get; }
public bool IsAvailableForMove { get; }
public long StateNum { get; }
public Semaphore ThreadNum { get; }


+ 29
- 1
logic/Preparation/Utility/LockedValue.cs View File

@@ -2,6 +2,7 @@

namespace Preparation.Utility
{
//理论上结构体最好不可变,这里采用了可变结构。
public struct AtomicInt
{
private int v;
@@ -11,6 +12,7 @@ namespace Preparation.Utility
}
public override string ToString() => Interlocked.CompareExchange(ref v, -1, -1).ToString();
public int Get() => Interlocked.CompareExchange(ref v, -1, -1);
public static implicit operator int(AtomicInt aint) => Interlocked.CompareExchange(ref aint.v, -1, -1);
public int Set(int value) => Interlocked.Exchange(ref v, value);

public int Add(int x) => Interlocked.Add(ref v, x);
@@ -18,6 +20,32 @@ namespace Preparation.Utility
public int Inc() => Interlocked.Increment(ref v);
public int Dec() => Interlocked.Decrement(ref v);

public int CompareExchange(int b, int c) => Interlocked.CompareExchange(ref v, b, c);
public void CompareExchange(int b, int c) => Interlocked.CompareExchange(ref v, b, c);
/// <returns>返回操作前的值</returns>
public int CompareExReturnOri(int b, int c) => Interlocked.CompareExchange(ref v, b, c);
}
public struct AtomicBool
{
private int v;//v==0为false,v!=0(v==1或v==-1)为true
public AtomicBool(bool x)
{
v = x ? 1 : 0;
}
public override string ToString() => (Interlocked.CompareExchange(ref v, -1, -1) == 0) ? "false" : "true";
public bool Get() => (Interlocked.CompareExchange(ref v, -1, -1) != 0);
public static implicit operator bool(AtomicBool abool) => (Interlocked.CompareExchange(ref abool.v, -1, -1) != 0);

public bool Set(bool value) => (Interlocked.Exchange(ref v, value ? 1 : 0) != 0);

/// <returns>赋值前的值是否与将赋予的值不相同</returns>
public bool TrySet(bool value)
{
int ori = Interlocked.CompareExchange(ref v, value ? 1 : 0, value ? 1 : 0);
return value ? (ori == 0) : (ori != 0);
}

public bool Invert() => Interlocked.Add(ref v, -1) != 0;
public bool And(bool x) => Interlocked.And(ref v, x ? 1 : 0) != 0;
public bool Or(bool x) => Interlocked.Or(ref v, x ? 1 : 0) != 0;
}
}

+ 0
- 1
logic/Preparation/Utility/XY.cs View File

@@ -2,7 +2,6 @@

namespace Preparation.Utility
{

public struct XY
{
public int x;


Loading…
Cancel
Save