Quantum 3 3.1.0

Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | List of all members
Quantum.FrameTimer Struct Reference

Built-in timer struct for game logic that counts frames, it is not affected by FrameBase.GameTimeScale (see GameTimer). Seconds are converted with the simulation rate (FrameBase.UpdateRate). More...

Public Member Functions

readonly FP DurationNormalized (FrameBase frame)
 The progress of the timer between 0 (just started) and 1 (expired), e.g. to display a cooldown. More...
 
readonly int ElapsedFrames (FrameBase frame)
 How many frames or ticks since the timer was started. More...
 
readonly FP ElapsedSeconds (FrameBase frame)
 How long since the timer has started in seconds. More...
 
readonly override int GetHashCode ()
 Overrides the hash code generation of this type. More...
 
readonly bool HasStoppedThisFrame (FrameBase frame)
 Has the timer expired during this frame. More...
 
readonly bool IsRunning (FrameBase frame)
 Is the timer set and still running. More...
 
readonly int RemainingFrames (FrameBase frame)
 How many frames are remaining until the timer expires. More...
 
readonly FP RemainingSeconds (FrameBase frame)
 How much time is remaining until the timer expires in seconds. More...
 
void Restart (FrameBase frame)
 Restarts the timer with the same Duration. More...
 

Static Public Member Functions

static FrameTimer FromFrames (FrameBase frame, int durationInFrames)
 Create a new timer from a delay in frames. More...
 
static FrameTimer FromSeconds (FrameBase frame, FP durationInSeconds)
 Create a new timer from a delay in seconds. More...
 
static void Serialize (void *ptr, IDeterministicFrameSerializer serializer)
 Serializes a FrameTimer into a FrameSerializer to write or read from a frame snapshot. More...
 

Public Attributes

readonly int Duration => TargetFrame - StartFrame
 The duration of the timer in frames, TargetFrame - StartFrame. More...
 
readonly bool IsSet => TargetFrame >= StartFrame && StartFrame > 0
 If the timer is has been set. A valid timer has a StartFrame greater than 0 and the target frame TargetFrame greater than or equal to the StartFrame. More...
 
int StartFrame
 The frame number at which the timer has started. More...
 
int TargetFrame
 The frame number at which the timer will run out. More...
 

Static Public Attributes

static readonly FrameTimer None = default
 Empty timer. More...
 
const int SIZE = sizeof(int) * 2
 The size of the component (or struct/type) in-memory inside the Frame data-buffers or stack (when passed as value parameter). Not related to the snapshot payload this occupies, which is bit-packed and compressed. More...
 

Detailed Description

Built-in timer struct for game logic that counts frames, it is not affected by FrameBase.GameTimeScale (see GameTimer). Seconds are converted with the simulation rate (FrameBase.UpdateRate).

Member Function Documentation

◆ DurationNormalized()

readonly FP Quantum.FrameTimer.DurationNormalized ( FrameBase  frame)
inline

The progress of the timer between 0 (just started) and 1 (expired), e.g. to display a cooldown.

Parameters
frameThe current Frame instance.
Returns
The elapsed frames divided by the Duration, clamped to [0, 1]. 1 for a set timer with a duration of 0, 0 if the timer is not set.

◆ IsRunning()

readonly bool Quantum.FrameTimer.IsRunning ( FrameBase  frame)

Is the timer set and still running.

Parameters
frameThe current Frame instance.
Returns
true if the timer is valid and the current frame > number is smaller than TargetFrame. false otherwise.

◆ HasStoppedThisFrame()

readonly bool Quantum.FrameTimer.HasStoppedThisFrame ( FrameBase  frame)

Has the timer expired during this frame.

Parameters
frameThe current Frame instance.
Returns
true if the timer is not expired and the current frame number is equal to TargetFrame. false otherwise.

Useful for one time operations that happen only at timer expiry.

◆ RemainingFrames()

readonly int Quantum.FrameTimer.RemainingFrames ( FrameBase  frame)
inline

How many frames are remaining until the timer expires.

Parameters
frameThe current Frame instance.
Returns
TargetFrame - current Frame number. Never returns a negative value.

◆ RemainingSeconds()

readonly FP Quantum.FrameTimer.RemainingSeconds ( FrameBase  frame)
inline

How much time is remaining until the timer expires in seconds.

Parameters
frameThe current Frame instance.
Returns
Divides the remaining frame count by the simulation rate (FrameBase.UpdateRate). Never returns a negative value.

◆ ElapsedFrames()

readonly int Quantum.FrameTimer.ElapsedFrames ( FrameBase  frame)
inline

How many frames or ticks since the timer was started.

Parameters
frameThe current Frame instance.
Returns
Returns current frame number - the start frame number. Never returns a negative value.

◆ ElapsedSeconds()

readonly FP Quantum.FrameTimer.ElapsedSeconds ( FrameBase  frame)
inline

How long since the timer has started in seconds.

Parameters
frameThe current Frame instance.
Returns
Divides the elapsed frame count by the simulation rate (FrameBase.UpdateRate). Never returns a negative value.

◆ Restart()

void Quantum.FrameTimer.Restart ( FrameBase  frame)
inline

Restarts the timer with the same Duration.

Parameters
frameThe current Frame instance.

◆ FromSeconds()

static FrameTimer Quantum.FrameTimer.FromSeconds ( FrameBase  frame,
FP  durationInSeconds 
)
inlinestatic

Create a new timer from a delay in seconds.

The seconds are converted with the simulation rate (FrameBase.UpdateRate) instead of dividing by FrameBase.UnscaledDeltaTime, so whole seconds are exact whole frames at any rate (e.g. 1 second at 60 Hz is exactly 60 frames, 1/60 cannot be stored exactly by FP).

Parameters
frameThe current Frame instance.
durationInSecondsThe time in seconds to wait before the timer expires, rounded up to whole frames.
Returns
A new FrameTimer instance with the specified duration.

◆ FromFrames()

static FrameTimer Quantum.FrameTimer.FromFrames ( FrameBase  frame,
int  durationInFrames 
)
inlinestatic

Create a new timer from a delay in frames.

Parameters
frameThe current Frame instance.
durationInFramesThe number of frames to wait before the timer expires.
Returns
A new FrameTimer instance with the specified duration.

◆ Serialize()

static void Quantum.FrameTimer.Serialize ( void *  ptr,
IDeterministicFrameSerializer  serializer 
)
inlinestatic

Serializes a FrameTimer into a FrameSerializer to write or read from a frame snapshot.

Parameters
ptrThe pointer to the FrameTimer.
serializerThe FrameSerializer instance into which the struct will be serialized.

◆ GetHashCode()

readonly override int Quantum.FrameTimer.GetHashCode ( )
inline

Overrides the hash code generation of this type.

Returns
A hash code of the current state of this instance.

Member Data Documentation

◆ SIZE

const int Quantum.FrameTimer.SIZE = sizeof(int) * 2
static

The size of the component (or struct/type) in-memory inside the Frame data-buffers or stack (when passed as value parameter). Not related to the snapshot payload this occupies, which is bit-packed and compressed.

◆ None

readonly FrameTimer Quantum.FrameTimer.None = default
static

Empty timer.

◆ TargetFrame

int Quantum.FrameTimer.TargetFrame

The frame number at which the timer will run out.

◆ StartFrame

int Quantum.FrameTimer.StartFrame

The frame number at which the timer has started.

◆ IsSet

readonly bool Quantum.FrameTimer.IsSet => TargetFrame >= StartFrame && StartFrame > 0

If the timer is has been set. A valid timer has a StartFrame greater than 0 and the target frame TargetFrame greater than or equal to the StartFrame.

◆ Duration

readonly int Quantum.FrameTimer.Duration => TargetFrame - StartFrame

The duration of the timer in frames, TargetFrame - StartFrame.