1.0.0 • Published 8 years ago

warriorjs-engine v1.0.0

Weekly downloads
7
License
MIT
Repository
github
Last release
8 years ago

banner

Travis npm

WarriorJS Engine

There's a small chance that you have played the game WarriorJS before, and an even smaller chance that you have come here intentionally, so you may be wondering what's this all about...

This is the rules engine behind WarriorJS, distributed as a standalone npm package.

Installation

$ npm install --save warriorjs-engine

API Reference

The WarriorJS Engine API exposes a single yet important function:

playLevel(levelConfig, playerCode, [maxTurns])

Plays a WarriorJS level using the player's code.

Arguments

  1. levelConfig (Object): The configuration of the level, with the following members:
  • timeBonus (Number): Amount of turns the player has to complete the level obtaining bonus points.
  • floor (Object): The floor of the level, with the following members:
    • size (Object): The size of the floor.
    • stairs (Object): The position of the stairs.
    • warrior (Object): The player's warrior.
    • units (Array): The other units in the level.
  1. playerCode (String): The code written by the player.
  2. [maxTurns] (Number): The maximum number of turns that will be played.

Returns

(Object) An object containing the play result with the following members:

  • passed (Boolean): Whether the level was passed or not.
  • score (Object):
    • warrior (Number): The points earned by the warrior by killing units and rescuing captives.
    • timeBonus (Number): A bonus for completing the level in less than a specified amount of turns.
    • clearBonus (Number): A bonus for defeating all enemies and rescuing all captives.
  • events (Array): The sequence of events that took place during the play.

Example

import playLevel from 'warriorjs-engine';

const levelConfig = {
  timeBonus: 15,
  floor: {
    size: {
      width: 8,
      height: 1
    },
    stairs: {
      x: 7,
      y: 0
    },
    warrior: {
      name: 'Spartacus',
      x: 0,
      y: 0,
      facing: 'east',
      abilities: [
        {
          name: 'walk',
          args: []
        },
        {
          name: 'attack',
          args: []
        },
        {
          name: 'feel',
          args: []
        }
      ]
    },
    units: [
      {
        type: 'sludge',
        x: 4,
        y: 0,
        facing: 'west'
      }
    ]
  }
};

const playerCode = `
  class Player {
    playTurn(warrior) {
      if (warrior.feel().isEnemy()) {
        warrior.attack();
      } else {
        warrior.walk();
      }
    }
  }
`;

const { passed, score, events } = playLevel(levelConfig, playerCode);
2.0.0-alpha.4

8 years ago

2.0.0-alpha.3

8 years ago

2.0.0-alpha.2

8 years ago

2.0.0-alpha.1

8 years ago

1.0.0

8 years ago

0.11.0

8 years ago

0.10.2

8 years ago

0.10.1

8 years ago

0.10.0

8 years ago

0.9.0

8 years ago

0.8.1

8 years ago

0.8.0

8 years ago

0.7.0

8 years ago

0.6.0

8 years ago

0.5.0

8 years ago

0.4.2

8 years ago

0.4.1

8 years ago

0.4.0

8 years ago

0.3.0

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago