@javelin/ecs
Version:
80 lines • 2.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createArchetype = void 0;
const core_1 = require("@javelin/core");
const component_1 = require("./component");
const type_1 = require("./type");
function hydrateTableFromSnapshot(table, type) {
return table.map((column, i) => {
const schemaId = type[i];
return column
.slice()
.map(component => component_1.toComponentFromType(component, schemaId));
});
}
function createArchetypeStateFromSnapshot(snapshot) {
const entities = Object.keys(snapshot.indices).map(Number);
const indices = core_1.unpackSparseArray(snapshot.indices);
const type = type_1.normalizeType(snapshot.type);
const table = hydrateTableFromSnapshot(snapshot.table, type);
return { entities, indices, type, table };
}
function createStore(options) {
if ("snapshot" in options) {
return createArchetypeStateFromSnapshot(options.snapshot);
}
const type = type_1.normalizeType(options.type);
const table = type.map(() => []);
return { entities: [], indices: [], type, table };
}
function invertSignature(signature) {
return signature.reduce((a, x, i) => {
a[x] = i;
return a;
}, []);
}
/**
* Create an Archetype.
* @param signature
* @param table
*/
function createArchetype(options) {
const { table, indices, entities, type } = createStore(options);
const typeInverse = invertSignature(type);
function insert(entity, components) {
for (let i = 0; i < components.length; i++) {
const component = components[i];
const schemaIndex = typeInverse[component_1.getSchemaId(component)];
table[schemaIndex].push(component);
}
indices[entity] = entities.push(entity) - 1;
}
function remove(entity) {
const length = entities.length;
const index = indices[entity];
const head = entities.pop();
delete indices[entity];
if (index === length - 1) {
for (const column of table)
column.pop();
}
else {
for (const column of table) {
column[index] = column.pop();
}
entities[index] = head;
indices[head] = index;
}
}
return {
entities,
indices,
insert,
remove,
type,
typeInverse,
table,
};
}
exports.createArchetype = createArchetype;
//# sourceMappingURL=archetype.js.map