-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBasicEnv.js
98 lines (98 loc) · 2.11 KB
/
BasicEnv.js
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
function sleep(ms) {
return new Promise(function(resolve) {setTimeout(resolve, ms)});
}
exports.display = function(hud, PObject, actions) {
return function(x) {
hud.Output(x+"");
}
}
exports._move = function(hud, PObject, actions) {
return function(x, y, z, speed) {
PObject.MoveCharacter(x, y, z, speed)
}
}
exports._turn = function(hud, PObject, actions) {
return function(angle) {
PObject.Turn(angle);
}
}
exports.turn_left = function(hud, PObject, actions) {
return function(angle) {
async function x() {
PObject.Turn(-Math.PI/2);
await sleep(500);
return new Promise(function(resolve) {
resolve();
});
}
if (actions.running) {
actions.list.unshift(x);
} else {
actions.list[actions.list.length] = x;
}
}
}
exports.turn_right = function(hud, PObject, actions) {
return function(angle) {
async function x() {
PObject.Turn(Math.PI/2);
await sleep(500);
return new Promise(function(resolve) {
resolve();
});
}
if (actions.running) {
actions.list.unshift(x);
} else {
actions.list[actions.list.length] = x;
}
}
}
exports._walk = function(hud, PObject, actions) {
return function(distance, speed) {
PObject.Walk(distance, speed);
}
}
exports.walk = function(hud, PObject, actions) {
return function(distance) {
async function x() {
PObject.Walk(distance, 100);
await sleep(distance * 10);
return new Promise(function(resolve) {
resolve();
});
}
if (actions.running) {
actions.list.unshift(x);
} else {
actions.list[actions.list.length] = x;
}
}
}
exports._remove = function(hud, PObject, actions) {
async function exec(resolve) {
if (actions.list.length === 0) {
actions.running = false;
resolve();
} else {
actions.running = true;
try {
await (actions.list.shift(1))();
exec(resolve);
} catch (e) {
hud.Output(e + "");
actions.running = false;
actions.list = [];
resolve();
}
}
}
function init() {
return new Promise(
function(resolve){
exec(resolve);
}
);
}
return init;
}