Skip to content

Commit

Permalink
feat: ✨ Add the progress TimeBasedProgressAtVariableSpeed
Browse files Browse the repository at this point in the history
  • Loading branch information
shangfengh committed Nov 4, 2023
1 parent ea69149 commit 70604b5
Show file tree
Hide file tree
Showing 7 changed files with 254 additions and 200 deletions.
2 changes: 1 addition & 1 deletion logic/GameClass/GameObj/Character/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ public long SetPlayerState(RunningStateType runningState, PlayerStateType value
case PlayerStateType.OpeningTheDoorway:
if (value == PlayerStateType.Rescued) return -1;
Doorway doorway = (Doorway)lastObj!;
doorway.StopOpenning();
doorway.ProgressOfDoorway.TryStop();
return ChangePlayerState(runningState, value, gameObj);
case PlayerStateType.OpeningTheDoor:
if (value == PlayerStateType.Rescued) return -1;
Expand Down
58 changes: 3 additions & 55 deletions logic/GameClass/GameObj/Map/Doorway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,70 +17,18 @@ public Doorway(XY initPos) :
public override ShapeType Shape => ShapeType.Square;
public override bool IgnoreCollideExecutor(IGameObj targetObj)
{
if (!IsOpen()) return false;
if (!ProgressOfDoorway.IsFinished()) return false;
if (targetObj.Type != GameObjType.Character)
return true; // 非玩家不碰撞
return false;
}

public AtomicBool PowerSupply { get; } = new(false);

private long openStartTime = 0;
public long OpenStartTime
{
get
{
lock (gameObjLock)
return openStartTime;
}
}
public TimeBasedProgressAtVariableSpeed ProgressOfDoorway { get; } = new(GameData.degreeOfOpenedDoorway, 1);
public bool TryToOpen()
{
if (!PowerSupply) return false;
lock (gameObjLock)
{
if (openStartTime > 0) return false;
openStartTime = Environment.TickCount64;
return true;
}
return ProgressOfDoorway.Start();
}

public bool StopOpenning()
{
lock (gameObjLock)
{
if (Environment.TickCount64 - openStartTime + openDegree >= GameData.degreeOfOpenedDoorway)
{
openDegree = GameData.degreeOfOpenedDoorway;
return true;
}
else
{
openDegree = (int)(Environment.TickCount64 - openStartTime) + openDegree;
openStartTime = 0;
return false;
}
}
}

public void FinishOpenning()
{
lock (gameObjLock)
{
openDegree = GameData.degreeOfOpenedDoorway;
}
}

private int openDegree = 0;
public int OpenDegree
{
get
{
lock (gameObjLock)
return openDegree;
}
}

public bool IsOpen() => (OpenDegree == GameData.degreeOfOpenedDoorway);
}
}
7 changes: 4 additions & 3 deletions logic/Gaming/ActionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Threading;
using GameClass.GameObj;
using GameEngine;
using Preparation.Interface;
using Preparation.Utility;
using Timothy.FrameRateTask;

Expand Down Expand Up @@ -148,11 +149,11 @@ public bool OpenDoorway(Student player)
player.ThreadNum.Release();
return;
}
Thread.Sleep(GameData.degreeOfOpenedDoorway - doorwayToOpen.OpenDegree);
Thread.Sleep(GameData.degreeOfOpenedDoorway - (int)doorwayToOpen.ProgressOfDoorway.GetProgressNow());

if (player.ResetPlayerState(stateNum))
{
doorwayToOpen.FinishOpenning();
doorwayToOpen.ProgressOfDoorway.Finish();
player.ThreadNum.Release();
}
}
Expand All @@ -168,7 +169,7 @@ public bool Escape(Student player)
return false;

Doorway? doorwayForEscape = (Doorway?)gameMap.OneForInteract(player.Position, GameObjType.Doorway);
if (doorwayForEscape != null && doorwayForEscape.IsOpen())
if (doorwayForEscape != null && doorwayForEscape.ProgressOfDoorway.IsProgressing())
{
if (!player.TryToRemoveFromGame(PlayerStateType.Escaped)) return false;
player.AddScore(GameData.StudentScoreEscape);
Expand Down
5 changes: 1 addition & 4 deletions logic/Preparation/Interface/IDoorway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ namespace Preparation.Interface
{
public interface IDoorway : IGameObj
{
public long OpenStartTime { get; }
public int OpenDegree { get; }
public bool StopOpenning();
public bool TryToOpen();
public TimeBasedProgressAtVariableSpeed ProgressOfDoorway { get; }
}
}
109 changes: 53 additions & 56 deletions logic/Preparation/Utility/SafeValue/InTheRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public InTheVariableRange()
/// </summary>
public class IntInTheVariableRange : InTheVariableRange
{
private int v;
private int maxV;
protected int v;
protected int maxV;
#region 构造与读取
public IntInTheVariableRange(int value, int maxValue) : base()
{
Expand Down Expand Up @@ -157,6 +157,13 @@ public bool TrySetMaxV(int maxValue)
return true;
}
}
public void SetVToMaxV()
{
lock (vLock)
{
v = maxV;
}
}
public bool Set0IfNotMaxor0()
{
lock (vLock)
Expand Down Expand Up @@ -397,6 +404,32 @@ public int AddV(StartTime startTime, double speed = 1.0)
return v - previousV;
}
}

/// <summary>
/// 试图加到满,如果加上时间差*速度可以达到MaxV,则加上并使startTime变为long.MaxValue
/// 如果无法加到maxValue则清零
/// </summary>
/// <returns>返回是否清零</returns>
public bool Set0IfNotAddToMaxV(StartTime startTime, double speed = 1.0)
{
lock (vLock)
{
if (v == maxV) return false;
int addV = (int)(startTime.StopIfPassing(maxV - v) * speed);
if (addV < 0)
{
v = 0;
return true;
}
if (maxV - v < addV)
{
v = maxV;
return false;
}
v = 0;
return false;
}
}
#endregion
}

Expand All @@ -405,8 +438,8 @@ public int AddV(StartTime startTime, double speed = 1.0)
/// </summary>
public class LongInTheVariableRange : InTheVariableRange
{
private long v;
private long maxV;
protected long v;
protected long maxV;
#region 构造与读取
public LongInTheVariableRange(long value, long maxValue) : base()
{
Expand Down Expand Up @@ -455,23 +488,6 @@ public bool IsMaxV()
}
#endregion

#region 内嵌读取(在锁的情况下读取内容同时读取其他更基本的外部数据)
public (long, long) GetValue(StartTime startTime)
{
lock (vLock)
{
return (v, startTime.Get());
}
}
public (long, long, long) GetValueAndMaxV(StartTime startTime)
{
lock (vLock)
{
return (v, maxV, startTime.Get());
}
}
#endregion

#region 普通设置MaxV与Value的值的方法
/// <summary>
/// 若maxValue<=0则maxValue设为0并返回False
Expand Down Expand Up @@ -645,6 +661,13 @@ public bool TrySetMaxV(long maxValue)
return true;
}
}
public void SetVToMaxV()
{
lock (vLock)
{
v = maxV;
}
}

public bool Set0IfNotMax()
{
Expand Down Expand Up @@ -709,22 +732,6 @@ public long TryAddToMaxV(long addV)
}
}

/// <summary>
/// 增加量为时间差*速度,并将startTime变为long.MaxValue
/// </summary>
/// <returns>返回实际改变量</returns>
public long AddV(StartTime startTime, double speed = 1.0)
{
lock (vLock)
{
long previousV = v;
long addV = (Environment.TickCount64 - startTime.Stop());
if (addV < 0) v += (long)(addV * speed);
else return 0;
if (v > maxV) v = maxV;
return v - previousV;
}
}
#endregion
}

Expand All @@ -733,8 +740,8 @@ public long AddV(StartTime startTime, double speed = 1.0)
/// </summary>
public class DoubleInTheVariableRange : InTheVariableRange
{
private double v;
private double maxV;
protected double v;
protected double maxV;
#region 构造与读取
public DoubleInTheVariableRange(double value, double maxValue) : base()
{
Expand Down Expand Up @@ -783,23 +790,6 @@ public bool IsMaxV()
}
#endregion

#region 内嵌读取(在锁的情况下读取内容同时读取其他更基本的外部数据)
public (double, long) GetValue(StartTime startTime)
{
lock (vLock)
{
return (v, startTime.Get());
}
}
public (double, double, long) GetValueAndMaxValue(StartTime startTime)
{
lock (vLock)
{
return (v, maxV, startTime.Get());
}
}
#endregion

#region 普通设置MaxV与Value的值的方法
/// <summary>
/// 若maxValue<=0则maxValue设为0并返回False
Expand Down Expand Up @@ -949,6 +939,13 @@ public bool TrySetMaxV(double maxValue)
return true;
}
}
public void SetVToMaxV()
{
lock (vLock)
{
v = maxV;
}
}

public bool Set0IfNotMax()
{
Expand Down
Loading

0 comments on commit 70604b5

Please sign in to comment.