wiki-plugin-mech 0.1.8 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/client/mech.js +56 -1
- package/package.json +1 -1
package/client/mech.js
CHANGED
|
@@ -246,6 +246,20 @@
|
|
|
246
246
|
}
|
|
247
247
|
}
|
|
248
248
|
|
|
249
|
+
function forward_emit ({elem,command,args,state}) {
|
|
250
|
+
if(args.length < 1) return trouble(elem,`FORWARD expects an argument, the number of steps to move a "turtle".`)
|
|
251
|
+
if(!('turtle' in state)) state.turtle = new Turtle(elem)
|
|
252
|
+
const position = state.turtle.forward(+args[0])
|
|
253
|
+
elem.innerHTML = command + ` ⇒ ${position.map(n => (n-200).toFixed(1)).join(', ')}`
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function turn_emit ({elem,command,args,state}) {
|
|
257
|
+
if(args.length < 1) return trouble(elem,`TURN expects an argument, the number of degrees to turn a "turtle".`)
|
|
258
|
+
if(!('turtle' in state)) state.turtle = new Turtle(elem)
|
|
259
|
+
const direction = state.turtle.turn(+args[0])
|
|
260
|
+
elem.innerHTML = command + ` ⇒ ${direction}°`
|
|
261
|
+
}
|
|
262
|
+
|
|
249
263
|
const blocks = {
|
|
250
264
|
CLICK: {emit:click_emit},
|
|
251
265
|
HELLO: {emit:hello_emit},
|
|
@@ -257,7 +271,9 @@
|
|
|
257
271
|
NEIGHBORS:{emit:neighbors_emit},
|
|
258
272
|
WALK: {emit:walk_emit},
|
|
259
273
|
TICK: {emit:tick_emit},
|
|
260
|
-
UNTIL: {emit:until_emit}
|
|
274
|
+
UNTIL: {emit:until_emit},
|
|
275
|
+
FORWARD: {emit:forward_emit},
|
|
276
|
+
TURN: {emit:turn_emit}
|
|
261
277
|
}
|
|
262
278
|
|
|
263
279
|
function run (nest,state={}) {
|
|
@@ -421,4 +437,43 @@
|
|
|
421
437
|
|
|
422
438
|
}
|
|
423
439
|
|
|
440
|
+
class Turtle {
|
|
441
|
+
constructor(elem) {
|
|
442
|
+
const size = elem
|
|
443
|
+
const div = document.createElement('div')
|
|
444
|
+
elem.closest('.item').firstElementChild.prepend(div)
|
|
445
|
+
div.outerHTML = `
|
|
446
|
+
<div style="border:1px solid black; background-color:#f8f8f8; margin-bottom:16px;">
|
|
447
|
+
<svg viewBox="0 0 400 400" width=100% height=400>
|
|
448
|
+
<circle id=dot r=5 cx=200 cy=200 stroke="#ccc"></circle>
|
|
449
|
+
</svg>
|
|
450
|
+
</div>`
|
|
451
|
+
this.svg = elem.closest('.item').getElementsByTagName('svg')[0]
|
|
452
|
+
this.position = [200,200]
|
|
453
|
+
this.direction = 0
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
forward(steps) {
|
|
457
|
+
const theta = this.direction*2*Math.PI/360
|
|
458
|
+
const [x1,y1] = this.position
|
|
459
|
+
const [x2,y2] = [x1+steps*Math.sin(theta), y1+steps*Math.cos(theta)]
|
|
460
|
+
const line = document.createElementNS("http://www.w3.org/2000/svg", 'line')
|
|
461
|
+
const set = (k,v) => line.setAttribute(k,Math.round(v))
|
|
462
|
+
set("x1",x1); set("y1",400-y1)
|
|
463
|
+
set("x2",x2); set("y2",400-y2)
|
|
464
|
+
line.style.stroke = "black"
|
|
465
|
+
line.style.strokeWidth = "2px"
|
|
466
|
+
this.svg.appendChild(line)
|
|
467
|
+
const dot = this.svg.getElementById('dot')
|
|
468
|
+
dot.setAttribute('cx',Math.round(x2))
|
|
469
|
+
dot.setAttribute('cy',Math.round(400-y2))
|
|
470
|
+
this.position = [x2,y2]
|
|
471
|
+
return this.position
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
turn(degrees) {
|
|
475
|
+
this.direction += degrees
|
|
476
|
+
return this.direction}
|
|
477
|
+
}
|
|
478
|
+
|
|
424
479
|
}).call(this)
|