A multiplayer game with Colyseus for websockets multiplayer messaging. The game logic is carried out server-side.
This scene shows you:
- How to support Colyseus in a Decentraland scene
- How to set up a simple game logic in Colyseus
- How to handle game state changes from the server in your scene
- How to fetch a player's realm
- How to keep track of the state of each realm as a separate room in your game
- How to handle team formation, scoring and time limits from the server
The server takes care of organizing players into teams and only starting a match when there are players on both sides.
Each scene runs its own calculations of the current remaining time and the scores of each team, but so does the server. In the end, the server has the final word about when the match is over and what is the final result.
Install the CLI
Download and install the Decentraland CLI by running the following command:
$ npm i -g decentraland
Run the server locally
$ cd server
$ npm run build
$ npm run start
NOTE: If this is your first time running the scene then you need to run npm install
before npm start
Run the scene
Keep the server running, run the scene on a separate command line window:
$ cd scene
$ dcl start
Scene Usage
Open two separate browser windows, and direct one player to the Blue tile and another to the Red tile, so that there are players in both teams.
Install colyseus.js
:
npm install --save colyseus.js
Add colyseus.js
to your "bundleDependencies"
in your package.json
:
"bundleDependencies": [
"colyseus.js"
]
To avoid TypeScript compilation errors you'll need to edit tsconfig.json
, and include a few ///<reference
to your source-code, as you can see in the scene/src/connection.ts file.
{
"compilerOptions": {
// ...
"noLib": false,
// ...
}
}
///<reference lib="es2015.symbol" />
///<reference lib="es2015.symbol.wellknown" />
///<reference lib="es2015.collection" />
///<reference lib="es2015.iterable" />
import { Client } from "colyseus.js";
The Colyseus SDK requires a few TypeScript libraries that are excluded by default by Decentraland.
npm init colyseus-app ./server
Deploying to Colyseus Arena
npm run build
Upload the lib
folder from the Arena control panel.
Learn more about how to build your own scenes in our documentation site.
If something doesn’t work, please file an issue.
This scene is protected with a standard Apache 2 licence. See the terms and conditions in the LICENSE file.