Skip to content

Commit

Permalink
Merge pull request #256 from shangfengh/dev
Browse files Browse the repository at this point in the history
feat(SafeValue): 🔒 Add the GetDivideValueByMaxV
  • Loading branch information
shangfengh authored Apr 18, 2024
2 parents 9aa1982 + 328722d commit ac50cd3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion logic/Gaming/AttackManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private void BombObj(Bullet bullet, GameObj objBeingShot)
{
Debugger.Output(ship, " is destroyed!");
var money = ship.GetCost();
bullet.Parent.AddMoney(money);
bullet.Parent!.AddMoney(money);
Debugger.Output(bullet.Parent, " get " + money.ToString() + " money because of destroying " + ship);
shipManager.Remove(ship);
}
Expand Down
2 changes: 1 addition & 1 deletion logic/Gaming/ShipManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public bool Recycle(Ship ship)
return false;
}
Debugger.Output(ship, $" 's value is {shipValue}");
ship.AddMoney((long)(shipValue * 0.5 * ship.HP / ship.HP.GetMaxV()));
ship.AddMoney((long)(shipValue * 0.5 * ship.HP.GetDivideValueByMaxV()));
Debugger.Output(ship, " is recycled!");
Remove(ship);
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public T GetMaxV()
{
return ReadNeed(() => (v, maxV));
}
public double GetDivideValueByMaxV()
{
return ReadNeed(() => (v.ToDouble(null) / maxV.ToDouble(null)));
}
#endregion

#region 特别的读取
Expand Down
28 changes: 12 additions & 16 deletions logic/Preparation/Utility/Value/SafeValue/TimeBased.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public class IntNumUpdateEachCD
{
private int num;
private int maxNum;
private int cd;
public AtomicInt CD { get; } = new(int.MaxValue);
private long updateTime = 0;
private readonly object numLock = new();
public IntNumUpdateEachCD(int num, int maxNum, int cd)
Expand All @@ -271,7 +271,7 @@ public IntNumUpdateEachCD(int num, int maxNum, int cd)
if (cd <= 0) Debugger.Output("Bug:IntNumUpdateEachCD.cd (" + cd.ToString() + ") is less than 0.");
this.num = (num < maxNum) ? num : maxNum;
this.maxNum = maxNum;
this.cd = cd;
CD.Set(cd);
this.updateTime = Environment.TickCount64;
}
/// <summary>
Expand All @@ -282,24 +282,23 @@ public IntNumUpdateEachCD(int maxNum, int cd)
if (maxNum < 0) Debugger.Output("Bug:IntNumUpdateEachCD.maxNum (" + maxNum.ToString() + ") is less than 0.");
if (cd <= 0) Debugger.Output("Bug:IntNumUpdateEachCD.cd (" + cd.ToString() + ") is less than 0.");
this.num = this.maxNum = maxNum;
this.cd = cd;
CD.Set(cd);
}
public IntNumUpdateEachCD()
{
this.num = this.maxNum = 0;
this.cd = int.MaxValue;
}

public int GetMaxNum() { lock (numLock) return maxNum; }
public int GetCD() { lock (numLock) return cd; }
public int GetCD() => CD.Get();
public int GetNum(long time)
{
lock (numLock)
{
if (num < maxNum && time - updateTime >= cd)
if (num < maxNum && time - updateTime >= CD)
{
int add = (int)Math.Min(maxNum - num, (time - updateTime) / cd);
updateTime += add * cd;
int add = (int)Math.Min(maxNum - num, (time - updateTime) / CD);
updateTime += add * CD;
return (num += add);
}
return num;
Expand All @@ -316,10 +315,10 @@ public int TrySub(int subV)
long time = Environment.TickCount64;
lock (numLock)
{
if (num < maxNum && time - updateTime >= cd)
if (num < maxNum && time - updateTime >= CD)
{
int add = (int)Math.Min(maxNum - num, (time - updateTime) / cd);
updateTime += add * cd;
int add = (int)Math.Min(maxNum - num, (time - updateTime) / CD);
updateTime += add * CD;
num += add;
}
if (num == maxNum) updateTime = time;
Expand Down Expand Up @@ -423,11 +422,8 @@ public void SetPositiveNum(int num)
}
public void SetCD(int cd)
{
lock (numLock)
{
if (cd <= 0) Debugger.Output("Bug:Set IntNumUpdateEachCD.cd to " + cd.ToString() + ".");
this.cd = cd;
}
if (cd <= 0) Debugger.Output("Bug:Set IntNumUpdateEachCD.cd to " + cd.ToString() + ".");
CD.Set(cd);
}
}
}

0 comments on commit ac50cd3

Please sign in to comment.