Handles ticks/frames
requestAnimationFrame()
is used to call a function before the next browser repaint --- this function performs all updates to the state of the game (hopefully) inbetween repaints- Every time an update happens, we record the timestamp in
InternalTimer.lastTick_ms
- When we next update, we see how much time has elapsed since the last update
- From this, we can calculate how many of our ideal-world (
GAME.FRAME_MS
) frames have passed - If we aren't quite keeping up with the ideal frame rate, eg 2 frames' worth of time has passed since our last update, we can try to keep things looking smooth by moving obstacles twice as far as during a single-frame update
- License
- GPL-3.0-only
- Source
Methods
(static) init()
- Source
Main init
(static) nextTick()
- Source
Call requestAnimationFrame()
for the next opportunity to run InternalTimer.tick()
(static) secondsToFrames(_secs) → {number}
- Source
Convert seconds to number of frames
Eg when an animation wants to run for 10secs, how many frames should it last for?
Parameters
Name | Type | Description |
---|---|---|
_secs |
number | How many seconds to convert |
Returns
number |
The estimated number of frames it will take for |
(static) startTicking()
- Source
Start the internal clock
(static) tick(_timestamp_ms)
- Source
This function is given as a callback for requestAnimationFrame()
- Calculate how many frames have passed since the last tick
- Increment the internal frame count variable
- Call the main game update function
Game.updateByFrameCount()
, passing it the number of frames - Start the next tick
Parameters
Name | Type | Description |
---|---|---|
_timestamp_ms |
DOMHighResTimeStamp | The time of the previous frame's rendering (automatically passed in by |
(static) updateFpsAverage(_latestFps)
- Source
Keep a rolling average of how many frames per second we are achieving
- On each
InternalTimer.tick()
an estimate of FPS for the latest frame is calculated and passed in to this function - The most recent few (
GAME.FPSDISPLAY_AVERAGE_FRAMES_SPAN
) fps timings are kept in an array - The array is summed and the sum is divided by the array length to get an average (mean) which is stored as
InternalTimer.currentFps
Parameters
Name | Type | Description |
---|---|---|
_latestFps |
number | The most recent FPS reading |