Skip to content

Commit c8e9948

Browse files
committed
turtle works; blocks need improvement
1 parent 95cfc35 commit c8e9948

File tree

9 files changed

+254
-94
lines changed

9 files changed

+254
-94
lines changed

blocks/minecraft.js

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ Blockly.Blocks['minecraft_turtle_go'] = {
2626
init: function() {
2727
this.appendDummyInput()
2828
.appendField("Turtle forward");
29-
this.appendValueInput("DISTANCE")
30-
.setCheck("Number")
31-
.appendField("");
29+
this.appendDummyInput()
30+
.appendField(new Blockly.FieldTextInput("0", Blockly.FieldTextInput.nonnegativeIntegerValidator), "DISTANCE");
3231
this.setInputsInline(true);
3332
this.setPreviousStatement(true);
3433
this.setNextStatement(true);
@@ -47,6 +46,8 @@ Blockly.Blocks['minecraft_turtle_yaw'] = {
4746
this.appendDummyInput("NAME")
4847
.appendField(new Blockly.FieldAngle("90"), "ANGLE");
4948
this.setInputsInline(true);
49+
this.setPreviousStatement(true);
50+
this.setNextStatement(true);
5051
this.setColour(164);
5152
this.setTooltip('');
5253
this.setHelpUrl('github.com/arpruss/raspberryjammod');
@@ -62,6 +63,8 @@ Blockly.Blocks['minecraft_turtle_pitch'] = {
6263
this.appendDummyInput("NAME")
6364
.appendField(new Blockly.FieldAngle("90"), "ANGLE");
6465
this.setInputsInline(true);
66+
this.setPreviousStatement(true);
67+
this.setNextStatement(true);
6568
this.setColour(164);
6669
this.setTooltip('');
6770
this.setHelpUrl('github.com/arpruss/raspberryjammod');
@@ -77,6 +80,51 @@ Blockly.Blocks['minecraft_turtle_roll'] = {
7780
this.appendDummyInput("NAME")
7881
.appendField(new Blockly.FieldAngle("90"), "ANGLE");
7982
this.setInputsInline(true);
83+
this.setPreviousStatement(true);
84+
this.setNextStatement(true);
85+
this.setColour(164);
86+
this.setTooltip('');
87+
this.setHelpUrl('github.com/arpruss/raspberryjammod');
88+
}
89+
};
90+
91+
Blockly.Blocks['minecraft_turtle_set_pen'] = {
92+
init: function() {
93+
this.appendDummyInput()
94+
.appendField("Set pen width to");
95+
this.appendDummyInput()
96+
.appendField(new Blockly.FieldTextInput("1", Blockly.FieldTextInput.nonnegativeIntegerValidator), "WIDTH");
97+
this.appendDummyInput()
98+
.appendField("and block to");
99+
this.appendDummyInput()
100+
.appendField(new Blockly.FieldDropdown([["air", "0"], ["stone", "1"], ["grass", "2"]]), "BLOCK");
101+
this.setInputsInline(true);
102+
this.setPreviousStatement(true);
103+
this.setNextStatement(true);
104+
this.setColour(164);
105+
this.setTooltip('');
106+
this.setHelpUrl('github.com/arpruss/raspberryjammod');
107+
}
108+
};
109+
110+
Blockly.Blocks['minecraft_turtle_pen_up'] = {
111+
init: function() {
112+
this.appendDummyInput()
113+
.appendField("Turtle pen up");
114+
this.setPreviousStatement(true);
115+
this.setNextStatement(true);
116+
this.setColour(164);
117+
this.setTooltip('');
118+
this.setHelpUrl('github.com/arpruss/raspberryjammod');
119+
}
120+
};
121+
122+
Blockly.Blocks['minecraft_turtle_pen_down'] = {
123+
init: function() {
124+
this.appendDummyInput()
125+
.appendField("Turtle pen down");
126+
this.setPreviousStatement(true);
127+
this.setNextStatement(true);
80128
this.setColour(164);
81129
this.setTooltip('');
82130
this.setHelpUrl('github.com/arpruss/raspberryjammod');

blocks_compressed.js

Lines changed: 7 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demos/code/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ <h1><a href="https://developers.google.com/blockly/">Blockly</a>&rlm; &gt;
7676
<block type="minecraft_turtle_yaw"></block>
7777
<block type="minecraft_turtle_pitch"></block>
7878
<block type="minecraft_turtle_roll"></block>
79+
<block type="minecraft_turtle_set_pen"></block>
80+
<block type="minecraft_turtle_pen_up"></block>
81+
<block type="minecraft_turtle_pen_down"></block>
7982
</category>
8083
<category id="catLogic">
8184
<block type="controls_if"></block>

generators/javascript/minecraft.js

Lines changed: 89 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ Blockly.JavaScript.minecraft = function() {
33
"\n"+
44
"var MCPI = Object.create(null);\n"+
55
"\n"+
6-
"MCPI.identityMatrix = [[1,0,0],[0,1,0],[0,0,1]];\n"+
76
"MCPI.TO_RADIANS = Math.PI / 180;\n"+
87
"MCPI.block = \"1\";\n"+
8+
"MCPI.penDown = true;\n"+
9+
"MCPI.nib = [[0,0,0]];\n"+
910
"\n"+
1011
"MCPI.mmMultiply = function(a,b) {\n"+
11-
" c = MCPI.identityMatrix;\n"+
12+
" var c = [[0,0,0],[0,0,0],[0,0,0]];\n"+
1213
" for (var i = 0; i < 3 ; i++) for (var j = 0; j < 3 ; j++)\n"+
1314
" c[i][j] = a[i][0]*b[0][j] + a[i][1]*b[1][j] + a[i][2]*b[2][j];\n"+
1415
" return c;\n"+
@@ -134,30 +135,82 @@ Blockly.JavaScript.minecraft = function() {
134135
"};\n"+
135136
"\n"+
136137
"MCPI.setBlock = function(x,y,z,block) {\n"+
138+
" if (block != \"0\" && Math.floor(x) == Math.floor(MCPI.playerX) && Math.floor(z) == Math.floor(MCPI.playerZ)\n"+
139+
" && (Math.floor(y) >= MCPI.playerShiftedHeight) ) {\n"+
140+
" MCPI.playerShiftedHeight = Math.floor(y) + 1;\n"+
141+
" MCPI.socket.send(\"player.setPos(\"+MCPI.playerX+\",\"+MCPI.playerShiftedHeight+\",\"+MCPI.playerZ+\")\");\n"+
142+
" }\n"+
137143
" MCPI.socket.send(\"world.setBlock(\"+x+\",\"+y+\",\"+z+\",\"+block+\")\");\n"+
138144
"}\n"+
139145
"\n"+
146+
"MCPI.drawPoint = function(x0,y0,z0) {\n"+
147+
" var l = MCPI.nib.length;\n"+
148+
" if (l == 0) {\n"+
149+
" return;\n"+
150+
" }\n"+
151+
" else if (l == 1) {\n"+
152+
" MCPI.setBlock(x0,y0,z0,MCPI.block);\n"+
153+
" return;\n"+
154+
" }\n"+
155+
"\n"+
156+
" for (i = 0 ; i < l ; i++) {\n"+
157+
" var p = MCPI.nib[i];\n"+
158+
" var x = p[0] + x0;\n"+
159+
" var y = p[1] + y0;\n"+
160+
" var z = p[2] + z0;\n"+
161+
" var indexable = \"\"+x+\",\"+y+\",\"+z;\n"+
162+
" if (! (indexable in MCPI.saved)) {\n"+
163+
" MCPI.setBlock(x,y,z,MCPI.block);\n"+
164+
" MCPI.saved[indexable] = 1;\n"+
165+
" }\n"+
166+
" }\n"+
167+
"}\n"+
168+
"\n"+
140169
"MCPI.drawLine = function(x1,y1,z1,x2,y2,z2) {\n"+
141-
" l = MCPI.getLine(x1,y1,z1,x2,y2,z2);\n"+
170+
" MCPI.saved = Object.create(null);\n"+
171+
" var l = MCPI.getLine(x1,y1,z1,x2,y2,z2);\n"+
142172
" for (var i=0; i<l.length ; i++) {\n"+
143-
" MCPI.setBlock(l[i][0],l[i][1],l[i][2],MCPI.block);\n"+
173+
" MCPI.drawPoint(l[i][0],l[i][1],l[i][2]);\n"+
144174
" }\n"+
145175
"}\n"+
146176
"\n"+
177+
"MCPI.turtleSetWidth = function(w) {\n"+
178+
" MCPI.nib = [];\n"+
179+
" if (w == 0) {\n"+
180+
" return;\n"+
181+
" }\n"+
182+
" else if (w == 1) {\n"+
183+
" MCPI.nib = [[0,0,0]];\n"+
184+
" }\n"+
185+
" else if (w == 2) {\n"+
186+
" for (x=-1; x<=0; x++)\n"+
187+
" for (y=0; y<=1; y++)\n"+
188+
" for (z=-1; z<=0; z++)\n"+
189+
" MCPI.nib.push([x,y,z]);\n"+
190+
" }\n"+
191+
" else {\n"+
192+
" var r2 = w*w/4;\n"+
193+
" for (var x = -Math.ceil(w) ; x <= Math.ceil(w); x++)\n"+
194+
" for (var y = -Math.ceil(w) ; y <= Math.ceil(w); y++)\n"+
195+
" for (var z = -Math.ceil(w) ; z <= Math.ceil(w); z++)\n"+
196+
" if (x*x + y*y + z*z < r2)\n"+
197+
" MCPI.nib.push([x,y,z,]);\n"+
198+
" } \n"+
199+
"}\n"+
200+
"\n"+
147201
"MCPI.turtleYaw = function(angleDegrees) {\n"+
148-
" MCPI.matrix = mmMultiply(MCPI.matrix, MCPI.yawMatrix(angleDegrees));\n"+
202+
" MCPI.matrix = MCPI.mmMultiply(MCPI.matrix, MCPI.yawMatrix(angleDegrees));\n"+
149203
"};\n"+
150204
"\n"+
151205
"MCPI.turtlePitch = function(angleDegrees) {\n"+
152-
" MCPI.matrix = mmMultiply(MCPI.matrix, MCPI.pitchMatrix(angleDegrees));\n"+
206+
" MCPI.matrix = MCPI.mmMultiply(MCPI.matrix, MCPI.pitchMatrix(angleDegrees));\n"+
153207
"};\n"+
154208
"\n"+
155209
"MCPI.turtleRoll = function(angleDegrees) {\n"+
156-
" MCPI.matrix = mmMultiply(MCPI.matrix, MCPI.rollMatrix(angleDegrees));\n"+
210+
" MCPI.matrix = MCPI.mmMultiply(MCPI.matrix, MCPI.rollMatrix(angleDegrees));\n"+
157211
"};\n"+
158212
"\n"+
159213
"MCPI.turtleGo = function(distance) {\n"+
160-
" var heading = [MCPI.matrix[0][2],MCPI.matrix[1][2],MCPI.matrix[2][2]]\n"+
161214
" var newX = MCPI.curX + MCPI.matrix[0][2] * distance;\n"+
162215
" var newY = MCPI.curY + MCPI.matrix[1][2] * distance;\n"+
163216
" var newZ = MCPI.curZ + MCPI.matrix[2][2] * distance;\n"+
@@ -176,45 +229,38 @@ Blockly.JavaScript.minecraft = function() {
176229
" MCPI.playerX = parseFloat(args[0]);\n"+
177230
" MCPI.playerY = parseFloat(args[1]);\n"+
178231
" MCPI.playerZ = parseFloat(args[2]);\n"+
179-
" MCPI.curX = MCPI.playerX\n"+
180-
" MCPI.curY = MCPI.playerY\n"+
181-
" MCPI.curZ = MCPI.playerZ\n"+
232+
" MCPI.curX = MCPI.playerX;\n"+
233+
" MCPI.curY = MCPI.playerY;\n"+
234+
" MCPI.curZ = MCPI.playerZ;\n"+
235+
" MCPI.playerShiftedHeight = Math.floor(MCPI.playerY);\n"+
182236
"\n"+
183237
" MCPI.socket.onmessage = function(event) {\n"+
184238
" var yaw = parseFloat(event.data.trim());\n"+
185-
"\n"+
186-
" MCPI.socket.onmessage = function(event) {\n"+
187-
" var pitch = parseFloat(event.data.trim());\n"+
188-
" MCPI.matrix = MCPI.mmMultiply(MCPI.yawMatrix(yaw), MCPI.pitchMatrix(-pitch));\n"+
239+
" MCPI.matrix = MCPI.yawMatrix(yaw);\n"+
189240
"\n";
190241
Blockly.JavaScript.cleanups_['minecraft'] = "MCPI.socket.close();\n"+
191242
"}; // end MCPI.socket.onmessage for player.getRotation()\n"+
192-
"MCPI.socket.send(\"player.getPitch()\");\n"+
193-
"}; // end MCPI.socket.onmessage for player.getPitch()\n"+
194243
"MCPI.socket.send(\"player.getRotation()\");\n"+
195244
"}; // end MCPI.socket.onmessage for player.getPos()\n"+
196245
"MCPI.socket.send(\"player.getPos()\");\n"+
197246
"}; // end MCPI.socket.onopen\n";
198247
};
199248

200-
201249
Blockly.JavaScript['minecraft_set_block'] = function(block) {
202250
Blockly.JavaScript.minecraft();
203251

204252
var dropdown_block = block.getFieldValue('block');
205253
var value_x = Blockly.JavaScript.valueToCode(block, 'x', Blockly.JavaScript.ORDER_ATOMIC);
206254
var value_y = Blockly.JavaScript.valueToCode(block, 'y', Blockly.JavaScript.ORDER_ATOMIC);
207255
var value_z = Blockly.JavaScript.valueToCode(block, 'z', Blockly.JavaScript.ORDER_ATOMIC);
208-
// TODO: Assemble JavaScript into code variable.
209256
var code = 'MCPI.setBlock('+value_x+','+value_y+','+value_z+',"'+dropdown_block+'");\n';
210257
return code;
211258
};
212259

213260
Blockly.JavaScript['minecraft_turtle_go'] = function(block) {
214261
Blockly.JavaScript.minecraft();
215-
var value_distance = Blockly.JavaScript.valueToCode(block, 'DISTANCE', Blockly.JavaScript.ORDER_ATOMIC);
216-
// TODO: Assemble JavaScript into code variable.
217-
var code = 'MCPI.turtleGo('+value_distance+');\n';
262+
var text_distance = parseFloat(block.getFieldValue('DISTANCE'));
263+
var code = 'MCPI.turtleGo('+text_distance+');\n';
218264
return code;
219265
};
220266

@@ -244,3 +290,24 @@ Blockly.JavaScript['minecraft_turtle_roll'] = function(block) {
244290
var code = 'MCPI.turtleRoll('+angle_angle+'*'+dropdown_direction+');\n';
245291
return code;
246292
};
293+
294+
Blockly.JavaScript['minecraft_turtle_set_pen'] = function(block) {
295+
Blockly.JavaScript.minecraft();
296+
var text_width = parseFloat(block.getFieldValue('WIDTH'));
297+
var dropdown_block = block.getFieldValue('BLOCK');
298+
var code = 'MCPI.turtleSetWidth('+text_width+');\n'+
299+
'MCPI.block = "'+dropdown_block+'";\n';
300+
return code;
301+
};
302+
303+
Blockly.JavaScript['minecraft_turtle_pen_up'] = function(block) {
304+
Blockly.JavaScript.minecraft();
305+
var code = 'MCPI.penDown = false;\n';
306+
return code;
307+
};
308+
309+
Blockly.JavaScript['minecraft_turtle_pen_down'] = function(block) {
310+
Blockly.JavaScript.minecraft();
311+
var code = 'MCPI.penDown = false;\n';
312+
return code;
313+
};

generators/javascript/source/mccleanup.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
MCPI.socket.close();
22
}; // end MCPI.socket.onmessage for player.getRotation()
3-
MCPI.socket.send("player.getPitch()");
4-
}; // end MCPI.socket.onmessage for player.getPitch()
53
MCPI.socket.send("player.getRotation()");
64
}; // end MCPI.socket.onmessage for player.getPos()
75
MCPI.socket.send("player.getPos()");

0 commit comments

Comments
 (0)