Last active
August 9, 2023 08:04
-
-
Save adarapata/a3dd37525f925be12758fe110509ceb7 to your computer and use it in GitHub Desktop.
Photon Fusion Custom Tick Timer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct CustomTickTimer : INetworkStruct | |
{ | |
private int _target; | |
private int _initialTick; | |
public bool Expired(NetworkRunner runner) => runner.IsRunning && _target > 0 | |
&& (Tick)_target <= runner.Simulation.Tick; | |
public bool IsRunning => _target > 0; | |
public static CustomTickTimer CreateFromTicks(NetworkRunner runner, int ticks) | |
{ | |
if (runner == false || runner.IsRunning == false) | |
return new CustomTickTimer(); | |
CustomTickTimer fromTicks = new CustomTickTimer(); | |
fromTicks._target = (int)runner.Simulation.Tick + ticks; | |
fromTicks._initialTick = runner.Simulation.Tick; | |
return fromTicks; | |
} | |
public static CustomTickTimer CreateFromSeconds(NetworkRunner runner, float delayInSeconds) | |
{ | |
if (!runner || !runner.IsRunning) | |
return new CustomTickTimer(); | |
CustomTickTimer fromSeconds = new CustomTickTimer | |
{ | |
_target = (int)runner.Simulation.Tick + (int)Math.Ceiling((double)delayInSeconds / (double)runner.DeltaTime), | |
_initialTick = runner.Simulation.Tick | |
}; | |
return fromSeconds; | |
} | |
/// <summary> | |
/// 経過時間を0-1で正規化して返す | |
/// </summary> | |
/// <param name="runner"></param> | |
/// <returns></returns> | |
public float NormalizedValue(NetworkRunner runner) | |
{ | |
if (runner == null || runner.IsRunning == false || IsRunning == false) | |
return 0; | |
if (Expired(runner)) | |
return 1; | |
return ElapsedTicks(runner) / (_target - (float)_initialTick); | |
} | |
/// <summary> | |
/// 経過時間を1-0で正規化して返す | |
/// </summary> | |
/// <param name="runner"></param> | |
/// <returns></returns> | |
public float InverseNormalizedValue(NetworkRunner runner) | |
{ | |
return 1 - NormalizedValue(runner); | |
} | |
public int ElapsedTicks(NetworkRunner runner) | |
{ | |
if (runner == false || runner.IsRunning == false) | |
return 0; | |
if (IsRunning == false || Expired(runner)) | |
return 0; | |
return runner.Simulation.Tick - _initialTick; | |
} | |
public int RemainingTicks(NetworkRunner runner) | |
{ | |
if (!runner.IsRunning) | |
return 0; | |
return this.IsRunning ? (Math.Max(0, this._target - (int)runner.Simulation.Tick)) : 0; | |
} | |
public float RemainingTime(NetworkRunner runner) | |
{ | |
int tick = this.RemainingTicks(runner); | |
return tick * runner.DeltaTime; | |
} | |
public static readonly CustomTickTimer None = new(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment