Both Playfield and Tape support "cursored read/write interface."
Chris Pressey
9 years ago
142 | 142 | this.isHeaded = function(dx, dy) { |
143 | 143 | return this.dx === dx && this.dy === dy; |
144 | 144 | }; |
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 | }; | |
156 | 145 | } |
9 | 9 | */ |
10 | 10 | yoob.Playfield = function() { |
11 | 11 | 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 || {}; | |
17 | 13 | this._default = cfg.defaultValue; |
18 | 14 | this.cursors = cfg.cursors || []; |
15 | this.clear(); | |
19 | 16 | return this; |
20 | 17 | }; |
21 | 18 | |
118 | 115 | this.minY = undefined; |
119 | 116 | this.maxX = undefined; |
120 | 117 | this.maxY = undefined; |
118 | return this; | |
121 | 119 | }; |
122 | 120 | |
123 | 121 | /* |
387 | 385 | return this.getUpperY() - this.getLowerY() + 1; |
388 | 386 | } |
389 | 387 | }; |
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 | }; | |
390 | 402 | }; |
0 | 0 | /* |
1 | * This file is part of yoob.js version 0.9 | |
1 | * This file is part of yoob.js version 0.11-PRE | |
2 | 2 | * Available from https://github.com/catseye/yoob.js/ |
3 | 3 | * This file is in the public domain. See http://unlicense.org/ for details. |
4 | 4 | */ |
10 | 10 | yoob.Tape = function() { |
11 | 11 | this.init = function(cfg) { |
12 | 12 | cfg = cfg || {}; |
13 | this.default = cfg.default; | |
13 | this._default = cfg.defaultValue; | |
14 | this.cursors = cfg.cursors || []; | |
14 | 15 | this.clear(); |
15 | 16 | return this; |
16 | 17 | }; |
31 | 32 | */ |
32 | 33 | this.get = function(pos) { |
33 | 34 | var val = this._store[pos]; |
34 | return val === undefined ? this.default : val; | |
35 | return val === undefined ? this._default : val; | |
35 | 36 | }; |
36 | 37 | |
37 | 38 | /* |
40 | 41 | this.put = function(pos, value) { |
41 | 42 | if (this.min === undefined || pos < this.min) this.min = pos; |
42 | 43 | if (this.max === undefined || pos > this.max) this.max = pos; |
43 | if (value === this.default) { | |
44 | if (value === this._default) { | |
44 | 45 | delete this._store[pos]; |
45 | 46 | } |
46 | 47 | this._store[pos] = value; |
63 | 64 | var value = this._store[pos]; |
64 | 65 | if (value === undefined) { |
65 | 66 | if (dense) { |
66 | value = this.default; | |
67 | value = this._default; | |
67 | 68 | } else { |
68 | 69 | continue; |
69 | 70 | } |
73 | 74 | this.put(pos, result); |
74 | 75 | } |
75 | 76 | } |
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; | |
76 | 91 | }; |
77 | 92 | |
78 | 93 | /* |