chore: naming

This commit is contained in:
cha0s 2024-06-11 19:10:57 -05:00
parent 7ccfe492cd
commit fc281a90c7
5 changed files with 137 additions and 137 deletions

View File

@ -31,9 +31,9 @@ export default class Arbitrary extends Base {
const allocated = this.allocateMany(entries.length); const allocated = this.allocateMany(entries.length);
const keys = Object.keys(this.constructor.schema.specification); const keys = Object.keys(this.constructor.schema.specification);
for (let i = 0; i < entries.length; ++i) { for (let i = 0; i < entries.length; ++i) {
const [entity, values = {}] = entries[i]; const [entityId, values = {}] = entries[i];
this.map[entity] = allocated[i]; this.map[entityId] = allocated[i];
this.data[allocated[i]].entity = entity; this.data[allocated[i]].entity = entityId;
if (false === values) { if (false === values) {
continue; continue;
} }
@ -51,16 +51,16 @@ export default class Arbitrary extends Base {
} }
} }
deserialize(entity, view, offset) { deserialize(entityId, view, offset) {
this.serializer.decode(view, this.get(entity), offset); this.serializer.decode(view, this.get(entityId), offset);
} }
serialize(entity, view, offset) { serialize(entityId, view, offset) {
this.serializer.encode(this.get(entity), view, offset); this.serializer.encode(this.get(entityId), view, offset);
} }
get(entity) { get(entityId) {
return this.data[this.map[entity]]; return this.data[this.map[entityId]];
} }
instanceFromSchema() { instanceFromSchema() {

View File

@ -14,22 +14,22 @@ export default class Base {
return results; return results;
} }
create(entity, values) { create(entityId, values) {
this.createMany([[entity, values]]); this.createMany([[entityId, values]]);
} }
destroy(entity) { destroy(entityId) {
this.destroyMany([entity]); this.destroyMany([entityId]);
} }
destroyMany(entities) { destroyMany(entities) {
this.freeMany( this.freeMany(
entities entities
.map((entity) => { .map((entityId) => {
if ('undefined' !== typeof this.map[entity]) { if ('undefined' !== typeof this.map[entityId]) {
return this.map[entity]; return this.map[entityId];
} }
throw new Error(`can't free for non-existent entity ${entity}`); throw new Error(`can't free for non-existent id ${entityId}`);
}), }),
); );
for (let i = 0; i < entities.length; i++) { for (let i = 0; i < entities.length; i++) {
@ -56,12 +56,12 @@ export default class Base {
insertMany(entities) { insertMany(entities) {
const creating = []; const creating = [];
for (let i = 0; i < entities.length; i++) { for (let i = 0; i < entities.length; i++) {
const [entity, values] = entities[i]; const [entityId, values] = entities[i];
if (!this.get(entity)) { if (!this.get(entityId)) {
creating.push([entity, values]); creating.push([entityId, values]);
} }
else { else {
const instance = this.get(entity); const instance = this.get(entityId);
for (const i in values) { for (const i in values) {
instance[i] = values[i]; instance[i] = values[i];
} }
@ -71,20 +71,20 @@ export default class Base {
} }
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
markChange(entity, components) {} markChange(entityId, components) {}
mergeDiff(original, update) { mergeDiff(original, update) {
return {...original, ...update}; return {...original, ...update};
} }
sizeOf(entity) { sizeOf(entityId) {
return this.constructor.schema.sizeOf(this.get(entity)); return this.constructor.schema.sizeOf(this.get(entityId));
} }
static wrap(name, ecs) { static wrap(name, ecs) {
class WrappedComponent extends this { class WrappedComponent extends this {
markChange(entity, key, value) { markChange(entityId, key, value) {
ecs.markChange(entity, {[name]: {[key]: value}}) ecs.markChange(entityId, {[name]: {[key]: value}})
} }
} }
return new WrappedComponent(); return new WrappedComponent();

View File

@ -36,10 +36,10 @@ export default class Ecs {
const destroying = []; const destroying = [];
const removing = []; const removing = [];
const updating = []; const updating = [];
for (const id in patch) { for (const entityId in patch) {
const components = patch[id]; const components = patch[entityId];
if (false === components) { if (false === components) {
destroying.push(id); destroying.push(entityId);
continue; continue;
} }
const componentsToRemove = []; const componentsToRemove = [];
@ -53,13 +53,13 @@ export default class Ecs {
} }
} }
if (componentsToRemove.length > 0) { if (componentsToRemove.length > 0) {
removing.push([id, componentsToRemove]); removing.push([entityId, componentsToRemove]);
} }
if (this.$$entities[id]) { if (this.$$entities[entityId]) {
updating.push([id, componentsToUpdate]); updating.push([entityId, componentsToUpdate]);
} }
else { else {
creating.push([id, componentsToUpdate]); creating.push([entityId, componentsToUpdate]);
} }
} }
this.destroyMany(destroying); this.destroyMany(destroying);
@ -69,8 +69,8 @@ export default class Ecs {
} }
create(components = {}) { create(components = {}) {
const [entity] = this.createMany([components]); const [entityId] = this.createMany([components]);
return entity; return entityId;
} }
createMany(componentsList) { createMany(componentsList) {
@ -82,40 +82,40 @@ export default class Ecs {
} }
createManySpecific(specificsList) { createManySpecific(specificsList) {
const entities = []; const entityIds = [];
const creating = {}; const creating = {};
for (let i = 0; i < specificsList.length; i++) { for (let i = 0; i < specificsList.length; i++) {
const [entity, components] = specificsList[i]; const [entityId, components] = specificsList[i];
const componentKeys = []; const componentKeys = [];
for (const key of Object.keys(components)) { for (const key of Object.keys(components)) {
if (this.Types[key]) { if (this.Types[key]) {
componentKeys.push(key); componentKeys.push(key);
} }
} }
entities.push(entity); entityIds.push(entityId);
this.rebuild(entity, () => componentKeys); this.rebuild(entityId, () => componentKeys);
for (const component of componentKeys) { for (const component of componentKeys) {
if (!creating[component]) { if (!creating[component]) {
creating[component] = []; creating[component] = [];
} }
creating[component].push([entity, components[component]]); creating[component].push([entityId, components[component]]);
} }
this.markChange(entity, components); this.markChange(entityId, components);
} }
for (const i in creating) { for (const i in creating) {
this.Types[i].createMany(creating[i]); this.Types[i].createMany(creating[i]);
} }
this.reindex(entities); this.reindex(entityIds);
return entities; return entityIds;
} }
createSpecific(entity, components) { createSpecific(entityId, components) {
return this.createManySpecific([[entity, components]]); return this.createManySpecific([[entityId, components]]);
} }
deindex(entities) { deindex(entityIds) {
for (let i = 0; i < this.$$systems.length; i++) { for (let i = 0; i < this.$$systems.length; i++) {
this.$$systems[i].deindex(entities); this.$$systems[i].deindex(entityIds);
} }
} }
@ -129,74 +129,74 @@ export default class Ecs {
const updating = new Map(); const updating = new Map();
const cursors = new Map(); const cursors = new Map();
for (let i = 0; i < count; ++i) { for (let i = 0; i < count; ++i) {
const entity = view.getUint32(cursor, true); const entityId = view.getUint32(cursor, true);
if (!ecs.$$entities[entity]) { if (!ecs.$$entities[entityId]) {
creating.set(entity, {}); creating.set(entityId, {});
} }
cursor += 4; cursor += 4;
const componentCount = view.getUint16(cursor, true); const componentCount = view.getUint16(cursor, true);
cursor += 2; cursor += 2;
cursors.set(entity, {}); cursors.set(entityId, {});
const addedComponents = []; const addedComponents = [];
for (let j = 0; j < componentCount; ++j) { for (let j = 0; j < componentCount; ++j) {
const id = view.getUint16(cursor, true); const componentId = view.getUint16(cursor, true);
cursor += 2; cursor += 2;
const component = keys[id]; const component = keys[componentId];
if (!component) { if (!component) {
throw new Error(`can't decode component ${id}`); throw new Error(`can't decode component ${componentId}`);
} }
if (!ecs.$$entities[entity]) { if (!ecs.$$entities[entityId]) {
creating.get(entity)[component] = false; creating.get(entityId)[component] = false;
} }
else if (!ecs.$$entities[entity].constructor.types.includes(component)) { else if (!ecs.$$entities[entityId].constructor.types.includes(component)) {
addedComponents.push(component); addedComponents.push(component);
if (!updating.has(component)) { if (!updating.has(component)) {
updating.set(component, []); updating.set(component, []);
} }
updating.get(component).push([entity, false]); updating.get(component).push([entityId, false]);
} }
cursors.get(entity)[component] = cursor; cursors.get(entityId)[component] = cursor;
cursor += ecs.Types[component].constructor.schema.readSize(view, cursor); cursor += ecs.Types[component].constructor.schema.readSize(view, cursor);
} }
if (addedComponents.length > 0 && ecs.$$entities[entity]) { if (addedComponents.length > 0 && ecs.$$entities[entityId]) {
ecs.rebuild(entity, (types) => types.concat(addedComponents)); ecs.rebuild(entityId, (types) => types.concat(addedComponents));
} }
} }
ecs.createManySpecific(Array.from(creating.entries())); ecs.createManySpecific(Array.from(creating.entries()));
for (const [component, entities] of updating) { for (const [component, entityIds] of updating) {
ecs.Types[component].createMany(entities); ecs.Types[component].createMany(entityIds);
} }
for (const [entity, components] of cursors) { for (const [entityId, components] of cursors) {
for (const component in components) { for (const component in components) {
ecs.Types[component].deserialize(entity, view, components[component]); ecs.Types[component].deserialize(entityId, view, components[component]);
} }
} }
return ecs; return ecs;
} }
destroy(entity) { destroy(entityId) {
this.destroyMany([entity]); this.destroyMany([entityId]);
} }
destroyAll() { destroyAll() {
this.destroyMany(this.entities); this.destroyMany(this.entities);
} }
destroyMany(entities) { destroyMany(entityIds) {
const destroying = {}; const destroying = {};
this.deindex(entities); this.deindex(entityIds);
for (const entity of entities) { for (const entityId of entityIds) {
if (!this.$$entities[entity]) { if (!this.$$entities[entityId]) {
throw new Error(`can't destroy non-existent entity ${entity}`); throw new Error(`can't destroy non-existent entity ${entityId}`);
} }
for (const component of this.$$entities[entity].constructor.types) { for (const component of this.$$entities[entityId].constructor.types) {
if (!destroying[component]) { if (!destroying[component]) {
destroying[component] = []; destroying[component] = [];
} }
destroying[component].push(entity); destroying[component].push(entityId);
} }
this.$$entities[entity] = undefined; this.$$entities[entityId] = undefined;
this.diff[entity] = false; this.diff[entityId] = false;
} }
for (const i in destroying) { for (const i in destroying) {
this.Types[i].destroyMany(destroying[i]); this.Types[i].destroyMany(destroying[i]);
@ -222,29 +222,29 @@ export default class Ecs {
}; };
} }
get(entity) { get(entityId) {
return this.$$entities[entity]; return this.$$entities[entityId];
} }
insert(entity, components) { insert(entityId, components) {
this.insertMany([[entity, components]]); this.insertMany([[entityId, components]]);
} }
insertMany(entities) { insertMany(entities) {
const inserting = {}; const inserting = {};
const unique = new Set(); const unique = new Set();
for (const [entity, components] of entities) { for (const [entityId, components] of entities) {
this.rebuild(entity, (types) => [...new Set(types.concat(Object.keys(components)))]); this.rebuild(entityId, (types) => [...new Set(types.concat(Object.keys(components)))]);
const diff = {}; const diff = {};
for (const component in components) { for (const component in components) {
if (!inserting[component]) { if (!inserting[component]) {
inserting[component] = []; inserting[component] = [];
} }
diff[component] = {}; diff[component] = {};
inserting[component].push([entity, components[component]]); inserting[component].push([entityId, components[component]]);
} }
unique.add(entity); unique.add(entityId);
this.markChange(entity, diff); this.markChange(entityId, diff);
} }
for (const component in inserting) { for (const component in inserting) {
this.Types[component].insertMany(inserting[component]); this.Types[component].insertMany(inserting[component]);
@ -252,68 +252,68 @@ export default class Ecs {
this.reindex(unique.values()); this.reindex(unique.values());
} }
markChange(entity, components) { markChange(entityId, components) {
// Deleted? // Deleted?
if (false === components) { if (false === components) {
this.diff[entity] = false; this.diff[entityId] = false;
} }
// Created? // Created?
else if (!this.diff[entity]) { else if (!this.diff[entityId]) {
const filtered = {}; const filtered = {};
for (const type in components) { for (const type in components) {
filtered[type] = false === components[type] filtered[type] = false === components[type]
? false ? false
: this.Types[type].constructor.filterDefaults(components[type]); : this.Types[type].constructor.filterDefaults(components[type]);
} }
this.diff[entity] = filtered; this.diff[entityId] = filtered;
} }
// Otherwise, merge. // Otherwise, merge.
else { else {
for (const type in components) { for (const type in components) {
this.diff[entity][type] = false === components[type] this.diff[entityId][type] = false === components[type]
? false ? false
: this.Types[type].mergeDiff( : this.Types[type].mergeDiff(
this.diff[entity][type] || {}, this.diff[entityId][type] || {},
components[type], components[type],
); );
} }
} }
} }
rebuild(entity, types) { rebuild(entityId, types) {
let existing = []; let existing = [];
if (this.$$entities[entity]) { if (this.$$entities[entityId]) {
existing.push(...this.$$entities[entity].constructor.types); existing.push(...this.$$entities[entityId].constructor.types);
} }
const Class = this.$$entityFactory.makeClass(types(existing), this.Types); const Class = this.$$entityFactory.makeClass(types(existing), this.Types);
this.$$entities[entity] = new Class(entity); this.$$entities[entityId] = new Class(entityId);
} }
reindex(entities) { reindex(entityIds) {
for (let i = 0; i < this.$$systems.length; i++) { for (let i = 0; i < this.$$systems.length; i++) {
this.$$systems[i].reindex(entities); this.$$systems[i].reindex(entityIds);
} }
} }
remove(entity, components) { remove(entityId, components) {
this.removeMany([[entity, components]]); this.removeMany([[entityId, components]]);
} }
removeMany(entities) { removeMany(entities) {
const removing = {}; const removing = {};
const unique = new Set(); const unique = new Set();
for (const [entity, components] of entities) { for (const [entityId, components] of entities) {
unique.add(entity); unique.add(entityId);
const diff = {}; const diff = {};
for (const component of components) { for (const component of components) {
diff[component] = false; diff[component] = false;
if (!removing[component]) { if (!removing[component]) {
removing[component] = []; removing[component] = [];
} }
removing[component].push(entity); removing[component].push(entityId);
} }
this.markChange(entity, diff); this.markChange(entityId, diff);
this.rebuild(entity, (types) => types.filter((type) => !components.includes(type))); this.rebuild(entityId, (types) => types.filter((type) => !components.includes(type)));
} }
for (const component in removing) { for (const component in removing) {
this.Types[component].destroyMany(removing[component]); this.Types[component].destroyMany(removing[component]);
@ -336,10 +336,10 @@ export default class Ecs {
let entitiesWritten = 0; let entitiesWritten = 0;
cursor += 4; cursor += 4;
const keys = Object.keys(ecs.Types); const keys = Object.keys(ecs.Types);
for (const id of ecs.entities) { for (const entityId of ecs.entities) {
const entity = ecs.get(id); const entity = ecs.get(entityId);
entitiesWritten += 1; entitiesWritten += 1;
view.setUint32(cursor, id, true); view.setUint32(cursor, entityId, true);
cursor += 4; cursor += 4;
const entityComponents = entity.constructor.types; const entityComponents = entity.constructor.types;
view.setUint16(cursor, entityComponents.length, true); view.setUint16(cursor, entityComponents.length, true);
@ -349,8 +349,8 @@ export default class Ecs {
const instance = ecs.Types[component]; const instance = ecs.Types[component];
view.setUint16(cursor, keys.indexOf(component), true); view.setUint16(cursor, keys.indexOf(component), true);
cursor += 2; cursor += 2;
instance.serialize(id, view, cursor); instance.serialize(entityId, view, cursor);
cursor += instance.sizeOf(id); cursor += instance.sizeOf(entityId);
} }
view.setUint16(componentsWrittenIndex, entityComponents.length, true); view.setUint16(componentsWrittenIndex, entityComponents.length, true);
} }
@ -365,8 +365,8 @@ export default class Ecs {
size() { size() {
// # of entities. // # of entities.
let size = 4; let size = 4;
for (const entity of this.entities) { for (const entityId of this.entities) {
size += this.get(entity).size(); size += this.get(entityId).size();
} }
return size; return size;
} }

View File

@ -22,40 +22,40 @@ export default class Query {
return this.$$index.size; return this.$$index.size;
} }
deindex(ids) { deindex(entityIds) {
for (let i = 0; i < ids.length; ++i) { for (let i = 0; i < entityIds.length; ++i) {
this.$$index.delete(ids[i]); this.$$index.delete(entityIds[i]);
} }
} }
reindex(ids) { reindex(entityIds) {
if (0 === this.$$criteria.with.length && 0 === this.$$criteria.without.length) { if (0 === this.$$criteria.with.length && 0 === this.$$criteria.without.length) {
for (const id of ids) { for (const entityId of entityIds) {
this.$$index.add(id); this.$$index.add(entityId);
} }
return; return;
} }
for (const id of ids) { for (const entityId of entityIds) {
let should = true; let should = true;
for (let j = 0; j < this.$$criteria.with.length; ++j) { for (let j = 0; j < this.$$criteria.with.length; ++j) {
if ('undefined' === typeof this.$$criteria.with[j].get(id)) { if ('undefined' === typeof this.$$criteria.with[j].get(entityId)) {
should = false; should = false;
break; break;
} }
} }
if (should) { if (should) {
for (let j = 0; j < this.$$criteria.without.length; ++j) { for (let j = 0; j < this.$$criteria.without.length; ++j) {
if ('undefined' !== typeof this.$$criteria.without[j].get(id)) { if ('undefined' !== typeof this.$$criteria.without[j].get(entityId)) {
should = false; should = false;
break; break;
} }
} }
} }
if (should) { if (should) {
this.$$index.add(id); this.$$index.add(entityId);
} }
else if (!should) { else if (!should) {
this.$$index.delete(id); this.$$index.delete(entityId);
} }
} }
} }

View File

@ -17,19 +17,19 @@ export default class System {
} }
} }
deindex(entities) { deindex(entityIds) {
for (const i in this.queries) { for (const i in this.queries) {
this.queries[i].deindex(entities); this.queries[i].deindex(entityIds);
} }
} }
destroyEntity(entity) { destroyEntity(entityId) {
this.destroyManyEntities([entity]); this.destroyManyEntities([entityId]);
} }
destroyManyEntities(entities) { destroyManyEntities(entityIds) {
for (let i = 0; i < entities.length; i++) { for (let i = 0; i < entityIds.length; i++) {
this.destroying.push(entities[i]); this.destroying.push(entityIds[i]);
} }
} }
@ -52,9 +52,9 @@ export default class System {
return {}; return {};
} }
reindex(entities) { reindex(entityIds) {
for (const i in this.queries) { for (const i in this.queries) {
this.queries[i].reindex(entities); this.queries[i].reindex(entityIds);
} }
} }
@ -89,20 +89,20 @@ export default class System {
return source; return source;
} }
insertComponents(entity, components) { insertComponents(entityId, components) {
this.ecs.insert(entity, components); this.ecs.insert(entityId, components);
} }
insertManyComponents(components) { insertManyComponents(components) {
this.ecs.insertMany(components); this.ecs.insertMany(components);
} }
removeComponents(entity, components) { removeComponents(entityId, components) {
this.ecs.remove(entity, components); this.ecs.remove(entityId, components);
} }
removeManyComponents(entities) { removeManyComponents(entityIds) {
this.ecs.removeMany(entities); this.ecs.removeMany(entityIds);
} }
} }