This repository has been archived by the owner on Feb 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommandRunner.js
115 lines (105 loc) · 3.3 KB
/
commandRunner.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
var EventEmitter = require("events").EventEmitter;
var util = require("util");
function CommandRunner(key) {
EventEmitter.call(this);
this.key = key;
this.commands = [];
this.baseSpeed = 0;
this.angle = 0;
this.loopTimeoutId = null;
this.customTimeoutIds = {};
this.backgroundTimeoutIds = {};
this.commandFunctions = {
rotate: (config, turn) => {
if (config.isBuiltIn) {
this.stopLoop();
this.clearCustomTimeoutIds();
}
const rotateFunction = () => {
this.angle = (this.angle + turn) % 360;
this.emit("command", "roll", [0, this.angle]);
this.customTimeoutIds.rotate = setTimeout(rotateFunction, 500);
};
rotateFunction();
},
stop: config => {
if (config.isBuiltIn) {
this.stopLoop();
this.clearCustomTimeoutIds();
}
this.emit("command", "roll", [0, this.angle]);
},
dash: (config, baseSpeed, dashTime) => {
if (this.backgroundTimeoutIds.dash !== null) {
clearTimeout(this.backgroundTimeoutIds.dash);
}
this.baseSpeed = baseSpeed;
this.backgroundTimeoutIds.dash = setTimeout(() => {
this.baseSpeed = 0;
}, dashTime * 1000);
},
roll: (config, speed, degree) => {
this.emit("command", "roll", [
this.baseSpeed + speed,
(360 + degree + this.angle) % 360
]);
}
};
}
util.inherits(CommandRunner, EventEmitter);
CommandRunner.prototype.setCommands = function(commands) {
if (commands.length === 1 && commands[0].time === -1) {
// built-in command
this.commandFunctions[commands[0].commandName].apply(this, [{
isBuiltIn: true
}].concat(processArguments(commands[0].args)));
} else {
this.commands = commands;
this.stopLoop();
this.clearCustomTimeoutIds();
this.loopMethod(0);
}
};
CommandRunner.prototype.stopLoop = function() {
if (this.loopTimeoutId !== null) {
clearTimeout(this.loopTimeoutId);
}
};
CommandRunner.prototype.clearCustomTimeoutIds = function() {
Object.keys(this.customTimeoutIds).forEach(timeoutIdName => {
const timeoutId = this.customTimeoutIds[timeoutIdName];
if (timeoutId !== null) {
clearTimeout(timeoutId);
}
});
}
CommandRunner.prototype.loopMethod = function(index) {
if (this.commands.length === 0) {
throw new Error("実行しようとしたcommandsは空でした。: " + this.key);
}
this.clearCustomTimeoutIds();
const currentCommand = this.commands[index];
this.commandFunctions[currentCommand.commandName].apply(this, [{
isBuiltIn: false
}].concat(processArguments(currentCommand.args)));
var nextIndex = index + 1 >= this.commands.length ? 0 : index + 1;
this.loopTimeoutId = setTimeout(() => {
this.loopMethod(nextIndex);
}, currentCommand.time * 1000);
};
// コマンドの引数にmotionSpecialDataがあった場合、それを実際の値に変更する
function processArguments(args) {
return args.map(arg => {
if (typeof arg === "object" && arg.isMotionSpecialData) {
switch (arg.dataName) {
case "randomRange":
return Math.floor(Math.random() * (arg.args[1] - arg.args[0])) + arg.args[0];
case "randomInArray":
return arg.args[0][Math.floor(Math.random() * arg.args[0].length)];
}
} else {
return arg;
}
});
}
module.exports = CommandRunner;