UNPKG

1.5 kBMarkdownView Raw
1# WarriorJS Engine
2
3```javascript
4import Engine from 'warriorjs-engine';
5```
6
7## Engine.playLevel(config, profile, maxTurns)
8
9Plays the level defined in the passed in `config` with the passed in `profile`, allowing `maxTurns` to complete the level (1000 by default). Returning an object with the result (whether it passed the level or not), the points earned and the sequence of events that took place during the play.
10
11```javascript
12Engine.playLevel(config, profile, maxTurns) // => { passed, score, events }
13```
14
15**Example**
16
17```javascript
18const config = {
19 timeBonus: 15,
20
21 floor: {
22 size: {
23 width: 8,
24 height: 1
25 },
26 stairs: {
27 x: 7,
28 y: 0
29 },
30 units: [
31 {
32 type: 'warrior',
33 x: 0,
34 y: 0,
35 facing: 'east',
36 abilities: [
37 {
38 name: 'attack',
39 args: []
40 },
41 {
42 name: 'feel',
43 args: []
44 }
45 ]
46 },
47 {
48 type: 'sludge',
49 x: 4,
50 y: 0,
51 facing: 'west'
52 }
53 ]
54 }
55};
56
57const profile = {
58 playerCode: `
59 class Player {
60 playTurn(warrior) {
61 if (warrior.feel().isEnemy()) {
62 warrior.attack();
63 } else {
64 warrior.walk();
65 }
66 }
67 }
68 `,
69 warriorName: 'Spartacus',
70 abilities: [
71 {
72 name: 'walk',
73 args: []
74 }
75 ]
76};
77
78const MAX_TURNS = 120;
79
80const { passed, score, events } = Engine.playLevel(config, profile, MAX_TURNS);
81```