forked from CodingTrain/Logo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commandList.js
190 lines (168 loc) · 3.84 KB
/
commandList.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* This instance of the CommandLookUp class will be the global variable
* that will store all the commands available.
*/
const commandLookUp = new CommandLookUp();
/**
* To add a new command, just need the name, the arguments,
* and then the function to execute.
* Note: When adding a new command, update the list of supported commands on the readme file
*/
commandLookUp.add(
new Command("fd", [new CommandArg("value", ARGUMENT_TYPES.EXPRESSION)], value => {
turtle.forward(value);
})
);
commandLookUp.add(
new Command("bd", [new CommandArg("value", ARGUMENT_TYPES.EXPRESSION)], value => {
turtle.forward(-value);
})
);
commandLookUp.add(
new Command("rt", [new CommandArg("value", ARGUMENT_TYPES.EXPRESSION)], value => {
turtle.right(value);
})
);
commandLookUp.add(
new Command("lt", [new CommandArg("value", ARGUMENT_TYPES.EXPRESSION)], value => {
turtle.right(-value);
})
);
commandLookUp.add(
new Command("pu", [], () => {
turtle.pen = false;
})
);
commandLookUp.add(
new Command("pd", [], () => {
turtle.pen = true;
})
);
commandLookUp.add(
new Command(
"pensize",
[new CommandArg("size", ARGUMENT_TYPES.EXPRESSION)],
size => {
turtle.strokeWeight = size;
}
)
);
commandLookUp.add(
new Command(
"setxy",
[
new CommandArg("x", ARGUMENT_TYPES.EXPRESSION),
new CommandArg("y", ARGUMENT_TYPES.EXPRESSION)
],
(x, y) => {
turtle.x = x;
turtle.y = y;
}
)
);
commandLookUp.add(
new Command("setx", [new CommandArg("x", ARGUMENT_TYPES.EXPRESSION)], x => {
turtle.x = x;
})
);
commandLookUp.add(
new Command("sety", [new CommandArg("y", ARGUMENT_TYPES.EXPRESSION)], y => {
turtle.y = y;
})
);
commandLookUp.add(
new Command("home", [], () => {
turtle["home"]();
})
);
commandLookUp.add(
new Command("radians", [], () => {
angleMode(RADIANS);
})
);
commandLookUp.add(
new Command("degrees", [], () => {
angleMode(DEGREES);
})
);
commandLookUp.add(
new Command(
"repeat",
[
new CommandArg("lengthLoop", ARGUMENT_TYPES.INT),
new CommandArg("commands", ARGUMENT_TYPES.COMMANDS)
],
function(lengthLoop, commands) {
for (let i = 0; i < lengthLoop; i++) {
for (let cmd of commands) {
cmd.execute(i + 1);
}
}
}
)
);
/**
* Color, added as example. Given a value, it set the stroke.
*/
commandLookUp.add(
new Command("color", [new CommandArg("color", ARGUMENT_TYPES.STR,(str)=>{
return /^#?([0-9A-Fa-f]{6}|[0-9A-Fa-f]{3})$/.test(str);
})], color => {
// sanity sake let you use hex without the need for #
if (color[0] != "#") {
color = "#" + color;
}
turtle.strokeColor = color;
})
);
/*
* Not apart of logo this allows us to use a RGB instead of HEX
* Though not standard in logo this just gives us a slightly more fine grain color
*/
commandLookUp.add(
new Command(
"colorrgb",
[new CommandArg("params", ARGUMENT_TYPES.PARAMETERS)],
params => {
let [r, g, b] = params;
r = parseInt(r);
g = parseInt(g);
b = parseInt(b);
if (r > 255) {
r = 255;
}
if (r < 0) {
r = 0;
}
if (g > 255) {
g = 255;
}
if (g < 0) {
g = 0;
}
if (b > 255) {
b = 255;
}
if (r < 0) {
b = 0;
}
turtle.strokeColor = color(r, g, b);
}
)
);
/**
* Added as example of taking [...] as not only commands
* but strings you can later process.
* This command expects 3 args separated by spaces.
*/
commandLookUp.add(
new Command(
"author",
[new CommandArg("params", ARGUMENT_TYPES.PARAMETERS)],
params => {
const [author, website, twitter] = params;
console.log("This repository has been created by:");
console.log(`${author} (@${twitter}) - ${website}`);
}
)
);