diff --git a/prosperon/examples/chess/grid.cm b/prosperon/examples/chess/grid.cm index 868f90a7..c6cdffa4 100644 --- a/prosperon/examples/chess/grid.cm +++ b/prosperon/examples/chess/grid.cm @@ -1,57 +1,69 @@ -var CELLS = Symbol() - -var key = function key(x,y) { return `${x},${y}` } - -function grid(w, h) -{ - this[CELLS] = new Map() - this.width = w; +function grid(w, h) { + this.width = w; this.height = h; + // create a height×width array of empty lists + this.cells = new Array(h); + for (let y = 0; y < h; y++) { + this.cells[y] = new Array(w); + for (let x = 0; x < w; x++) { + this.cells[y][x] = []; // each cell holds its own list + } + } } grid.prototype = { - cell(x,y) { - var k = key(x,y) - if (!this[CELLS].has(k)) this[CELLS].set(k,[]) - return this[CELLS].get(k) + // return the array at (x,y) + cell(x, y) { + return this.cells[y][x]; }, - + + // alias for cell + at(pos) { + return this.cell(pos.x, pos.y); + }, + + // add an entity into a cell add(entity, pos) { this.cell(pos.x, pos.y).push(entity); entity.coord = pos.slice(); }, - - remove(entity, pos) { - var c = this.cell(pos.x, pos.y); - c.splice(c.indexOf(entity), 1); - }, - - at(pos) { - return this.cell(pos.x, pos.y); - }, - - inBounds(pos) { - return pos.x >= 0 && pos.x < this.width && pos.y >= 0 && pos.y < this.height; - }, - - each(fn) { - for (var [k, list] of this[CELLS]) - for (var p of list) fn(p, p.coord); - }, - - toString() { - var out = `grid [${this.width}x${this.height}] -` - for (var y = 0; y < this.height; y++) { - for (var x = 0; x < this.width; x++) { - var cell = this.at([x,y]); - out += cell.length - } - if (y != this.height - 1) out += "\n" - } - - return out - }, -} -return grid + // remove an entity from a cell + remove(entity, pos) { + const c = this.cell(pos.x, pos.y); + const i = c.indexOf(entity); + if (i !== -1) c.splice(i, 1); + }, + + // bounds check + inBounds(pos) { + return ( + pos.x >= 0 && pos.x < this.width && + pos.y >= 0 && pos.y < this.height + ); + }, + + // call fn(entity, coord) for every entity in every cell + each(fn) { + for (let y = 0; y < this.height; y++) { + for (let x = 0; x < this.width; x++) { + const list = this.cells[y][x]; + for (let entity of list) { + fn(entity, entity.coord); + } + } + } + }, + + // printable representation + toString() { + let out = `grid [${this.width}×${this.height}]\n`; + for (let y = 0; y < this.height; y++) { + for (let x = 0; x < this.width; x++) { + out += this.cells[y][x].length; + } + if (y !== this.height - 1) out += "\n"; + } + return out; + } +}; diff --git a/prosperon/graphics.cm b/prosperon/graphics.cm index 48d7454c..331268f6 100644 --- a/prosperon/graphics.cm +++ b/prosperon/graphics.cm @@ -18,7 +18,7 @@ var CPU = Symbol() var LASTUSE = Symbol() var LOADING = Symbol() -var cache = new Map() +var cache = {} // Image constructor function graphics.Image = function(surfaceData) { @@ -250,14 +250,14 @@ graphics.texture = function texture(path) { throw new Error('need a string for graphics.texture') var id = path //.split(':')[0] - if (cache.has(id)) return cache.get(id) + if (cache[id]) return cache[id] var ipath = res.find_image(id) if (!ipath) throw new Error(`unknown image ${id}`) var image = create_image(ipath) - cache.set(id, image) + cache[id] = image return image } graphics.texture[cell.DOC] = ` diff --git a/prosperon/resources.cm b/prosperon/resources.cm index d170f973..80eaa962 100644 --- a/prosperon/resources.cm +++ b/prosperon/resources.cm @@ -2,12 +2,12 @@ var io = use('io') Object.defineProperty(Function.prototype, "hashify", { value: function () { - var hash = new Map() + var hash = {} var fn = this function hashified(...args) { var key = args[0] - if (!hash.has(key)) hash.set(key, fn(...args)) - return hash.get(key) + if (hash[key] == null) hash[key] = fn(...args) + return hash[key] } return hashified }, diff --git a/scripts/engine.cm b/scripts/engine.cm index 68999d8c..46f3dfaa 100644 --- a/scripts/engine.cm +++ b/scripts/engine.cm @@ -425,7 +425,7 @@ $_.connection[cell.DOC] = "The connection function takes a callback function, an var peers = {} var id_address = {} -var peer_queue = new Map() +var peer_queue = {} var portal = null var portal_fn = null