Skip to main content
This page builds a complete client-only game: a square on a dark background that you move with the arrow keys. It uses the ECS, 2D graphics and input libraries, which is all most 2D games start from. You need a project first. If you don’t have one, follow Create a project with TypeScript and no server.

1. Define the components

A component is plain data attached to an entity. Each one is a class with a name field equal to its class name: the ECS uses that name to find the component again. Replace the files in src/components with one file, src/components/components.ts:
src/components/components.ts
Drawable holds a Konva shape from @nanoforge-dev/graphics-2d. The shape is what you see; Position is where the game thinks the entity is.

2. Write the systems

A system is a function the engine calls on every tick with the registry and the context. It reads and changes components. registry.getZipper([A, B]) returns every entity that has both components, as objects keyed by component name. Replace the files in src/systems with src/systems/systems.ts:
src/systems/systems.ts
  • ctx.input comes from the input library, and ctx.app.delta is the time since the last tick, in milliseconds. Multiplying by it keeps the speed the same at any frame rate.
  • Systems run in the order you add them: read input, then move, then draw.

3. Write the entry point

main creates the application, registers the libraries, spawns the player entity and starts the game loop. Replace src/main.ts:
src/main.ts
The order matters:
  1. app.use registers each library. It must happen before init.
  2. await app.init(options) initializes every library. graphics.baseLayer and ecs.registry exist only after it.
  3. Spawn entities and add systems.
  4. await app.run() starts the game loop.
The viewport sets the game’s own resolution, 800 by 600 here. Positions are in these game units, and the engine scales the canvas to fit the browser window.

4. Run it

Open the address nf dev prints and press the arrow keys: the square moves and stops at the edges.

What’s next

  • Core concepts explains the application, libraries, the context and the game loop.
  • ECS covers entities, components and systems in more depth.
  • Graphics 2D, Input and Sound and music cover the libraries this game uses, and the ones it could add.
  • Assets shows how to load images and sounds from the assets folder.
  • Network shows how to add a server and other players.