Commits

Anonymous committed 77aac53

Initial, I-wouldn't-call-it-fantastic work on yoob.Turtle.

Comments (0)

Files changed (3)

+<!DOCTYPE html>
+<head>
+  <meta charset="utf-8">
+  <title>yoob REPL Demo</title>
+  <style>
+#terminal {
+    background: black;
+}
+#floor {
+    background: #ffffdd;
+    border: 1px solid red;
+}
+  </style>
+</head>
+<body>
+
+<h1>Turtle REPL Demo</h1>
+
+<canvas id="floor" width="400" height="400" tabindex="0">
+  Your browser doesn't support displaying an HTML5 canvas.
+</canvas>
+
+<canvas id="terminal" width="400" height="400" tabindex="0">
+  Your browser doesn't support displaying an HTML5 canvas.
+</canvas>
+
+</body>
+<script src="../src/yoob/sprite-manager.js"></script>
+<script src="../src/yoob/turtle.js"></script>
+<script src="../src/yoob/playfield.js"></script>
+<script src="../src/yoob/cursor.js"></script>
+<script src="../src/yoob/playfield-canvas-view.js"></script>
+<script src="../src/yoob/text-terminal.js"></script>
+<script src="../src/yoob/line-input-buffer.js"></script>
+<script src="../src/yoob/tree.js"></script>
+<script src="../src/yoob/scanner.js"></script>
+<script src="../src/yoob/sexp-parser.js"></script>
+<script>
+  var element = document.getElementById('terminal');
+  var floor = document.getElementById('floor');
+  var ctx = floor.getContext('2d');
+  var t = new yoob.TextTerminal().init(80, 8);
+  var view = t.createPlayfieldCanvasView(element, 12, 18);
+  var manager = new yoob.SpriteManager();
+  manager.init(floor);
+  var deg2rad = function(d) { return (d / 360) * Math.PI * 2; }
+  var turtle = new yoob.Turtle();
+  turtle.init(200, 200, 20, 20);
+  turtle.setTheta(deg2rad(270));
+  manager.addSprite(turtle);
+
+  t.write("yoob turtle REPL\n");
+  var p = new yoob.SexpParser();
+  var ast = null;
+  var ib = new yoob.LineInputBuffer().init(element, t);
+  ib.onupdate = function(str) { view.draw(); };
+  ib.onenter = function(str) {
+    var cmd = str.substr(0, 2);
+    var arg = parseInt(str.substr(3), 10);
+
+    if (cmd === 'fd') {
+        turtle.moveBy(arg);
+    } else if (cmd === 'bk') {
+        turtle.moveBy(-1 * arg);
+    } else if (cmd === 'rt') {
+        turtle.rotateBy(deg2rad(arg));
+    } else if (cmd === 'lt') {
+        turtle.rotateBy(-1 * deg2rad(arg));
+    }
+    ctx.clearRect(0, 0, floor.width, floor.height);
+    manager.draw();
+    view.draw();
+  };
+  manager.draw();
+  view.draw();
+  element.focus();
+</script>

src/yoob/sprite-manager.js

     });
   };
 
-  this.move = function(ctx) {
+  this.move = function() {
     this.foreach(function(sprite) { sprite.move(); });
   };
 
   this.draw = function(ctx) {
+    if (ctx === undefined) {
+      ctx = this.canvas.getContext('2d');
+    }
     for (var i = 0; i < this.sprites.length; i++) {
       this.sprites[i].draw(ctx);
     }

src/yoob/turtle.js

+/*
+ * This file is part of yoob.js version 0.5-PRE
+ * Available from https://github.com/catseye/yoob.js/
+ * This file is in the public domain.  See http://unlicense.org/ for details.
+ */
+if (window.yoob === undefined) yoob = {};
+
+/*
+ * requires yoob.Sprite.
+ *
+ * I really haven't worked this out fully yet.
+ * It could also have a velocity, and dx/dy = cos/sin(theta) * velocity.
+ * moveBy is maybe useful enough to have in Sprite itself.
+ */
+
+yoob.Turtle = function() {
+    this.theta = 0;
+
+    /* theta is in radians */
+    this.setTheta = function(theta) {
+        this.theta = theta;
+        this.dx = Math.cos(theta);
+        this.dy = Math.sin(theta);
+    };
+
+    /* dtheta is in radians */
+    this.rotateBy = function(dtheta) {
+        this.setTheta(this.theta + dtheta);
+    };
+    
+    this.moveBy = function(units) {
+        this.x += this.dx * units;
+        this.y += this.dy * units;
+    };
+
+    this.draw = function(ctx) {
+        var x = this.getCenterX();
+        var y = this.getCenterY();
+        var r = this.getWidth() / 2;
+        ctx.fillStyle = "blue";
+        ctx.beginPath();
+        ctx.arc(x, y, r, 0, 2 * Math.PI, false);
+        ctx.fill();
+        ctx.strokeStyle = "black";
+        ctx.lineWidth = 1;
+        ctx.stroke();
+        ctx.beginPath();
+        ctx.strokeStyle = "white";
+        ctx.moveTo(x, y);
+        ctx.lineTo(x + this.dx * r, y + this.dy * r);
+        ctx.stroke();
+    };
+};
+yoob.Turtle.prototype = new yoob.Sprite();