// SPDX-FileCopyrightText: Chris Pressey, the original author of this work, has dedicated it to the public domain.
// For more information, please refer to <https://unlicense.org/>
// SPDX-License-Identifier: Unlicense
Playfield = function() {
this._store = {};
this.min_x = undefined;
this.min_y = undefined;
this.max_x = undefined;
this.max_y = undefined;
this.get = function(x, y) {
return this._store[x+','+y];
};
this.put = function(x, y, value) {
if (this.min_x === undefined || x < this.min_x) this.min_x = x;
if (this.max_x === undefined || x > this.max_x) this.max_x = x;
if (this.min_y === undefined || y < this.min_y) this.min_y = y;
if (this.max_y === undefined || y > this.max_y) this.max_y = y;
if (value === undefined) {
delete this._store[x+','+y];
}
this._store[x+','+y] = value;
};
this.foreach = function(fun) {
for (var y = this.min_y; y <= this.max_y; y++) {
for (var x = this.min_x; x <= this.max_x; x++) {
var key = x+','+y;
var value = this._store[key];
if (value === undefined)
continue;
var result = fun(x, y, value);
if (result !== undefined) {
if (result === ' ') {
result = undefined;
}
this.put(x, y, result);
}
}
}
};
this.load = function(x, y, map, string) {
var lx = x;
var ly = y;
for (var i = 0; i < string.length; i++) {
var c = string.charAt(i);
if (c === '\n') {
lx = x;
ly++;
} else if (c === ' ') {
this.put(lx, ly, undefined);
lx++;
} else if (c === '\r') {
} else {
this.put(lx, ly, map[c]);
lx++;
}
}
};
};