remove usage of map

This commit is contained in:
2025-06-17 21:16:35 -05:00
parent 41fdf49df5
commit 843b4bd8a8
4 changed files with 66 additions and 54 deletions

View File

@@ -1,57 +1,69 @@
var CELLS = Symbol()
var key = function key(x,y) { return `${x},${y}` }
function grid(w, h)
{
this[CELLS] = new Map()
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 = {
// return the array at (x,y)
cell(x, y) {
var k = key(x,y)
if (!this[CELLS].has(k)) this[CELLS].set(k,[])
return this[CELLS].get(k)
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 an entity from a cell
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);
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;
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 (var [k, list] of this[CELLS])
for (var p of list) fn(p, p.coord);
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() {
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
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"
if (y !== this.height - 1) out += "\n";
}
return out
},
return out;
}
return grid
};

View File

@@ -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] = `

View File

@@ -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
},

View File

@@ -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