Browse Source

refactor: 🔒 add a lock for PropInventory

tags/v0.1.0
shangfengh 2 years ago
parent
commit
bdab9a65a3
3 changed files with 30 additions and 17 deletions
  1. +25
    -15
      logic/GameClass/GameObj/Character/Character.cs
  2. +4
    -0
      logic/GameClass/GameObj/Prop/Gadget.cs
  3. +1
    -2
      logic/Gaming/PropManager.cs

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

@@ -13,6 +13,8 @@ namespace GameClass.GameObj
public ReaderWriterLockSlim HPReadWriterLock => hpReaderWriterLock; public ReaderWriterLockSlim HPReadWriterLock => hpReaderWriterLock;
private readonly object vampireLock = new(); private readonly object vampireLock = new();
public object VampireLock => vampire; public object VampireLock => vampire;
private readonly object inventoryLock = new();
public object InventoryLock => inventoryLock;


#region 装弹、攻击相关的基本属性及方法 #region 装弹、攻击相关的基本属性及方法
/// <summary> /// <summary>
@@ -572,14 +574,15 @@ namespace GameClass.GameObj
{new NullProp(), new NullProp(),new NullProp() }; {new NullProp(), new NullProp(),new NullProp() };
public Gadget[] PropInventory public Gadget[] PropInventory
{ {
get => propInventory;
get
{
lock (inventoryLock)
return propInventory;
}
set set
{ {
lock (gameObjLock)
{
lock (inventoryLock)
propInventory = value; propInventory = value;
Debugger.Output(this, " prop becomes " + (PropInventory == null ? "null" : PropInventory.ToString()));
}
} }
} }


@@ -591,9 +594,10 @@ namespace GameClass.GameObj
{ {
if (indexing < 0 || indexing >= GameData.maxNumOfPropInPropInventory) if (indexing < 0 || indexing >= GameData.maxNumOfPropInPropInventory)
return new NullProp(); return new NullProp();
lock (gameObjLock)
lock (inventoryLock)
{ {
Gadget prop = propInventory[indexing]; Gadget prop = propInventory[indexing];
if (!prop.IsUsable()) return new NullProp();
PropInventory[indexing] = new NullProp(); PropInventory[indexing] = new NullProp();
return prop; return prop;
} }
@@ -601,13 +605,13 @@ namespace GameClass.GameObj


public Gadget UseProp(PropType propType) public Gadget UseProp(PropType propType)
{ {
lock (gameObjLock)
if (propType == PropType.Null)
{ {
if (propType == PropType.Null)
lock (inventoryLock)
{ {
for (int indexing = 0; indexing < GameData.maxNumOfPropInPropInventory; ++indexing) for (int indexing = 0; indexing < GameData.maxNumOfPropInPropInventory; ++indexing)
{ {
if (PropInventory[indexing].GetPropType() != PropType.Null)
if (PropInventory[indexing].IsUsable())
{ {
Gadget prop = PropInventory[indexing]; Gadget prop = PropInventory[indexing];
PropInventory[indexing] = new NullProp(); PropInventory[indexing] = new NullProp();
@@ -615,18 +619,23 @@ namespace GameClass.GameObj
} }
} }
} }
else
}
else
{
lock (inventoryLock)
{
for (int indexing = 0; indexing < GameData.maxNumOfPropInPropInventory; ++indexing) for (int indexing = 0; indexing < GameData.maxNumOfPropInPropInventory; ++indexing)
{ {
if (PropInventory[indexing].GetPropType() == propType)
if (PropInventory[indexing].GetPropType() == propType && PropInventory[indexing].IsUsable())
{ {
Gadget prop = PropInventory[indexing]; Gadget prop = PropInventory[indexing];
PropInventory[indexing] = new NullProp(); PropInventory[indexing] = new NullProp();
return prop; return prop;
} }
} }
return new NullProp();
}
} }
return new NullProp();
} }


/// <summary> /// <summary>
@@ -635,9 +644,10 @@ namespace GameClass.GameObj
public int IndexingOfAddProp() public int IndexingOfAddProp()
{ {
int indexing = 0; int indexing = 0;
for (; indexing < GameData.maxNumOfPropInPropInventory; ++indexing)
if (PropInventory[indexing].GetPropType() == PropType.Null)
break;
lock (inventoryLock)
for (; indexing < GameData.maxNumOfPropInPropInventory; ++indexing)
if (propInventory[indexing].GetPropType() == PropType.Null)
break;
return indexing; return indexing;
} }




+ 4
- 0
logic/GameClass/GameObj/Prop/Gadget.cs View File

@@ -18,6 +18,7 @@ namespace GameClass.GameObj


public override ShapeType Shape => ShapeType.Square; public override ShapeType Shape => ShapeType.Square;


public abstract bool IsUsable();
public abstract PropType GetPropType(); public abstract PropType GetPropType();


public Gadget(XY initPos, int radius = GameData.PropRadius) : public Gadget(XY initPos, int radius = GameData.PropRadius) :
@@ -45,10 +46,12 @@ namespace GameClass.GameObj
} }
} }
} }
public override bool IsUsable() => !IsUsed;
public Tool(XY initPos) : base(initPos) { } public Tool(XY initPos) : base(initPos) { }
} }
public abstract class Consumables : Gadget public abstract class Consumables : Gadget
{ {
public override bool IsUsable() => true;
public Consumables(XY initPos) : base(initPos) { } public Consumables(XY initPos) : base(initPos) { }
} }


@@ -134,6 +137,7 @@ namespace GameClass.GameObj
} }
public sealed class NullProp : Gadget public sealed class NullProp : Gadget
{ {
public override bool IsUsable() => false;
public NullProp() : base(new XY(1, 1)) public NullProp() : base(new XY(1, 1))
{ {
} }


+ 1
- 2
logic/Gaming/PropManager.cs View File

@@ -73,8 +73,7 @@ namespace Gaming
/// <returns></returns> /// <returns></returns>
public bool PickProp(Character player, PropType propType = PropType.Null) public bool PickProp(Character player, PropType propType = PropType.Null)
{ {
if (player.IsRemoved)
return false;
if (!player.Commandable()) return false;
int indexing = player.IndexingOfAddProp(); int indexing = player.IndexingOfAddProp();
if (indexing == GameData.maxNumOfPropInPropInventory) if (indexing == GameData.maxNumOfPropInPropInventory)
return false; return false;


Loading…
Cancel
Save