forked from scratchfoundation/scratch-editor
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkoshien.js
More file actions
143 lines (122 loc) · 6.32 KB
/
Copy pathkoshien.js
File metadata and controls
143 lines (122 loc) · 6.32 KB
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
/**
* Render a list-typed argument for a koshien block.
*
* Lists differ between Ruby versions:
* - v1: `list("$名前")` (Smalruby3::List wrapper)
* - v2: `$名前` (plain global array variable; `list()` syntax is rejected in v2)
* @param {RubyGenerator} Generator - the RubyGenerator.
* @param {?string} listName - the resolved list name (e.g. "$名前") or null.
* @returns {string} - the argument expression, or 'nil' when no list.
*/
const koshienListArg = (Generator, listName) => {
if (!listName) {
return 'nil';
}
return String(Generator.version) === '2' ? listName : `list(${Generator.quote_(listName)})`;
};
/**
* Define Ruby code generator for Microbit More Blocks
* @param {RubyGenerator} Generator The RubyGenerator
* @returns {RubyGenerator} same as param.
*/
export default function (Generator) {
Generator.koshien_connectGame = function (block) {
const name = Generator.valueToCode(block, 'NAME', Generator.ORDER_NONE) || Generator.quote_('player1');
// v2: ゲーム接続をイベント hat として表現し、AI 本体を do...end (サブスタック) に包む。
// これによりクラス表現 (class < Smalruby3::Sprite) の中に配置できる。
// v1: 従来どおりフラットな文 (Sprite.new do...end の中で逐次実行)。
if (String(Generator.version) === '2') {
block.isStatement = true;
return `koshien.when_connect_game(name: ${name}) do\n`;
}
return `koshien.connect_game(name: ${name})\n`;
};
Generator.koshien_getMapArea = function (block) {
const position = Generator.valueToCode(block, 'POSITION', Generator.ORDER_NONE) || Generator.quote_('0:0');
return `koshien.get_map_area(${position})\n`;
};
Generator.koshien_map = function (block) {
const position = Generator.valueToCode(block, 'POSITION', Generator.ORDER_NONE) || Generator.quote_('0:0');
return [`koshien.map(${position})`];
};
Generator.koshien_moveTo = function (block) {
const position = Generator.valueToCode(block, 'POSITION', Generator.ORDER_NONE) || Generator.quote_('0:0');
return `koshien.move_to(${position})\n`;
};
Generator.koshien_calcGoalRoute = function (block) {
const resultListName = Generator.listNameByName(
Generator.getFieldValue(block, 'RESULT', Generator.ORDER_NONE)
);
const result = koshienListArg(Generator, resultListName);
return `koshien.calc_route(result: ${result})\n`;
};
Generator.koshien_calcRoute = function (block) {
const src = Generator.valueToCode(block, 'SRC', Generator.ORDER_NONE) || Generator.quote_('0:0');
const dst = Generator.valueToCode(block, 'DST', Generator.ORDER_NONE) || Generator.quote_('0:0');
const exceptCellsListName = Generator.listNameByName(
Generator.getFieldValue(block, 'EXCEPT_CELLS', Generator.ORDER_NONE)
);
const exceptCells = koshienListArg(Generator, exceptCellsListName);
const resultListName = Generator.listNameByName(
Generator.getFieldValue(block, 'RESULT', Generator.ORDER_NONE)
);
const result = koshienListArg(Generator, resultListName);
return `koshien.calc_route(result: ${result}, src: ${src}, dst: ${dst}, except_cells: ${exceptCells})\n`;
};
Generator.koshien_setItem = function (block) {
const item = Generator.getFieldValue(block, 'ITEM') || null;
const position = Generator.valueToCode(block, 'POSITION', Generator.ORDER_NONE) || Generator.quote_('0:0');
return `koshien.set_${item}(${position})\n`;
};
Generator.koshien_mapFrom = function (block) {
const position = Generator.valueToCode(block, 'POSITION', Generator.ORDER_NONE) || Generator.quote_('0:0');
const mapVariableName = Generator.variableNameByName(
Generator.getFieldValue(block, 'MAP', Generator.ORDER_NONE)
);
const map = mapVariableName ? mapVariableName : 'nil';
return [`koshien.map_from(${position}, ${map})`];
};
Generator.koshien_mapAll = function () {
return ['koshien.map_all'];
};
Generator.koshien_locateObjects = function (block) {
const position = Generator.valueToCode(block, 'POSITION', Generator.ORDER_NONE) || Generator.quote_('0:0');
const sqSize = Generator.valueToCode(block, 'SQ_SIZE', Generator.ORDER_NONE) || 5;
const objects = Generator.valueToCode(block, 'OBJECTS', Generator.ORDER_NONE) || Generator.quote_('ABCD');
const resultListName = Generator.listNameByName(
Generator.getFieldValue(block, 'RESULT', Generator.ORDER_NONE)
);
const result = koshienListArg(Generator, resultListName);
return `koshien.locate_objects(result: ${result}, sq_size: ${sqSize}, cent: ${position}, objects: ${objects})\n`;
};
Generator.koshien_targetCoordinate = function (block) {
const target = Generator.getFieldValue(block, 'TARGET') || 'player';
const coordinate = Generator.getFieldValue(block, 'COORDINATE') || 'position';
if (coordinate === 'position') {
return [`koshien.${target}`];
}
return [`koshien.${target}_${coordinate}`];
};
Generator.koshien_turnOver = function () {
return `koshien.turn_over\n`;
};
Generator.koshien_position = function (block) {
const x = Generator.valueToCode(block, 'X', Generator.ORDER_NONE) || 0;
const y = Generator.valueToCode(block, 'Y', Generator.ORDER_NONE) || 0;
return [`koshien.position(${x}, ${y})`];
};
Generator.koshien_positionOf = function (block) {
const position = Generator.valueToCode(block, 'POSITION', Generator.ORDER_NONE) || Generator.quote_('0:0');
const coordinate = Generator.getFieldValue(block, 'COORDINATE') || 'x';
return [`koshien.position_of_${coordinate}(${position})`];
};
Generator.koshien_object = function (block) {
const object = Generator.quote_(Generator.getFieldValue(block, 'OBJECT') || 'unknown');
return [`koshien.object(${object})`];
};
Generator.koshien_setMessage = function (block) {
const message = Generator.valueToCode(block, 'MESSAGE', Generator.ORDER_NONE) || Generator.quote_('hello');
return `koshien.set_message(${message})\n`;
};
return Generator;
}