0.1.11 • Published 7 months ago

bridge-1100 v0.1.11

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

Bridge 1100

npm

A simplified and type-safe interface to easily bridge between Brick 100 and external games/apps.

How it works

How it works

The interface allows you to send and receive messages in form of events between the Brick 1100 and your app.

From Brick 1100's perspective, the interface allows it to emit operational events to your app such as key presses, start trigger, etc. and listen to any event emitted from your app.

From your app's perspective, the interface allows you to listen to the operational events from Brick 1100 to control your game/app and send back events to notify Brick 1100 of any changes in your game/app state.

Installation

CDN

Simply add the below line in your HTML file's <body> tag.

<body>
  ...
  <script src="https://unpkg.com/bridge-1100/dist/index.umd.js"></script>
</body>

A window.bridge object will then be available to use.

npm

npm install bridge-1100

# or

yarn add bridge-1100

Usage

import bridge from 'bridge-1100';

bridge.on('keypress', (key) => {
  console.log(key)
});

bridge.off('keypress');

bridge.send(window.parent, {
  event: 'stop',
  data: { /* data sent back to Brick 1100 */ },
});

Styling

The interface also packs a default stylesheet that can be used to style your app. Simply add the below line in your HTML file's <head> tag.

<head>
  ...
  <link rel="stylesheet" href="https://unpkg.com/bridge-1100/dist/index.css" />
  <link rel="stylesheet" href="https://unpkg.com/bridge-1100/dist/font.css" /> <!-- If you wish to use the same font as Brick 1100 -->
</head>

API

on(...)

bridge.on(event: BridgeEvent, callback: KeyCallback | ShakeCallback | GameloopCallback) => void;

Subscribe to a message event.

event

  • Type: BridgeEvent
  • Description: The event to subscribe to.

callback

off(...)

bridge.off(event: BridgeEvent) => void;

Unsubscribe from a message event.

event

  • Type: BridgeEvent
  • Description: The event to unsubscribe from.

send(...)

bridge.send(target: Window, eventData: BridgeEventData) => void;

Send an event to the target window.

target

  • Type: Window
  • Description: The target window to send the event to. In most cases, this will be window.parent.

eventData

Types

BridgeEvent

Available events: "keypress" | "keyrelease" | "numpress" | "numrelease" | "shake" | "start" | 'pause' | "stop"

KeyCallback

(key: Key) => void;

The callback handler when a key event is received. Available for keypress, keyrelease, numpress, and numrelease events. See Key for available keys.

ShakeCallback

(intensity: ShakeIntensity) => void

The callback handler when a shake event is received. Available for the shake event. See ShakeIntensity for available intensity options.

GameloopCallback

(...args: any[]) => void

The callback handler when a gameloop event is received. Available for the start, pause, and stop events.

BridgeEventData

{ event: BridgeEvent; data: any; }

ShakeIntensity

Available options: "LIGHT" | "MEDIUM" | "HEAVY"

Enums

Key

MemberValue
Power"power"
Ok"ok"
Clear"clear"
Up"up"
Down"down"
Zero0
One1
Two2
Three3
Four4
Five5
Six6
Seven7
Eight8
Nine9
Aste"*"
Hash"#"

See also

0.1.11

7 months ago

0.1.10

7 months ago

0.1.9

8 months ago

0.1.8

8 months ago

0.1.7

8 months ago

0.1.6

8 months ago

0.1.5

8 months ago

0.1.4

8 months ago

0.1.3

8 months ago

0.1.2

8 months ago

0.1.1

8 months ago

0.1.0

8 months ago