Skip to content

Commit

Permalink
Merge pull request #164 from shangfengh/dev
Browse files Browse the repository at this point in the history
refactor: 🎨 use the InVariableRange<T>
  • Loading branch information
asdawej authored Apr 6, 2024
2 parents 9887042 + c553095 commit 7b3405f
Show file tree
Hide file tree
Showing 14 changed files with 200 additions and 707 deletions.
4 changes: 2 additions & 2 deletions logic/GameClass/GameObj/Areas/Construction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class Construction(XY initPos)
: Immovable(initPos, GameData.NumOfPosGridPerCell / 2, GameObjType.Construction)
{
public AtomicLong TeamID { get; } = new(long.MaxValue);
public LongInTheVariableRange HP { get; } = new(0, GameData.CommunityHP);
public InVariableRange<long> HP { get; } = new(0, GameData.CommunityHP);
public override bool IsRigid => constructionType == ConstructionType.Community;
public override ShapeType Shape => ShapeType.Square;

Expand Down Expand Up @@ -58,7 +58,7 @@ public bool Construct(int constructSpeed, ConstructionType constructionType, Shi
{
return false;
}
return ship.MoneyPool.SubMoney(HP.AddV(addHP) / 10) > 0;
return ship.MoneyPool.SubMoney(HP.AddPositiveVRChange(addHP) / 10) > 0;
}
public void BeAttacked(Bullet bullet)
{
Expand Down
2 changes: 1 addition & 1 deletion logic/GameClass/GameObj/Areas/Home.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class Home(XY initPos, long id)
: Immovable(initPos, GameData.NumOfPosGridPerCell / 2, GameObjType.Home), IHome
{
public long TeamID { get; } = id;
public LongInTheVariableRange HP => new(GameData.HomeHP);
public InVariableRange<long> HP => new(GameData.HomeHP);
public override bool IsRigid => false;
public override ShapeType Shape => ShapeType.Square;

Expand Down
4 changes: 2 additions & 2 deletions logic/GameClass/GameObj/Areas/Resource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ namespace GameClass.GameObj.Areas;
public class Resource(XY initPos)
: Immovable(initPos, GameData.NumOfPosGridPerCell / 2, GameObjType.Resource)
{
public LongInTheVariableRange HP { get; } = new LongInTheVariableRange(GameData.ResourceHP);
public InVariableRange<long> HP { get; } = new InVariableRange<long>(GameData.ResourceHP);
public override bool IsRigid => true;
public override ShapeType Shape => ShapeType.Square;
public AtomicInt ProduceNum { get; } = new AtomicInt(0);
public bool Produce(int produceSpeed, Ship ship)
{
return ship.MoneyPool.AddMoney(HP.SubV(produceSpeed)) > 0;
return ship.MoneyPool.AddMoney(HP.SubRChange(produceSpeed)) > 0;
}
public void AddProduceNum(int add = 1)
{
Expand Down
4 changes: 2 additions & 2 deletions logic/GameClass/GameObj/Areas/Wormhole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace GameClass.GameObj.Areas;
public class Wormhole(XY initPos, List<XY> grids)
: Immovable(initPos, GameData.NumOfPosGridPerCell / 2, GameObjType.Wormhole), IWormhole
{
public LongInTheVariableRange HP = new(GameData.WormholeHP);
public InVariableRange<long> HP = new(GameData.WormholeHP);
private readonly List<XY> grids = grids;
public List<XY> Grids => grids;
public override bool IsRigid => HP > GameData.WormholeHP / 2;
Expand All @@ -20,7 +20,7 @@ public bool Repair(int constructSpeed, Ship ship)
{
return false;
}
return ship.MoneyPool.SubMoney(HP.AddV(addHP) / 10) > 0;
return ship.MoneyPool.SubMoney(HP.AddRChange(addHP) / 10) > 0;
}
public void BeAttacked(Bullet bullet)
{
Expand Down
18 changes: 9 additions & 9 deletions logic/GameClass/GameObj/Ship.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public override bool IgnoreCollideExecutor(IGameObj targetObj)
return false;
}
// 属性值
public LongInTheVariableRange HP { get; }
public LongInTheVariableRange Armor { get; }
public LongInTheVariableRange Shield { get; }
public InVariableRange<long> HP { get; }
public InVariableRange<long> Armor { get; }
public InVariableRange<long> Shield { get; }
public ShipType ShipType { get; }
private ShipStateType shipState = ShipStateType.Null;
public ShipStateType ShipState => shipState;
Expand Down Expand Up @@ -160,37 +160,37 @@ public bool InstallModule(ModuleType moduleType)
case ModuleType.Armor1:
armorType = ArmorType.Armor1;
armor = ModuleFactory.FindIArmor(ShipType, armorType);
Armor.SetV(armor.ArmorHP);
Armor.SetRNow(armor.ArmorHP);
SubMoney(armor.Cost);
return true;
case ModuleType.Armor2:
armorType = ArmorType.Armor2;
armor = ModuleFactory.FindIArmor(ShipType, armorType);
Armor.SetV(armor.ArmorHP);
Armor.SetRNow(armor.ArmorHP);
SubMoney(armor.Cost);
return true;
case ModuleType.Armor3:
armorType = ArmorType.Armor3;
armor = ModuleFactory.FindIArmor(ShipType, armorType);
Armor.SetV(armor.ArmorHP);
Armor.SetRNow(armor.ArmorHP);
SubMoney(armor.Cost);
return true;
case ModuleType.Shield1:
shieldType = ShieldType.Shield1;
shield = ModuleFactory.FindIShield(ShipType, shieldType);
Shield.SetV(shield.ShieldHP);
Shield.SetRNow(shield.ShieldHP);
SubMoney(shield.Cost);
return true;
case ModuleType.Shield2:
shieldType = ShieldType.Shield2;
shield = ModuleFactory.FindIShield(ShipType, shieldType);
Shield.SetV(shield.ShieldHP);
Shield.SetRNow(shield.ShieldHP);
SubMoney(shield.Cost);
return true;
case ModuleType.Shield3:
shieldType = ShieldType.Shield3;
shield = ModuleFactory.FindIShield(ShipType, shieldType);
Shield.SetV(shield.ShieldHP);
Shield.SetRNow(shield.ShieldHP);
SubMoney(shield.Cost);
return true;
case ModuleType.LaserGun:
Expand Down
3 changes: 1 addition & 2 deletions logic/Gaming/ShipManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ public bool Recover(Ship ship, long recover)
{
return false;
}
long actualRecover = ship.HP.AddPositiveV(recover);
Debugger.Output(ship, " 's HP is recovered to " + ship.HP.ToString());
long actualRecover = ship.HP.AddPositiveVRChange(recover);
ship.SubMoney((long)(actualRecover * 1.2));
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion logic/Preparation/Interface/IHome.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace Preparation.Interface
public interface IHome
{
public long TeamID { get; }
public LongInTheVariableRange HP { get; }
public InVariableRange<long> HP { get; }
}
}
6 changes: 3 additions & 3 deletions logic/Preparation/Interface/IShip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace Preparation.Interface
{
public interface IShip : IMovable, IPlayer
{
public LongInTheVariableRange HP { get; }
public LongInTheVariableRange Armor { get; }
public LongInTheVariableRange Shield { get; }
public InVariableRange<long> HP { get; }
public InVariableRange<long> Armor { get; }
public InVariableRange<long> Shield { get; }
public ShipType ShipType { get; }
public ShipStateType ShipState { get; }
public IntNumUpdateEachCD BulletNum { get; }
Expand Down
5 changes: 5 additions & 0 deletions logic/Preparation/Utility/Value/IAddable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ public interface IAddable<T>
{
public void Add(T value);
}

public interface IIntAddable
{
public void Add(int value);
}
}
12 changes: 6 additions & 6 deletions logic/Preparation/Utility/Value/SafeValue/AtomicInt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Preparation.Utility
{
//其对应属性不应当有set访问器,避免不安全的=赋值

public class AtomicInt(int x) : Atomic, IAddable<int>
public class AtomicInt(int x) : Atomic, IIntAddable
{
protected int v = x;

Expand Down Expand Up @@ -49,8 +49,8 @@ public class AtomicInt(int x) : Atomic, IAddable<int>
/// </summary>
public class AtomicIntOnlyAddScore(int x, double speed = 1.0) : AtomicInt(x)
{
public IAddable<int> score = new AtomicInt(0);
public IAddable<int> Score
public IIntAddable score = new AtomicInt(0);
public IIntAddable Score
{
get
{
Expand Down Expand Up @@ -154,8 +154,8 @@ public int CompareExROriNotAddScore(int newV, int compareTo)
/// </summary>
public class AtomicIntChangeAffectScore(int x, double speed = 1.0) : AtomicInt(x)
{
public IAddable<int> score = new AtomicInt(0);
public IAddable<int> Score
public IIntAddable score = new AtomicInt(0);
public IIntAddable Score
{
get
{
Expand Down Expand Up @@ -260,7 +260,7 @@ public int CompareExROriNotAddScore(int newV, int compareTo)
}
}

public class AtomicLong(long x) : Atomic, IAddable<int>, IAddable<long>
public class AtomicLong(long x) : Atomic, IIntAddable, IAddable<long>
{
protected long v = x;

Expand Down
Loading

0 comments on commit 7b3405f

Please sign in to comment.