git @ Cat's Eye Technologies yoob.js / 9ba5dd8
Both Playfield and Tape support "cursored read/write interface." Chris Pressey 9 years ago
3 changed file(s) with 37 addition(s) and 21 deletion(s). Raw diff Collapse all Expand all
142142 this.isHeaded = function(dx, dy) {
143143 return this.dx === dx && this.dy === dy;
144144 };
145
146 this.read = function() {
147 if (!this.tape) return undefined;
148 return this.tape.get(this.x);
149 };
150
151 this.write = function(value) {
152 if (!this.tape) return;
153 this.tape.put(this.x, value);
154 return this;
155 };
156145 }
99 */
1010 yoob.Playfield = function() {
1111 this.init = function(cfg) {
12 this._store = {};
13 this.minX = undefined;
14 this.minY = undefined;
15 this.maxX = undefined;
16 this.maxY = undefined;
12 cfg = cfg || {};
1713 this._default = cfg.defaultValue;
1814 this.cursors = cfg.cursors || [];
15 this.clear();
1916 return this;
2017 };
2118
118115 this.minY = undefined;
119116 this.maxX = undefined;
120117 this.maxY = undefined;
118 return this;
121119 };
122120
123121 /*
387385 return this.getUpperY() - this.getLowerY() + 1;
388386 }
389387 };
388
389 /*
390 * Cursored read/write interface
391 */
392 this.read = function(index) {
393 var cursor = this.cursors[index || 0];
394 return this.get(cursor.getX(), cursor.getY());
395 };
396
397 this.write = function(value, index) {
398 var cursor = this.cursors[index || 0];
399 this.put(cursor.getX(), cursor.getY(), value);
400 return this;
401 };
390402 };
00 /*
1 * This file is part of yoob.js version 0.9
1 * This file is part of yoob.js version 0.11-PRE
22 * Available from https://github.com/catseye/yoob.js/
33 * This file is in the public domain. See http://unlicense.org/ for details.
44 */
1010 yoob.Tape = function() {
1111 this.init = function(cfg) {
1212 cfg = cfg || {};
13 this.default = cfg.default;
13 this._default = cfg.defaultValue;
14 this.cursors = cfg.cursors || [];
1415 this.clear();
1516 return this;
1617 };
3132 */
3233 this.get = function(pos) {
3334 var val = this._store[pos];
34 return val === undefined ? this.default : val;
35 return val === undefined ? this._default : val;
3536 };
3637
3738 /*
4041 this.put = function(pos, value) {
4142 if (this.min === undefined || pos < this.min) this.min = pos;
4243 if (this.max === undefined || pos > this.max) this.max = pos;
43 if (value === this.default) {
44 if (value === this._default) {
4445 delete this._store[pos];
4546 }
4647 this._store[pos] = value;
6364 var value = this._store[pos];
6465 if (value === undefined) {
6566 if (dense) {
66 value = this.default;
67 value = this._default;
6768 } else {
6869 continue;
6970 }
7374 this.put(pos, result);
7475 }
7576 }
77 };
78
79 /*
80 * Cursored read/write interface
81 */
82 this.read = function(index) {
83 var cursor = this.cursors[index || 0];
84 return this.get(cursor.getX());
85 };
86
87 this.write = function(value, index) {
88 var cursor = this.cursors[index || 0];
89 this.put(cursor.getX(), value);
90 return this;
7691 };
7792
7893 /*