Levels v7 - nodeGame/nodegame GitHub Wiki

Overview

Levels break the game in independent parts, each of which can be played with different synchronization rules and group sizes.

The typical use case consists in having an initial single-player level where the game is explained, then the client reaches the waiting room of the next level where it waits to start the multiplayer game.

Create a New Level

To create a new game level, create a new folder named after the level inside the levels/ directory. For instance to create a level named part2 levels/part2/.

Then, inside the new level directory, add a game/ folder, and optionally a waitroom/ folder, following exactly the same structure as for the corresponding folders at the top-level directory.

Levels Order

The order of the levels must be specified manually. The game begins by running the code inside the game/ folder; this is the default level. Then, clients must be moved to the next level manually.

Code Example to move clients to another levels

A client finishing the default level could be moved to a next level named part2 with the following code.

The client sends a message to the logic:

node.say('level_done');

Upon receiving it, the logic uses the Server API to move the client to next room (usually the waiting room of the next level).

node.on.data('level_done', function(msg) {
    // currentRoom is optional, avoid lookup.
    let currentRoom; // let currentRoom = gameRoom.name; 
    let levelName = 'part2';
    // Move client to the next level.
    // (async so that it finishes all current step operations).
    setTimeout(function() {
        console.log('moving client to next level: ', msg.from);
        channel.moveClientToGameLevel(msg.from, levelName, currentRoom);
    }, 100);
});

Settings and Treatments

Levels in the levels/ folder inherit all the settings and treatments from the default level (inside the top-level game/ folder). In addition, they can also add new settings and treatments or extend existing ones by specifying an own game.settings.js inside the level's game/ directory.

Storing Data to Be Accessible Across Levels

Player data can be stored in the channel registry (you need to know the player id), while other game-related data can be stored directly in the channel object.

Player data

let client = channel.registry.getClient(playerId);
client.userName = 'XXX';

// Alternatively.
channel.registry.updateClient(playerId, { userName: 'XXX' });

Game data

channel.highScore = 1000;

Next

⚠️ **GitHub.com Fallback** ⚠️