git @ Cat's Eye Technologies ALPACA / 348b963
Close, but I think we need a default state attr on playfield. catseye 12 years ago
1 changed file(s) with 26 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
3737 return this._store[x+','+y];
3838 };
3939
40 /* TODO: better bounds recalculation */
4140 this.put = function(x, y, value) {
4241 if (value === undefined) {
4342 delete this._store[x+','+y];
44 return;
43 } else {
44 this._store[x+','+y] = value;
4545 }
46 if (this.min_x === undefined || x < this.min_x) this.min_x = x;
47 if (this.max_x === undefined || x > this.max_x) this.max_x = x;
48 if (this.min_y === undefined || y < this.min_y) this.min_y = y;
49 if (this.max_y === undefined || y > this.max_y) this.max_y = y;
50 this._store[x+','+y] = value;
46 };
47
48 this.recalculate_limits = function() {
49 this.min_x = undefined;
50 this.min_y = undefined;
51 this.max_x = undefined;
52 this.max_y = undefined;
53
54 for (var cell in this._store) {
55 var pos = cell.split(',');
56 var x = parseInt(pos[0], 10);
57 var y = parseInt(pos[1], 10);
58 if (this.min_x === undefined || this.min_x > x)
59 this.min_x = x;
60 if (this.max_x === undefined || this.max_x < x)
61 this.max_x = x;
62 if (this.min_y === undefined || this.min_y > y)
63 this.min_y = y;
64 if (this.max_y === undefined || this.max_y < y)
65 this.max_y = y;
66 }
5167 };
5268 };
5369
6884 function evolve_playfield(pf, new_pf) {
6985 for (var y = pf.min_y - %d; y <= pf.max_y - %d; y++) {
7086 for (var x = pf.min_x - %d; x <= pf.max_x - %d; x++) {
71 new_pf.put(x, y, evalState(pf, x, y))
87 new_pf.put(x, y, evalState(pf, x, y));
7288 }
7389 }
90 new_pf.recalculate_limits();
7491 }
7592 """ % (bb.max_dy, bb.min_dy, bb.max_dx, bb.min_dx))
7693 class_map = get_class_map(self.alpaca)
93110 self.file.write("pf = new Playfield();\n")
94111 for (x, y, c) in pf.iteritems():
95112 self.file.write("pf.put(%d, %d, '%s');\n" % (x, y, c))
113 self.file.write("pf.recalculate_limits();\n")
96114 self.file.write("""
97115 function dump_playfield(pf) {
98116 for (var y = pf.min_y; y <= pf.max_y; y++) {