/
13_robotMaze.jsx
120 lines (67 loc) · 2.19 KB
/
13_robotMaze.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#BEGIN_PROPERTIES#
{
"commandsIntroduced": ["map.getAdjacentEmptyCells"]
}
#END_PROPERTIES#
/*
* robot.js
*
* Dr. Eval had nowhere to go. The door was locked.
* Fortunately, his trusty robot companion showed up
* just in the nick of time with the key that he needed.
*/
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function startLevel(map) {
map.placePlayer(map.getWidth()-2, map.getHeight()-2);
map.defineObject('robot', {
'type': 'dynamic',
'symbol': 'R',
'color': 'gray',
'inventory': ['key'],
'onCollision': function (player, me) {
me.giveItemTo(player, 'key');
},
'behavior': function (me) {
#BEGIN_EDITABLE#
// move randomly
var moves = map.getAdjacentEmptyCells(me.getX(), me.getY());
// getAdjacentEmptyCells gives array of ((x, y), direction) pairs
me.move(moves[getRandomInt(0, moves.length - 1)][1]);
#END_EDITABLE#
}
});
map.defineObject('barrier', {
'symbol': '░',
'color': 'purple',
'impassable': true,
'passableFor': ['robot']
});
map.defineObject('lock', {
'symbol': String.fromCharCode(0x13cc),
'color': 'gray',
'impassable': function (player) {
return !player.hasItem('key');
}
});
map.placeObject(0, map.getHeight() - 1, 'exit');
map.placeObject(0, map.getHeight() - 2, 'lock');
map.placeObject(1, map.getHeight() - 1, 'lock');
map.placeObject(1, 1, 'robot');
map.placeObject(map.getWidth() - 2, 9, 'barrier');
var autoGeneratedMaze = new ROT.Map.DividedMaze(map.getWidth(), 10);
autoGeneratedMaze.create( function (x, y, mapValue) {
// don't write maze over robot or barrier
if ((x == 1 && y == 1) || (x == map.getWidth() - 2 && y == 9)) {
return 0;
} else if (mapValue === 1) { //0 is empty space 1 is wall
map.placeObject(x,y, 'block');
} else {
map.placeObject(x,y,'empty');
}
});
}
function validateLevel(map) {
validateExactlyXManyObjects(map, 1, 'exit');
}