Commits

catseye  committed 729c76e

Add yoob.PathSet, use in yoob.Turtle, add pu and pd cmds in demo.

  • Participants
  • Parent commits d3113a0

Comments (0)

Files changed (3)

File eg/turtle.html

 
 </body>
 <script src="../src/yoob/sprite-manager.js"></script>
+<script src="../src/yoob/path.js"></script>
 <script src="../src/yoob/turtle.js"></script>
 <script src="../src/yoob/playfield.js"></script>
 <script src="../src/yoob/cursor.js"></script>
         turtle.rotateBy(deg2rad(arg));
     } else if (cmd === 'lt') {
         turtle.rotateBy(-1 * deg2rad(arg));
+    } else if (cmd === 'pu') {
+        turtle.setPenDown(false);
+    } else if (cmd === 'pd') {
+        turtle.setPenDown(true);
     }
     ctx.clearRect(0, 0, floor.width, floor.height);
     turtle.drawTrail(ctx);

File src/yoob/path.js

  */
 yoob.Path = function(cfg) {
     this.init = function(cfg) {
+        cfg = cfg || {};
         this.points = cfg.points || [];
         this.title = cfg.title;
         // if defined, the path will be filled with this style
         return path;
     };
 
-    this.mapWithJitter = function(jitter) {
-        var r = function() {
-            return Math.random() * jitter - (jitter / 2);
-        };
-        return this.map(function(x, y) {
-            return [x + r(), y + r()];
-        });
-    };
+    // view methods follow
 
     this.applyLine = function(ctx) {
         this.foreachPoint(function(x, y) {
         }
     };
 
+    /*
+     * Values in the cfg object will be used if they are not specified on
+     * the yoob.Line object.
+     */
     this.draw = function(ctx, cfg) {
         cfg = cfg || {};
         this._draw(ctx,
         );
     };
 
+    /*
+     * Values in the cfg object will be used instead of the values on
+     * the yoob.Line object.
+     */
     this.drawOverride = function(ctx, cfg) {
         cfg = cfg || {};
         this._draw(ctx,
 
     this.init(cfg);
 };
+
+yoob.PathSet = function() {
+    this.init = function(paths) {
+        this.paths = paths || [];
+        return this;
+    };
+
+    this.toString = function() {
+        var t = 'new yoob.PathSet([';
+        for (var i = 0; i < this.paths.length; i++) {
+            t += this.paths[i].toString();
+            if (i < this.paths.length - 1) {
+                t += ', ';
+            }
+        }
+        return t + '])';
+    };
+
+    this.add = function(path) {
+        if (path !== undefined)
+            this.paths.push(path);
+    };
+
+    this.clear = function(path) {
+        this.paths = [];
+    };
+
+    /*
+     * Values in the cfg object will be used when they are not specified on
+     * a yoob.Line object.
+     */
+    this.draw = function(ctx, cfg) {
+        for (var i = 0; i < this.paths.length; i++) {
+            this.paths[i].draw(ctx, cfg);
+        }
+    };
+
+    /*
+     * Values in the cfg object will be used instead of the values on
+     * a yoob.Line object.
+     */
+    this.drawOverride = function(ctx, cfg) {
+        for (var i = 0; i < this.paths.length; i++) {
+            this.paths[i].drawOverride(ctx, cfg);
+        }
+    };
+};

File src/yoob/turtle.js

 if (window.yoob === undefined) yoob = {};
 
 /*
- * requires yoob.Sprite.
+ * requires yoob.Sprite and yoob.Path.
  *
  * I really haven't worked this out fully yet.
  * It could also have a velocity, and dx/dy = cos/sin(theta) * velocity.
         this.dy = 0;
         this.selected = false;
         this.theta = 0;
-        this.trail = [this.getCenterX(), this.getCenterY()];
-        this.penDown = true;
+        this.trail = new yoob.PathSet().init();
+        this.setPenDown(true);
     };
 
     this.setPenDown = function(penDown) {
-        this.penDown = !!penDown;
+        penDown = !!penDown;
+        if (penDown === this.penDown) return;
+        this.penDown = penDown;
+        if (this.penDown) {
+            this.trail.add(this.curPath);
+            this.curPath = new yoob.Path().init();
+            this.curPath.addPoint(this.getCenterX(), this.getCenterY());
+        }
     };
 
     /* theta is in radians */
     this.moveBy = function(units) {
         this.x += this.dx * units;
         this.y += this.dy * units;
-        this.trail.push(this.getCenterX());
-        this.trail.push(this.getCenterY());
+        if (this.penDown) {
+            this.curPath.addPoint(this.getCenterX(), this.getCenterY());
+        }
     };
 
     this.draw = function(ctx) {
         ctx.stroke();
     };
 
-    this.drawTrail = function(ctx) {
-        if (this.trail.length < 2) return;
-        ctx.strokeStyle = "black";
-        ctx.lineWidth = 1;
-        ctx.beginPath();
-        ctx.moveTo(this.trail[0], this.trail[1]);
-        for (var i = 2; i < this.trail.length; i += 2) {
-            ctx.lineTo(this.trail[i], this.trail[i + 1]);
-        }
-        ctx.stroke();
+    this.drawTrail = function(ctx, cfg) {
+        cfg = cfg || {
+            strokeStyle: "black",
+            lineWidth: 1
+        };
+        this.trail.drawOverride(ctx, cfg);
+        this.curPath.drawOverride(ctx, cfg);
     };
 };
 yoob.Turtle.prototype = new yoob.Sprite();