0.1.0-alpha.43 • Published 7 months ago

moroboxai-game-sdk v0.1.0-alpha.43

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

moroboxai-game-sdk

NPM version Node.js CI gitHub license Code Quality: Javascript Total Alerts

This package is for you if you want to build a game for MoroboxAI and allow others to code their own AI for it.

Introduction

There are barely any requirements on how you should write your game logic, or what third party libraries you should use; but for your game to interface correctly with MoroboxAI and AIs written by the community, you have to implement some basic callbacks and functions in your game.

This package is meant to provide a common interface for all MoroboxAI games so that they all provide essential features such as:

  • Showing instructions on how to code an AI for the game.
  • Playing, pausing, and stopping the game.
  • Displaying runtime informations to help coding an AI.
  • Read output data from the current frame.
  • Write input data for the next frame.

This ensure that your game will run correctly on MoroboxAI and that the community will be able to code new AIs for it.

Install

Using npm:

npm install moroboxai-game-sdk --save-dev

Log installed SDK version to console:

import * from MoroboxAIGameSDK from 'moroboxai-game-sdk';

console.log(`MoroboxAIGameSDK v${MoroboxAIGameSDK.VERSION}`);

Minimal Node.js project

We will setup a simple Node.js project for writing a game that can be run in MoroboxAI. As moroboxai-game-sdk is written using TypeScript, we will also configure the project to be compiled with TypeScript.

cd my/game
npm init

Install typescript and moroboxai-game-sdk:

npm install typescript moroboxai-game-sdk --save-dev

Note: moroboxai-game-sdk is only required in development so that typescript knows about the types. At runtime, the SDK will be initialized and provided directly by MoroboxAI, so there is no need to include it in your game.

Add the following configuration to package.json:

"scripts": {
    "build": "tsc"
}

Create a tsconfig.json file containing:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es2019",
        "lib": [
            "es2019",
            "es2019.object",
            "dom"
        ],
        "outDir": "./",
        "strict": true
    },
    "files": ["src/game.ts"],
    "exclude": ["node_modules"]
}

This will tell TypeScript to compile only src/game.ts without unnecessary files from node_modules. Also, your game will be compiled as a CommonJS module.

Write your game

Add a new src/game.ts file to the project:

import * as MoroboxAIGameSDK from 'moroboxai-game-sdk';

class Game implements MoroboxAIGameSDK.IGame {
    private _player: MoroboxAIGameSDK.IPlayer;
    
    constructor(player: MoroboxAIGameSDK.IPlayer) {
        this._player = player;

        // start the process of loading assets asynchronously
        setTimeout(() => {
            console.log("assets loaded");

            // notify the game is ready to play
            player.ready();
        }, 1000);
    }

    help(): string {
        return "";
    }

    play(): void {
        console.log("play");
    }

    pause(): void {

    }

    stop(): void {

    }

    resize(): void {

    }    
}

export function boot(player: MoroboxAIGameSDK.IPlayer): MoroboxAIGameSDK.IGame {
    return new Game(player);
}

IPlayer is a the interface your game must implement to be compatible with MoroboxAI. The boot function is required and must be exported at the end of the script. This will be the entrypoint used by MoroboxAI to boot your game.

Build

npm run build

This will generate a game.js file containing your game exported as a CommonJS module:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.boot = void 0;
class Game {
    constructor(player) {
        this._player = player;
        setTimeout(() => {
            console.log("assets loaded");
            // notify the game is loaded and ready
            player.ready();
        }, 1000);
    }
    help() {
        return "";
    }
    play() {
        console.log("play");
    }
    pause() {
    }
    stop() {
    }
    resize() {
    }
}
function boot(player) {
    return new Game(player);
}
exports.boot = boot;

You can see that moroboxai-game-sdk has been stripped out and that the boot function is correctly exported at the end.

Package & distribute

For MoroboxAI to know how to run your game, you must add an header.json file containing some metadata:

{
    "id": "00000000-0000-0000-0000-000000000000",
    "version": "1.0.0",
    "title": "Game",
    "author": "yourname",
    "description": "Some description.",
    "boot": "game.js"
}

Now you have two possibilities for distributing your game:

  • Upload header.json and game.js on a server and indicate the URL to MoroboxAI
  • Upload a ZIP containing header.json and game.js on a server and indicate the URL to MoroboxAI

Test

There are multiple ways of testing your game in MoroboxAI, but the simplest one is probably to start a local HTTP server to locally run MoroboxAI.

First install moroboxai-player-web and http-server:

npm install moroboxai-player-web http-server --save-dev

Then create an index.html file:

<html>
 <div id="player"></div> 
  
 <script type="text/javascript" src="node_modules/moroboxai-player-web/lib/umd/moroboxai-player-web.js"></script>
 <script type="text/javascript">
  // Initialize the player on our div
  const player = MoroboxAIPlayer.init({
   element: document.getElementById("player"),
   url: "./",
   width: "256px",
   height: "256px"
  });

  // Will be called when the game is ready
  player.onReady = () => console.log("game is loaded and ready");
 </script>
</html>

Open a command prompt and run:

http-server

Now you can access the page on localhost and the port opened by http-server. You won't see anything as the game is not displaying anything, but you can check the game is correctly loaded in the console.

License

This content is released under the MIT License.

0.1.0-alpha.30

8 months ago

0.1.0-alpha.32

8 months ago

0.1.0-alpha.31

8 months ago

0.1.0-alpha.34

8 months ago

0.1.0-alpha.33

8 months ago

0.1.0-alpha.36

8 months ago

0.1.0-alpha.35

8 months ago

0.1.0-alpha.38

8 months ago

0.1.0-alpha.37

8 months ago

0.1.0-alpha.39

7 months ago

0.1.0-alpha.41

7 months ago

0.1.0-alpha.40

7 months ago

0.1.0-alpha.43

7 months ago

0.1.0-alpha.42

7 months ago

0.1.0-alpha.27

9 months ago

0.1.0-alpha.26

9 months ago

0.1.0-alpha.29

8 months ago

0.1.0-alpha.28

8 months ago

0.1.0-alpha.16

9 months ago

0.1.0-alpha.18

9 months ago

0.1.0-alpha.17

9 months ago

0.1.0-alpha.19

9 months ago

0.1.0-alpha.21

9 months ago

0.1.0-alpha.20

9 months ago

0.1.0-alpha.23

9 months ago

0.1.0-alpha.22

9 months ago

0.1.0-alpha.25

9 months ago

0.1.0-alpha.24

9 months ago

0.1.0-alpha.12

2 years ago

0.1.0-alpha.11

2 years ago

0.1.0-alpha.14

2 years ago

0.1.0-alpha.13

2 years ago

0.1.0-alpha.15

2 years ago

0.1.0-alpha.10

2 years ago

0.1.0-alpha.7

2 years ago

0.1.0-alpha.9

2 years ago

0.1.0-alpha.8

2 years ago

0.1.0-alpha.5

3 years ago

0.1.0-alpha.6

3 years ago

0.1.0-alpha.4

3 years ago

0.1.0-alpha.3

3 years ago

0.1.0-alpha.2

3 years ago

0.1.0-alpha.1

3 years ago