Game Loop
The engine has two game classes that share the same underlying loop: BaseGame (headless) and Game (client with rendering).
BaseGame
BaseGame is the headless core used by both the server and the client. It manages:
- Physics world — Rapier3D
Worldinstance - Character controller —
KinematicCharacterControllerfor physics-driven movement - Scene — Three.js
Scene(shared between server and client) - Tick rate — Fixed update rate (default: 30 ticks/second)
- World —
BaseWorldinstance managing all entities - Active scene — The scene loaded with
loadScene()(activeScene)
The physics world and character controller are only set when you pass a Rapier world in, see Physics.
import BaseGame from '@mavonengine/core/BaseGame'
class MyGame extends BaseGame {
// ...
}
const game = MyGame.instance()
Tick vs update
| Method | Called by | Purpose |
|---|---|---|
tick() | Fixed interval (tickRate) | Physics step, networked state sync |
update(delta) | Each frame / tick | Entity updates, physics simulation |
Each tick triggers tickComplete with how long it took in milliseconds.
Registering update callbacks
game.onUpdate((delta) => {
// Called every frame/tick with delta time in seconds
})
// Unregister when no longer needed
game.unregisterOnUpdate(callback)
Environment checks
game.isDevMode() // true when NODE_ENV !== 'production'
game.isProductionMode() // true when NODE_ENV === 'production'
Game
Game extends BaseGame for the client. In addition to everything in BaseGame, it sets up:
- Canvas —
HTMLCanvasElementfor WebGL rendering - UI root —
HTMLDivElementfor overlay UI - Sizes — Window size tracking with resize handling
- Camera —
PerspectiveCamerawithAudioListener - Renderer —
WebGLRendererwith shadow mapping - Resources — Asset loader
- InputManager — Keyboard, mouse and gamepad input
- ParticleSystem — Particle effects
- LoadingScreen — Asset loading progress screen
- Debug — Tweakpane debug panel (activated via
#debug) - Editor — In-dev editor (dev mode only)
- Plugins — The engine plugins set with
Game.usePlugins() - Network —
game.network, theNetworkManageronce one is created - Local player —
game.localPlayer, the entity this client controls, if your game sets it (used by dev tools)
import Game from '@mavonengine/core/Game'
const game = new Game(
sources, // Asset[] to load on start
physicsWorld, // optional RAPIER.World
renderParameters // optional THREE.WebGLRendererParameters
)
// Canvas and UI are automatically appended to document.body
Render loop vs tick
The render loop runs via requestAnimationFrame and renders the scene and particles. The game update(delta) — input, physics, entities, plugins, camera — runs on the fixed tick, like on the server. So capping the frame rate never slows down the simulation:
game.renderer.maxFps = 60 // 0 = render every frame
Events
| Event | When |
|---|---|
documentReady | The canvas and UI root are in the document |
sceneLoading / sceneLoaded | A scene is being / was loaded (also on reloads) |
editorBoot | The level editor booted |
tickComplete | A tick finished, with its duration in ms |
Singleton pattern
Both classes use a static instance() method. Call it anywhere to get the existing instance or create one on first call.
const game = Game.instance()
// Always returns the same instance