15.1.1 • Published 9 months ago

@game-vir/multiplayer v15.1.1

Weekly downloads
-
License
(MIT or CC0 1.0)
Repository
github
Last release
9 months ago

@game-vir/multiplayer

A multiplayer service definition that uses WebSockets to connect to a room host and then WebRTC to connect the host to each of its clients.

Game state is synced between all room clients using a lock-step architecture.

Single player game state updates are also supported.

Reference docs: https://electrovir.github.io/game-vir/multiplayer

See the multiplayer connection server here: https://www.npmjs.com/package/@game-vir/multiplayer-server

Install

npm i @game-vir/multiplayer

Usage

import {ControllerFrameEvent, ControllerRoomListEvent, MultiplayerController} from '@game-vir/multiplayer';

type GameAction =
    | {
          action: 'jump';
      }
    | {
          action: 'move';
          direction: 'right' | 'left';
      };

const myController = new MultiplayerController<GameAction>({
    gameId: 'multi',
});

myController.listen(ControllerFrameEvent, (event) => {
    const actions = event.detail;
    /** Take the list of `actions` and apply them to your game state here. */
});
myController.listen(ControllerRoomListEvent, (event) => {
    const rooms = event.detail;
    /** Render a list of available multiplayer rooms to your user so they can select one to join. */
});
myController.startMultiplayer({
    /** The origin of your multiplayer connection server. */
    backendOrigin: 'http://localhost:3000',
});

await myController.joinOrCreateRoom({
    /**
     * If the given room id already exists on the server, this will join it.
     *
     * If not, this will create a new room with the given room id, name, and password requirement.
     */
    roomId: 'some-room-id-goes-here',
    /**
     * Name of the room. This is only used for displaying a human friendly name to clients browsing
     * the current room list.
     */
    roomName: 'name of room',
    /** Set to empty string if there is no password. */
    roomPassword: '',
});

/** Apply actions to your local state and propagate them to all other room clients. */
myController.act({
    action: 'jump',
});

For single player, simply set singleplayer to true:

import {ControllerFrameEvent, MultiplayerController} from '@game-vir/multiplayer';

type GameAction =
    | {
          action: 'jump';
      }
    | {
          action: 'move';
          direction: 'right' | 'left';
      };

const myController = new MultiplayerController<GameAction>({
    gameId: 'single',
});

myController.listen(ControllerFrameEvent, (event) => {
    const actions = event.detail;
    /** Take the list of `actions` and apply them to your game state here. */
});
myController.startSingleplayer();

/** Apply actions to your local state. */
myController.act({
    action: 'jump',
});
15.1.1

9 months ago

15.1.0

9 months ago

15.0.1

9 months ago

15.0.0

9 months ago

14.10.0

9 months ago

14.9.1

9 months ago

14.9.0

9 months ago

14.8.0

9 months ago

14.7.0

9 months ago

14.6.2

9 months ago

14.6.1

9 months ago

14.6.0

9 months ago

14.5.0

9 months ago

14.4.0

9 months ago

14.3.1

9 months ago

14.3.0

9 months ago

14.2.0

9 months ago

14.1.0

9 months ago

14.0.1

9 months ago

14.0.0

9 months ago

13.1.0

9 months ago

13.0.0

9 months ago

12.0.0

9 months ago

11.2.1

9 months ago

11.2.0

9 months ago

11.1.2

10 months ago

11.1.1

10 months ago

11.1.0

10 months ago

11.0.2

10 months ago

11.0.1

10 months ago

11.0.0

10 months ago

10.2.0

10 months ago

10.1.0

10 months ago

10.0.1

11 months ago

10.0.0

11 months ago

9.0.0

11 months ago

8.0.0

11 months ago

7.0.0

11 months ago