Advanced • Lesson 7

WebGL2, 2D Lighting, i18n & RPG Dialog

Take your singleplayer browser game from flat daytime sprites to atmospheric dungeons with dynamic torches, GPU-batched WebGL2 rendering, multi-language support, and branching RPG dialog boxes.

Without Lighting, i18n & Dialog Runners

Darkening a room by hand with semi-transparent rectangles makes everything dull without real light falloff. Hardcoding English strings inside every NPC makes translating your game later nearly impossible, and hand-rolling typewriter timers + branching choices quickly turns into nested if/else spaghetti.

Why Maverick Built These Tools

Lighting2D paints ambient darkness and punches out soft glowing point lights. LocalizationManager swaps whole languages with one call. UIDialogBox + DialogRunner handle typewriter text and branching choices, while renderer: 'webgl2' batches GPU quads with automatic Canvas 2D fallback.

1. Opt-In WebGL2 & Backend-Agnostic Primitives

Standard Canvas 2D draws shapes one-by-one on the CPU. Turning on renderer: 'webgl2' batches sprites on the GPU—and if a player's device doesn't support WebGL2, Maverick automatically falls back to Canvas 2D! Use Viewport.drawRect(), drawCircle(), drawLine(), and setBlendMode() so your drawing code works on both backends.

const game = new MyGame({
  name: 'Dungeon',
  viewport: { width: 560, height: 260, renderer: 'webgl2', bgColor: '#070b12' },
});

// Inside onDraw():
this.Viewport.setBlendMode('additive');
this.Viewport.drawRect(80, 60, 48, 48, '#00d4ff');
this.Viewport.setBlendMode('normal');

2. Dynamic 2D Lighting (Lighting2D)

Think of Lighting2D like turning off the lights in a dark room and handing your hero a flashlight. You set how dark the room is (ambientAlpha), then place glowing point lights (torches, spells, campfires) that carve soft circles of visibility out of the darkness.

import { Lighting2D } from 'maverick';

const lighting = new Lighting2D({ ambientColor: '#050814', ambientAlpha: 0.82 });
const torch = lighting.addLight({ id: 'heroTorch', x: 200, y: 120, radius: 110, color: '#ff8c1a' });

// In onDraw(), after drawing the world:
lighting.draw(this.Viewport, this.cam);

3. Localization (LocalizationManager) & Typewriter Dialog

LocalizationManager works like a dictionary book: you give it a short key like 'npc.greet' and it looks up the matching sentence in English, Spanish, or Japanese—automatically filling in variables like {player}. Pair it with UIDialogBox and DialogRunner for classic RPG conversations.

import { DialogRunner, LocalizationManager, UIDialogBox } from 'maverick';

const i18n = new LocalizationManager('en');
i18n.loadLocale('en', { npc: { greet: 'Stay close to the torch, {name}!' } });
i18n.loadLocale('es', { npc: { greet: '¡Quédate cerca de la antorcha, {name}!' } });

const box = new UIDialogBox({ id: 'dlg', bounds: { x: 24, y: 164, width: 512, height: 84 }, charsPerSecond: 36 });
this.UI.add(box);
const runner = new DialogRunner(box, [
  { id: 'intro', speaker: 'Keeper', text: i18n.t('npc.greet', { name: 'Hero' }) },
]);
runner.start('intro');
Live Proof of Concept — Lighting2D, LocalizationManager & UIDialogBox
Move your mouse over the dungeon to aim the torch! Click the dialog box (or press Enter) to skip/advance.