Close, but I think we need a default state attr on playfield.
catseye
12 years ago
37 | 37 | return this._store[x+','+y]; |
38 | 38 | }; |
39 | 39 | |
40 | /* TODO: better bounds recalculation */ | |
41 | 40 | this.put = function(x, y, value) { |
42 | 41 | if (value === undefined) { |
43 | 42 | delete this._store[x+','+y]; |
44 | return; | |
43 | } else { | |
44 | this._store[x+','+y] = value; | |
45 | 45 | } |
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 | } | |
51 | 67 | }; |
52 | 68 | }; |
53 | 69 | |
68 | 84 | function evolve_playfield(pf, new_pf) { |
69 | 85 | for (var y = pf.min_y - %d; y <= pf.max_y - %d; y++) { |
70 | 86 | 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)); | |
72 | 88 | } |
73 | 89 | } |
90 | new_pf.recalculate_limits(); | |
74 | 91 | } |
75 | 92 | """ % (bb.max_dy, bb.min_dy, bb.max_dx, bb.min_dx)) |
76 | 93 | class_map = get_class_map(self.alpaca) |
93 | 110 | self.file.write("pf = new Playfield();\n") |
94 | 111 | for (x, y, c) in pf.iteritems(): |
95 | 112 | self.file.write("pf.put(%d, %d, '%s');\n" % (x, y, c)) |
113 | self.file.write("pf.recalculate_limits();\n") | |
96 | 114 | self.file.write(""" |
97 | 115 | function dump_playfield(pf) { |
98 | 116 | for (var y = pf.min_y; y <= pf.max_y; y++) { |