-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathscratch2mip_for3.js
81 lines (78 loc) · 2.06 KB
/
scratch2mip_for3.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
class Scratch2Mip {
constructor() {
this.ws = undefined;
}
getInfo() { // 拡張機能の各種情報
return {
id: 'scratch2mip',
name: 'Scratch2Mip', // 拡張機能の名前
blocks: [ // 各ブロックの定義
{
opcode: 'connect',
blockType: Scratch.BlockType.COMMAND,
text: 'connect'
},
{
opcode: 'move_forward',
blockType: Scratch.BlockType.COMMAND,
text: 'move forward [STEPS] steps',
arguments: {
STEPS: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: 10
}
}
},
{
opcode: 'move_backward',
blockType: Scratch.BlockType.COMMAND,
text: 'move backward [STEPS] steps',
arguments: {
STEPS: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: 10
}
}
},
{
opcode: 'turn_right',
blockType: Scratch.BlockType.COMMAND,
text: 'turn right [DEGREES] degrees',
arguments: {
DEGREES: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: 90
}
}
},
{
opcode: 'turn_left',
blockType: Scratch.BlockType.COMMAND,
text: 'turn left [DEGREES] degrees',
arguments: {
DEGREES: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: 90
}
}
}
]
}
}
connect() {
this.ws = new WebSocket('ws://localhost:8080');
}
move_forward({STEPS}) {
this.ws.send(JSON.stringify({command: 'forward', steps: STEPS}));
}
move_backward({STEPS}) {
this.ws.send(JSON.stringify({command: 'backward', steps: STEPS}));
}
turn_right({DEGREES}) {
this.ws.send(JSON.stringify({command: 'right', degrees: DEGREES}));
}
turn_left({DEGREES}) {
this.ws.send(JSON.stringify({command: 'left', degrees: DEGREES}));
}
}
Scratch.extensions.register(new Scratch2Mip());