Skip to content

Commit d743833

Browse files
author
Ke, Mingze
committed
add change fish speed
1 parent 264f18e commit d743833

6 files changed

Lines changed: 113 additions & 74 deletions

File tree

blocks/msg/en.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ Blockly.Msg.WEBDUINO_CAR_MOVE_SEC = "secs";
9494
Blockly.Msg.WEBDUINO_FISH = "Fish";
9595
Blockly.Msg.WEBDUINO_FISH_MOVE_FOR = "for";
9696
Blockly.Msg.WEBDUINO_FISH_MOVE_SEC = "secs";
97+
Blockly.Msg.WEBDUINO_FISH_MOVE_SPEED = ", at speed";
98+
Blockly.Msg.WEBDUINO_FISH_SOAR = "(soar)";
99+
Blockly.Msg.WEBDUINO_FISH_SINK = "(sink)";
100+
Blockly.Msg.WEBDUINO_FISH_MOVE = "(move toward)";
101+
Blockly.Msg.WEBDUINO_FISH_LEFT = "(turn left)";
102+
Blockly.Msg.WEBDUINO_FISH_RIGHT = "(turn right)";
97103

98104
Blockly.Msg.WEBDUINO_BUZZER = "Buzzer , pin:";
99105
Blockly.Msg.WEBDUINO_BUZZER_MUSIC1 = "Add Music, name:";

blocks/msg/zh-hant.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ Blockly.Msg.WEBDUINO_CAR_MOVE_SEC = "秒";
121121
Blockly.Msg.WEBDUINO_FISH = "飛天魚";
122122
Blockly.Msg.WEBDUINO_FISH_MOVE_FOR = "持續";
123123
Blockly.Msg.WEBDUINO_FISH_MOVE_SEC = "秒";
124+
Blockly.Msg.WEBDUINO_FISH_MOVE_SPEED = ", 速度";
125+
Blockly.Msg.WEBDUINO_FISH_SOAR = "(上升)";
126+
Blockly.Msg.WEBDUINO_FISH_SINK = "(下降)";
127+
Blockly.Msg.WEBDUINO_FISH_MOVE = "(前進)";
128+
Blockly.Msg.WEBDUINO_FISH_LEFT = "(左轉)";
129+
Blockly.Msg.WEBDUINO_FISH_RIGHT = "(右轉)";
124130

125131
Blockly.Msg.WEBDUINO_FIREBASE_NAME = "< Firebase > 名稱:";
126132
Blockly.Msg.WEBDUINO_FIREBASE_URL = "網址:";

blocks/webduino.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,20 +221,46 @@ Blockly.Blocks['fish_new'] = {
221221
}
222222
};
223223

224+
Blockly.Blocks['fish_angle'] = {
225+
init: function () {
226+
this.appendValueInput("secs_")
227+
.appendField(new Blockly.FieldVariable(" "), "fish_")
228+
.appendField(new Blockly.FieldDropdown([
229+
["➚ " + Blockly.Msg.WEBDUINO_FISH_SOAR, "soar"],
230+
["➘ " + Blockly.Msg.WEBDUINO_FISH_SINK, "sink"]
231+
]), "angle_")
232+
.appendField(Blockly.Msg.WEBDUINO_FISH_MOVE_FOR);
233+
this.appendDummyInput()
234+
.appendField(Blockly.Msg.WEBDUINO_FISH_MOVE_SEC);
235+
this.setInputsInline(true);
236+
this.setPreviousStatement(true);
237+
this.setNextStatement(true);
238+
this.setColour(65);
239+
this.setTooltip('');
240+
this.setHelpUrl('http://www.example.com/');
241+
}
242+
};
243+
224244
Blockly.Blocks['fish_move'] = {
225245
init: function () {
226246
this.appendValueInput("secs_")
227247
.appendField(new Blockly.FieldVariable(" "), "fish_")
228248
.appendField(new Blockly.FieldDropdown([
229-
["↑", "soar"],
230-
["↓", "sink"],
231-
["move", "move"],
232-
["left", "left"],
233-
["right", "right"]
249+
["↑ " + Blockly.Msg.WEBDUINO_FISH_MOVE, "move"],
250+
["↺ " + Blockly.Msg.WEBDUINO_FISH_LEFT, "left"],
251+
["↻ " + Blockly.Msg.WEBDUINO_FISH_RIGHT, "right"]
234252
]), "move_")
235253
.appendField(Blockly.Msg.WEBDUINO_FISH_MOVE_FOR);
236254
this.appendDummyInput()
237255
.appendField(Blockly.Msg.WEBDUINO_FISH_MOVE_SEC);
256+
this.appendDummyInput()
257+
.appendField(Blockly.Msg.WEBDUINO_FISH_MOVE_SPEED)
258+
.appendField(new Blockly.FieldDropdown([
259+
["1", "1"],
260+
["2", "2"],
261+
["3", "3"],
262+
["4", "4"]
263+
]), "speed_");
238264
this.setInputsInline(true);
239265
this.setPreviousStatement(true);
240266
this.setNextStatement(true);

generators/webduino.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,21 @@ Blockly.JavaScript['fish_new'] = function (block) {
8181
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
8282
};
8383

84+
Blockly.JavaScript['fish_angle'] = function (block) {
85+
var variable_fish_ = Blockly.JavaScript.variableDB_.getName(block.getFieldValue('fish_'), Blockly.Variables.NAME_TYPE);
86+
var dropdown_angle_ = block.getFieldValue('angle_');
87+
var value_secs_ = Blockly.JavaScript.valueToCode(block, 'secs_', Blockly.JavaScript.ORDER_NONE);
88+
var code = variable_fish_ + '.' + dropdown_angle_ + '(' + value_secs_ + ');\n';
89+
block.setPromise(true);
90+
return code;
91+
};
92+
8493
Blockly.JavaScript['fish_move'] = function (block) {
8594
var variable_fish_ = Blockly.JavaScript.variableDB_.getName(block.getFieldValue('fish_'), Blockly.Variables.NAME_TYPE);
8695
var dropdown_move_ = block.getFieldValue('move_');
8796
var value_secs_ = Blockly.JavaScript.valueToCode(block, 'secs_', Blockly.JavaScript.ORDER_NONE);
88-
var code = variable_fish_ + '.' + dropdown_move_ + '(' + value_secs_ + ');\n';
97+
var dropdown_speed_ = block.getFieldValue('speed_');
98+
var code = variable_fish_ + '.' + dropdown_move_ + '(' + value_secs_ + ', ' + dropdown_speed_ + ');\n';
8999
block.setPromise(true);
90100
return code;
91101
};

views/index.handlebars

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,14 +429,24 @@
429429
<block type="fish_new"></block>
430430
</value>
431431
</block>
432+
<block type="fish_angle">
433+
<field name="fish_">fish</field>
434+
<field name="angle_">soar</field>
435+
<value name="secs_">
436+
<block type="math_number">
437+
<field name="NUM">1</field>
438+
</block>
439+
</value>
440+
</block>
432441
<block type="fish_move">
433442
<field name="fish_">fish</field>
434-
<field name="move_">soar</field>
443+
<field name="move_">move</field>
435444
<value name="secs_">
436445
<block type="math_number">
437446
<field name="NUM">1</field>
438447
</block>
439448
</value>
449+
<field name="speed_">2</field>
440450
</block>
441451
</category>
442452
<sep></sep>

webduino-fish.js

Lines changed: 48 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ function getFish(board) {
55
function Fish(board) {
66
this._board = board;
77
this._pins = [];
8-
this._speed = 2;
98
this._timer = null;
109
var self = this;
1110
[0, 2, 4, 5, 12, 13, 14, 15].forEach(function (val) {
@@ -51,11 +50,13 @@ Fish.prototype.sink = function (secs) {
5150
Fish.prototype.left = function (secs, speed) {
5251
var self = this;
5352
return new Promise(function (resolve, reject) {
54-
speed = speed || self._speed;
55-
self._speed = speed;
56-
self.flapLeft();
53+
if (self._timer) {
54+
self.stop();
55+
}
56+
57+
self.flap('left', speed);
5758
setTimeout(function () {
58-
self.stopFlap();
59+
self.stop();
5960
resolve();
6061
}, secs * 1000);
6162
});
@@ -64,11 +65,13 @@ Fish.prototype.left = function (secs, speed) {
6465
Fish.prototype.right = function (secs, speed) {
6566
var self = this;
6667
return new Promise(function (resolve, reject) {
67-
speed = speed || self._speed;
68-
self._speed = speed;
69-
self.flapRight();
68+
if (self._timer) {
69+
self.stop();
70+
}
71+
72+
self.flap('right', speed);
7073
setTimeout(function () {
71-
self.stopFlap();
74+
self.stop();
7275
resolve();
7376
}, secs * 1000);
7477
});
@@ -77,87 +80,65 @@ Fish.prototype.right = function (secs, speed) {
7780
Fish.prototype.move = function (secs, speed) {
7881
var self = this;
7982
return new Promise(function (resolve, reject) {
80-
speed = speed || self._speed;
81-
self._speed = speed;
82-
self.flap();
83+
if (self._timer) {
84+
self.stop();
85+
}
86+
87+
self.flap(speed);
8388
setTimeout(function () {
84-
self.stopFlap();
89+
self.stop();
8590
resolve();
8691
}, secs * 1000);
8792
});
8893
};
8994

90-
Fish.prototype.flap = function () {
91-
var self = this;
92-
if (self._timer) {
93-
self.stopFlap();
95+
Fish.prototype.flap = function (direction, speed) {
96+
var lSpeed, rSpeed, self = this;
97+
98+
if (isNaN(speed = parseInt(speed))) {
99+
if (isNaN(speed = parseInt(direction))) {
100+
speed = 1;
101+
}
94102
}
95-
self._pins[4].on();
96-
self._pins[5].on();
97-
self._pins[6].off();
98-
self._pins[7].off();
99-
self._timer = setTimeout(function () {
100-
self._pins[4].off();
101-
self._pins[5].off();
102-
self._pins[6].on();
103-
self._pins[7].on();
104-
self._timer = setTimeout(function () {
105-
if (self._timer) {
106-
self.flap();
107-
}
108-
}, Fish.SPEED[self._speed]);
109-
}, Fish.SPEED[self._speed]);
110-
};
111103

112-
Fish.prototype.flapRight = function () {
113-
var self = this;
114-
if (self._timer) {
115-
self.stopFlap();
104+
switch (direction) {
105+
case 'left':
106+
lSpeed = 2 * speed;
107+
rSpeed = speed;
108+
break;
109+
case 'right':
110+
lSpeed = speed;
111+
rSpeed = 2 * speed;
112+
break;
113+
default:
114+
lSpeed = rSpeed = speed;
115+
break;
116116
}
117+
117118
self._pins[4].on();
118119
self._pins[5].on();
119120
self._pins[6].off();
120121
self._pins[7].off();
121122
self._timer = setTimeout(function () {
122123
self._pins[4].off();
123124
self._pins[5].off();
124-
self._pins[6].off();
125-
self._pins[7].off();
126-
self._timer = setTimeout(function () {
127-
if (self._timer) {
128-
self.flapRight();
129-
}
130-
}, Fish.SPEED[self._speed]);
131-
}, Fish.SPEED[self._speed]);
132-
};
133-
134-
Fish.prototype.flapLeft = function () {
135-
var self = this;
136-
if (self._timer) {
137-
self.stopFlap();
138-
}
139-
self._pins[4].off();
140-
self._pins[5].off();
141-
self._pins[6].on();
142-
self._pins[7].on();
143-
self._timer = setTimeout(function () {
144-
self._pins[4].off();
145-
self._pins[5].off();
146-
self._pins[6].off();
147-
self._pins[7].off();
125+
self._pins[6].on();
126+
self._pins[7].on();
148127
self._timer = setTimeout(function () {
149128
if (self._timer) {
150-
self.flapLeft();
129+
self.flap(direction, speed);
151130
}
152-
}, Fish.SPEED[self._speed]);
153-
}, Fish.SPEED[self._speed]);
131+
}, 1000 / lSpeed);
132+
}, 1000 / rSpeed);
154133
};
155134

156-
Fish.prototype.stopFlap = function () {
135+
Fish.prototype.stop = function () {
157136
if (this._timer) {
158137
clearTimeout(this._timer);
159138
delete this._timer;
139+
this._pins[4].off();
140+
this._pins[5].off();
141+
this._pins[6].off();
142+
this._pins[7].off();
160143
}
161144
};
162-
163-
Fish.SPEED = [1500, 900, 300];

0 commit comments

Comments
 (0)