1. Define the components
A component is plain data attached to an entity. Each one is a class with aname 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.inputcomes from the input library, andctx.app.deltais 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
app.useregisters each library. It must happen beforeinit.await app.init(options)initializes every library.graphics.baseLayerandecs.registryexist only after it.- Spawn entities and add systems.
await app.run()starts the game loop.
4. Run it
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
assetsfolder. - Network shows how to add a server and other players.