The application
Your game’ssrc/main.ts exports a main function. The NanoForge loader calls it with the run options: the page element to draw in (container, client only), the game files (files) and the environment variables (env). You pass these options on to the application.
You create the application with NanoforgeFactory from @nanoforge-dev/core:
The
viewport option sets the game’s design resolution (width and height, default 1920 by 1080) and how it fits the window (fit: "contain" by default, "cover" or "fill"). Game coordinates are always in that resolution; the engine scales the canvas to the window.
Every application has the same three steps:
app.use throws once init has resolved: register every library before. Libraries are ready only after init resolves.
Libraries
Everything the engine does, it does through a library: the ECS, graphics, input, audio, networking and scenes are all libraries. You register the ones your game needs withapp.use, and each one adds a key to the context:
Two libraries can’t share a key: registering both graphics libraries throws.
The context
The context (Context from @nanoforge-dev/common) is the object every system and library receives each tick. It always has:
ctx.app: the loop state and controls.delta(milliseconds since the last tick),tickRate,isRunning,isPaused, andrequestStop(),requestPause(),requestResume(),requestStep().ctx.assets: the game files.ctx.assets.getAsset("/hero.png"). See Assets.ctx.vars: a key/value store shared by every library and system for the whole run.ctx.viewport: on the client only, the design resolution and conversions between screen and game coordinates.
The game loop
app.run() starts a loop that runs tickRate times per second. Each library has four hooks, called in this order:
__init, once, duringapp.init. A library that lists another in itsdependenciesis initialized after it.__events, every tick, even when the game is paused.__run, every tick unless the game is paused. This is whereEcsLibraryruns your systems.__clear, once, afterctx.app.requestStop()stops the loop.
Entities, components and systems
The game state lives in the ECS registry,ctx.ecs.registry (or ecs.registry in main):
- an entity is an id, made with
registry.spawnEntity(); - a component is a data object attached to an entity with
registry.addComponent(entity, component); - a system is a function
(registry, ctx) => void, added withregistry.addSystem(system)and called on every tick.
Client and server
A game can run as a client alone, or as a client plus a server:- The client runs in the browser. It draws, plays sounds and reads input. The graphics, input and audio libraries work only there.
- The server runs on Bun. It has no screen: it owns the shared game state and sends it to the clients. It uses
EcsLibraryfrom@nanoforge-dev/ecs/serverandNetworkServerLibrary. - Both sides share their packet definitions through a common file. See Network, and Peer-to-peer to run the server inside a player’s browser instead.
Writing your own library
A library is a class that extendsLibrary from @nanoforge-dev/common. It declares its context key, then overrides the hooks it needs and expose(), which returns what ctx.[key] gives to game code:
app.use(new ScoreLibrary()). Systems can then call ctx.score.add(1).
- The
declare moduleblock typesctx.score.defineLibraryKeyonly accepts a key declared there. - Return a plain object from
expose(), neverthis, so game code can’t call the hooks. - To set the order, pass relationships to
super()in the constructor:dependenciesare initialized first,runBeforelibraries run before this one each tick,runAfterlibraries run after it. InrunBeforeandrunAfter,"*"means every other library;dependenciesonly takes library keys.