Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@nasser
Created October 15, 2018 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nasser/d19972a4ce73e7bcdb9b07e7b7139ef2 to your computer and use it in GitHub Desktop.
Save nasser/d19972a4ce73e7bcdb9b07e7b7139ef2 to your computer and use it in GitHub Desktop.
minimal janky port of entt to js
function ComponentSet() {
this.entities = [];
this.indices = []; // could be a hashmap
this.components = [];
this.size = 0;
}
ComponentSet.prototype.insert = function (entity, component) {
this.entities.push(entity);
this.components.push(component);
this.indices[entity] = this.size;
this.size++;
};
ComponentSet.prototype.replace = function (entity, component) {
this.components[this.indices[entity]] = component;
};
ComponentSet.prototype.assign = function (entity, component) {
if(this.contains(entity))
this.replace(entity, component);
else
this.insert(entity, component);
}
ComponentSet.prototype.remove = function (entity) {
if(!this.contains(entity)) return;
let last = this.entities[this.size-1];
this.entities[this.indices[entity]] = last;
this.indices[last] = this.indices[entity];
this.size--;
};
ComponentSet.prototype.get = function (entity) {
return this.components[this.indices[entity]];
};
ComponentSet.prototype.contains = function (entity) {
return this.entities[this.indices[entity]] == entity;
};
ComponentSet.prototype.eachEntity = function (f) {
for (var i = this.size-1; i >= 0; i--) {
f(this.entities[i]);
}
}
ComponentSet.prototype.eachComponent = function (f) {
for (var i = this.size-1; i >= 0; i--) {
f(this.components[i]);
}
}
ComponentSet.prototype.each = function (f) {
for (var i = this.size-1; i >= 0; i--) {
f(this.entities[i], this.components[i]);
}
}
/*****************************/
function View(componentSets) {
this.componentSets = componentSets == undefined ? [] : componentSets;
}
View.prototype.each = function (f) {
if(this.componentSets.length == 0) return;
if(this.componentSets.length == 1) return this.componentSets[0].each(f);
let smallestSet = this.componentSets[0];
for (var i = this.componentSets.length-1; i >= 0; i--)
if(this.componentSets[i].size < smallestSet)
smallestSet = this.componentSets[i];
var componentCount = this.componentSets.length;
var arguments = new Array(componentCount+1);
for (var i = smallestSet.size-1; i >= 0; i--) {
let e = smallestSet.entities[i];
arguments[0] = e;
var j=0;
for (; j < componentCount; j++) {
if(!this.componentSets[j].contains(e))
break;
arguments[j+1] = this.componentSets[j].get(e);
}
if(j < componentCount) continue;
f.apply(null, arguments)
}
};
let positions = new ComponentSet();
let names = new ComponentSet();
positions.assign(7, {x:90, y:12})
positions.assign(3, {x:90, y:12})
positions.assign(9, {x:90, y:12})
positions.assign(9, {x:1, y:3})
names.assign(3, "ramsey")
names.assign(9, "jim")
names.assign(8, "absent")
positions.remove(3);
var v = new View([positions, names])
let i = 4;
// names.each((e, c) => console.log(e, c))
v.each((e, n, c) => console.log("view", e, n, c))
// console.log(names)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment