remove usage of map
This commit is contained in:
@@ -1,57 +1,69 @@
|
|||||||
var CELLS = Symbol()
|
function grid(w, h) {
|
||||||
|
this.width = w;
|
||||||
var key = function key(x,y) { return `${x},${y}` }
|
|
||||||
|
|
||||||
function grid(w, h)
|
|
||||||
{
|
|
||||||
this[CELLS] = new Map()
|
|
||||||
this.width = w;
|
|
||||||
this.height = h;
|
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 = {
|
grid.prototype = {
|
||||||
cell(x,y) {
|
// return the array at (x,y)
|
||||||
var k = key(x,y)
|
cell(x, y) {
|
||||||
if (!this[CELLS].has(k)) this[CELLS].set(k,[])
|
return this.cells[y][x];
|
||||||
return this[CELLS].get(k)
|
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// alias for cell
|
||||||
|
at(pos) {
|
||||||
|
return this.cell(pos.x, pos.y);
|
||||||
|
},
|
||||||
|
|
||||||
|
// add an entity into a cell
|
||||||
add(entity, pos) {
|
add(entity, pos) {
|
||||||
this.cell(pos.x, pos.y).push(entity);
|
this.cell(pos.x, pos.y).push(entity);
|
||||||
entity.coord = pos.slice();
|
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;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ var CPU = Symbol()
|
|||||||
var LASTUSE = Symbol()
|
var LASTUSE = Symbol()
|
||||||
var LOADING = Symbol()
|
var LOADING = Symbol()
|
||||||
|
|
||||||
var cache = new Map()
|
var cache = {}
|
||||||
|
|
||||||
// Image constructor function
|
// Image constructor function
|
||||||
graphics.Image = function(surfaceData) {
|
graphics.Image = function(surfaceData) {
|
||||||
@@ -250,14 +250,14 @@ graphics.texture = function texture(path) {
|
|||||||
throw new Error('need a string for graphics.texture')
|
throw new Error('need a string for graphics.texture')
|
||||||
|
|
||||||
var id = path //.split(':')[0]
|
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)
|
var ipath = res.find_image(id)
|
||||||
if (!ipath)
|
if (!ipath)
|
||||||
throw new Error(`unknown image ${id}`)
|
throw new Error(`unknown image ${id}`)
|
||||||
|
|
||||||
var image = create_image(ipath)
|
var image = create_image(ipath)
|
||||||
cache.set(id, image)
|
cache[id] = image
|
||||||
return image
|
return image
|
||||||
}
|
}
|
||||||
graphics.texture[cell.DOC] = `
|
graphics.texture[cell.DOC] = `
|
||||||
|
|||||||
@@ -2,12 +2,12 @@ var io = use('io')
|
|||||||
|
|
||||||
Object.defineProperty(Function.prototype, "hashify", {
|
Object.defineProperty(Function.prototype, "hashify", {
|
||||||
value: function () {
|
value: function () {
|
||||||
var hash = new Map()
|
var hash = {}
|
||||||
var fn = this
|
var fn = this
|
||||||
function hashified(...args) {
|
function hashified(...args) {
|
||||||
var key = args[0]
|
var key = args[0]
|
||||||
if (!hash.has(key)) hash.set(key, fn(...args))
|
if (hash[key] == null) hash[key] = fn(...args)
|
||||||
return hash.get(key)
|
return hash[key]
|
||||||
}
|
}
|
||||||
return hashified
|
return hashified
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -425,7 +425,7 @@ $_.connection[cell.DOC] = "The connection function takes a callback function, an
|
|||||||
|
|
||||||
var peers = {}
|
var peers = {}
|
||||||
var id_address = {}
|
var id_address = {}
|
||||||
var peer_queue = new Map()
|
var peer_queue = {}
|
||||||
var portal = null
|
var portal = null
|
||||||
var portal_fn = null
|
var portal_fn = null
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user