Skip to content

Commit

Permalink
Merge pull request #670 from shangfengh/new
Browse files Browse the repository at this point in the history
feat: ✨ Add the progress TimeBasedProgressAtVariableSpeed
  • Loading branch information
shangfengh authored Nov 4, 2023
2 parents ea69149 + f8d0f83 commit f345eee
Show file tree
Hide file tree
Showing 9 changed files with 480 additions and 219 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);
}
}
2 changes: 1 addition & 1 deletion logic/GameClass/GameObj/Map/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void AddNumOfRepairedGenerators()
GameObjLockDict[GameObjType.EmergencyExit].EnterReadLock();
try
{
Random r = new Random(Environment.TickCount);
Random r = new(Environment.TickCount);
EmergencyExit emergencyExit = (EmergencyExit)(GameObjDict[GameObjType.EmergencyExit][r.Next(0, GameObjDict[GameObjType.EmergencyExit].Count)]);
emergencyExit.CanOpen.SetReturnOri(true);
Preparation.Utility.Debugger.Output(emergencyExit, emergencyExit.Position.ToString());
Expand Down
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; }
}
}
Loading

0 comments on commit f345eee

Please sign in to comment.