Core Concepts

Game Loop

How BaseGame and Game drive the engine's update and render loops.

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 World instance
  • Character controller — KinematicCharacterController for physics-driven movement
  • Scene — Three.js Scene (shared between server and client)
  • Tick rate — Fixed update rate (default: 30 ticks/second)
  • World — BaseWorld instance 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

MethodCalled byPurpose
tick()Fixed interval (tickRate)Physics step, networked state sync
update(delta)Each frame / tickEntity 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 — HTMLCanvasElement for WebGL rendering
  • UI root — HTMLDivElement for overlay UI
  • Sizes — Window size tracking with resize handling
  • Camera — PerspectiveCamera with AudioListener
  • Renderer — WebGLRenderer with 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, the NetworkManager once 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

EventWhen
documentReadyThe canvas and UI root are in the document
sceneLoading / sceneLoadedA scene is being / was loaded (also on reloads)
editorBootThe level editor booted
tickCompleteA 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