Registers a game system: code that belongs to the whole game rather than to a scene. Music that
goes on between levels, keys that work everywhere (M to mute, F for full screen), achievements, the
time played, analytics. Each game that starts after this runs fn once, before its first scene, in
a box of its own that no change of scene touches, and tears it down when the game ends, its
useCleanup with it. Called in main.ts before createGame; a plugin exports an install function
that calls it.
It runs whatever scene the game starts on: a level played straight from the editor, a game made with
initialScene, a test that starts on the third level. A scene that launches the others would be
missing in all three.
In each frame, the systems' useUpdate run before any scene's, and their useLateUpdate after
every scene's: a system gets things ready for the scenes and reads where they left them. A paused
game pauses its systems as well (useGame().onPaused tells one so); a paused scene does not.
A system belongs to no scene, so the hooks that need one throw inside it (the cameras, the lights,
physics, useRenderStyle, useFog): what concerns the whole game is useGame()'s, and what lasts
goes in a store. useScene works in it for the scenes it names (launch('Hud'), pause('Level')),
and throws for what would be about a scene of its own: change, and the calls with no name. It
draws nothing: what it creates is not seen, so something always on screen (a score) is a scene
launched beside the levels; launched from its body or its useReady, before the game has a scene,
it comes on the first frame the game is shown, over the scene the game starts on, the splash
or not. Behind a splash its updates wait, as the scenes' do. One that throws, starting or in a
frame, is reported and the game goes on without it.
The same name with the same function does nothing, so an install called twice does nothing when
the body is made once, at the top of the module, and not inside the install; the same name with
another function throws. Registered while a game is running, it waits for the next game, and says
so once. A plugin starts the name with its own ('nacatamalon-saves'), so two never pick the same.
While a tool lays the game out rather than playing it (an editor's edit mode), its systems are
taken down, and started again, from the ones registered then, when it is played.
Example
import { createGame, createScene, registerGameSystem, useAudio, useKeyboard, useUpdate } from 'nacatamalon';
// M mutes the game wherever the player is. The body is made once, here.
const muteKey = () => {
const keys = useKeyboard();
const audio = useAudio();
useUpdate(() => {
if (keys.justPressed('m')) audio.setVolume(audio.getVolume() > 0 ? 0 : 1);
});
};
const Level1 = () => createScene();
const Level2 = () => createScene();
registerGameSystem('mute-key', muteKey);
createGame('#app', { width: 320, height: 180 })({ Level1, Level2 });