chore: naming
This commit is contained in:
parent
7ccfe492cd
commit
fc281a90c7
|
@ -31,9 +31,9 @@ export default class Arbitrary extends Base {
|
|||
const allocated = this.allocateMany(entries.length);
|
||||
const keys = Object.keys(this.constructor.schema.specification);
|
||||
for (let i = 0; i < entries.length; ++i) {
|
||||
const [entity, values = {}] = entries[i];
|
||||
this.map[entity] = allocated[i];
|
||||
this.data[allocated[i]].entity = entity;
|
||||
const [entityId, values = {}] = entries[i];
|
||||
this.map[entityId] = allocated[i];
|
||||
this.data[allocated[i]].entity = entityId;
|
||||
if (false === values) {
|
||||
continue;
|
||||
}
|
||||
|
@ -51,16 +51,16 @@ export default class Arbitrary extends Base {
|
|||
}
|
||||
}
|
||||
|
||||
deserialize(entity, view, offset) {
|
||||
this.serializer.decode(view, this.get(entity), offset);
|
||||
deserialize(entityId, view, offset) {
|
||||
this.serializer.decode(view, this.get(entityId), offset);
|
||||
}
|
||||
|
||||
serialize(entity, view, offset) {
|
||||
this.serializer.encode(this.get(entity), view, offset);
|
||||
serialize(entityId, view, offset) {
|
||||
this.serializer.encode(this.get(entityId), view, offset);
|
||||
}
|
||||
|
||||
get(entity) {
|
||||
return this.data[this.map[entity]];
|
||||
get(entityId) {
|
||||
return this.data[this.map[entityId]];
|
||||
}
|
||||
|
||||
instanceFromSchema() {
|
||||
|
|
|
@ -14,22 +14,22 @@ export default class Base {
|
|||
return results;
|
||||
}
|
||||
|
||||
create(entity, values) {
|
||||
this.createMany([[entity, values]]);
|
||||
create(entityId, values) {
|
||||
this.createMany([[entityId, values]]);
|
||||
}
|
||||
|
||||
destroy(entity) {
|
||||
this.destroyMany([entity]);
|
||||
destroy(entityId) {
|
||||
this.destroyMany([entityId]);
|
||||
}
|
||||
|
||||
destroyMany(entities) {
|
||||
this.freeMany(
|
||||
entities
|
||||
.map((entity) => {
|
||||
if ('undefined' !== typeof this.map[entity]) {
|
||||
return this.map[entity];
|
||||
.map((entityId) => {
|
||||
if ('undefined' !== typeof this.map[entityId]) {
|
||||
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++) {
|
||||
|
@ -56,12 +56,12 @@ export default class Base {
|
|||
insertMany(entities) {
|
||||
const creating = [];
|
||||
for (let i = 0; i < entities.length; i++) {
|
||||
const [entity, values] = entities[i];
|
||||
if (!this.get(entity)) {
|
||||
creating.push([entity, values]);
|
||||
const [entityId, values] = entities[i];
|
||||
if (!this.get(entityId)) {
|
||||
creating.push([entityId, values]);
|
||||
}
|
||||
else {
|
||||
const instance = this.get(entity);
|
||||
const instance = this.get(entityId);
|
||||
for (const i in values) {
|
||||
instance[i] = values[i];
|
||||
}
|
||||
|
@ -71,20 +71,20 @@ export default class Base {
|
|||
}
|
||||
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
markChange(entity, components) {}
|
||||
markChange(entityId, components) {}
|
||||
|
||||
mergeDiff(original, update) {
|
||||
return {...original, ...update};
|
||||
}
|
||||
|
||||
sizeOf(entity) {
|
||||
return this.constructor.schema.sizeOf(this.get(entity));
|
||||
sizeOf(entityId) {
|
||||
return this.constructor.schema.sizeOf(this.get(entityId));
|
||||
}
|
||||
|
||||
static wrap(name, ecs) {
|
||||
class WrappedComponent extends this {
|
||||
markChange(entity, key, value) {
|
||||
ecs.markChange(entity, {[name]: {[key]: value}})
|
||||
markChange(entityId, key, value) {
|
||||
ecs.markChange(entityId, {[name]: {[key]: value}})
|
||||
}
|
||||
}
|
||||
return new WrappedComponent();
|
||||
|
|
170
app/ecs/ecs.js
170
app/ecs/ecs.js
|
@ -36,10 +36,10 @@ export default class Ecs {
|
|||
const destroying = [];
|
||||
const removing = [];
|
||||
const updating = [];
|
||||
for (const id in patch) {
|
||||
const components = patch[id];
|
||||
for (const entityId in patch) {
|
||||
const components = patch[entityId];
|
||||
if (false === components) {
|
||||
destroying.push(id);
|
||||
destroying.push(entityId);
|
||||
continue;
|
||||
}
|
||||
const componentsToRemove = [];
|
||||
|
@ -53,13 +53,13 @@ export default class Ecs {
|
|||
}
|
||||
}
|
||||
if (componentsToRemove.length > 0) {
|
||||
removing.push([id, componentsToRemove]);
|
||||
removing.push([entityId, componentsToRemove]);
|
||||
}
|
||||
if (this.$$entities[id]) {
|
||||
updating.push([id, componentsToUpdate]);
|
||||
if (this.$$entities[entityId]) {
|
||||
updating.push([entityId, componentsToUpdate]);
|
||||
}
|
||||
else {
|
||||
creating.push([id, componentsToUpdate]);
|
||||
creating.push([entityId, componentsToUpdate]);
|
||||
}
|
||||
}
|
||||
this.destroyMany(destroying);
|
||||
|
@ -69,8 +69,8 @@ export default class Ecs {
|
|||
}
|
||||
|
||||
create(components = {}) {
|
||||
const [entity] = this.createMany([components]);
|
||||
return entity;
|
||||
const [entityId] = this.createMany([components]);
|
||||
return entityId;
|
||||
}
|
||||
|
||||
createMany(componentsList) {
|
||||
|
@ -82,40 +82,40 @@ export default class Ecs {
|
|||
}
|
||||
|
||||
createManySpecific(specificsList) {
|
||||
const entities = [];
|
||||
const entityIds = [];
|
||||
const creating = {};
|
||||
for (let i = 0; i < specificsList.length; i++) {
|
||||
const [entity, components] = specificsList[i];
|
||||
const [entityId, components] = specificsList[i];
|
||||
const componentKeys = [];
|
||||
for (const key of Object.keys(components)) {
|
||||
if (this.Types[key]) {
|
||||
componentKeys.push(key);
|
||||
}
|
||||
}
|
||||
entities.push(entity);
|
||||
this.rebuild(entity, () => componentKeys);
|
||||
entityIds.push(entityId);
|
||||
this.rebuild(entityId, () => componentKeys);
|
||||
for (const component of componentKeys) {
|
||||
if (!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) {
|
||||
this.Types[i].createMany(creating[i]);
|
||||
}
|
||||
this.reindex(entities);
|
||||
return entities;
|
||||
this.reindex(entityIds);
|
||||
return entityIds;
|
||||
}
|
||||
|
||||
createSpecific(entity, components) {
|
||||
return this.createManySpecific([[entity, components]]);
|
||||
createSpecific(entityId, components) {
|
||||
return this.createManySpecific([[entityId, components]]);
|
||||
}
|
||||
|
||||
deindex(entities) {
|
||||
deindex(entityIds) {
|
||||
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 cursors = new Map();
|
||||
for (let i = 0; i < count; ++i) {
|
||||
const entity = view.getUint32(cursor, true);
|
||||
if (!ecs.$$entities[entity]) {
|
||||
creating.set(entity, {});
|
||||
const entityId = view.getUint32(cursor, true);
|
||||
if (!ecs.$$entities[entityId]) {
|
||||
creating.set(entityId, {});
|
||||
}
|
||||
cursor += 4;
|
||||
const componentCount = view.getUint16(cursor, true);
|
||||
cursor += 2;
|
||||
cursors.set(entity, {});
|
||||
cursors.set(entityId, {});
|
||||
const addedComponents = [];
|
||||
for (let j = 0; j < componentCount; ++j) {
|
||||
const id = view.getUint16(cursor, true);
|
||||
const componentId = view.getUint16(cursor, true);
|
||||
cursor += 2;
|
||||
const component = keys[id];
|
||||
const component = keys[componentId];
|
||||
if (!component) {
|
||||
throw new Error(`can't decode component ${id}`);
|
||||
throw new Error(`can't decode component ${componentId}`);
|
||||
}
|
||||
if (!ecs.$$entities[entity]) {
|
||||
creating.get(entity)[component] = false;
|
||||
if (!ecs.$$entities[entityId]) {
|
||||
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);
|
||||
if (!updating.has(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);
|
||||
}
|
||||
if (addedComponents.length > 0 && ecs.$$entities[entity]) {
|
||||
ecs.rebuild(entity, (types) => types.concat(addedComponents));
|
||||
if (addedComponents.length > 0 && ecs.$$entities[entityId]) {
|
||||
ecs.rebuild(entityId, (types) => types.concat(addedComponents));
|
||||
}
|
||||
}
|
||||
ecs.createManySpecific(Array.from(creating.entries()));
|
||||
for (const [component, entities] of updating) {
|
||||
ecs.Types[component].createMany(entities);
|
||||
for (const [component, entityIds] of updating) {
|
||||
ecs.Types[component].createMany(entityIds);
|
||||
}
|
||||
for (const [entity, components] of cursors) {
|
||||
for (const [entityId, components] of cursors) {
|
||||
for (const component in components) {
|
||||
ecs.Types[component].deserialize(entity, view, components[component]);
|
||||
ecs.Types[component].deserialize(entityId, view, components[component]);
|
||||
}
|
||||
}
|
||||
return ecs;
|
||||
}
|
||||
|
||||
destroy(entity) {
|
||||
this.destroyMany([entity]);
|
||||
destroy(entityId) {
|
||||
this.destroyMany([entityId]);
|
||||
}
|
||||
|
||||
destroyAll() {
|
||||
this.destroyMany(this.entities);
|
||||
}
|
||||
|
||||
destroyMany(entities) {
|
||||
destroyMany(entityIds) {
|
||||
const destroying = {};
|
||||
this.deindex(entities);
|
||||
for (const entity of entities) {
|
||||
if (!this.$$entities[entity]) {
|
||||
throw new Error(`can't destroy non-existent entity ${entity}`);
|
||||
this.deindex(entityIds);
|
||||
for (const entityId of entityIds) {
|
||||
if (!this.$$entities[entityId]) {
|
||||
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]) {
|
||||
destroying[component] = [];
|
||||
}
|
||||
destroying[component].push(entity);
|
||||
destroying[component].push(entityId);
|
||||
}
|
||||
this.$$entities[entity] = undefined;
|
||||
this.diff[entity] = false;
|
||||
this.$$entities[entityId] = undefined;
|
||||
this.diff[entityId] = false;
|
||||
}
|
||||
for (const i in destroying) {
|
||||
this.Types[i].destroyMany(destroying[i]);
|
||||
|
@ -222,29 +222,29 @@ export default class Ecs {
|
|||
};
|
||||
}
|
||||
|
||||
get(entity) {
|
||||
return this.$$entities[entity];
|
||||
get(entityId) {
|
||||
return this.$$entities[entityId];
|
||||
}
|
||||
|
||||
insert(entity, components) {
|
||||
this.insertMany([[entity, components]]);
|
||||
insert(entityId, components) {
|
||||
this.insertMany([[entityId, components]]);
|
||||
}
|
||||
|
||||
insertMany(entities) {
|
||||
const inserting = {};
|
||||
const unique = new Set();
|
||||
for (const [entity, components] of entities) {
|
||||
this.rebuild(entity, (types) => [...new Set(types.concat(Object.keys(components)))]);
|
||||
for (const [entityId, components] of entities) {
|
||||
this.rebuild(entityId, (types) => [...new Set(types.concat(Object.keys(components)))]);
|
||||
const diff = {};
|
||||
for (const component in components) {
|
||||
if (!inserting[component]) {
|
||||
inserting[component] = [];
|
||||
}
|
||||
diff[component] = {};
|
||||
inserting[component].push([entity, components[component]]);
|
||||
inserting[component].push([entityId, components[component]]);
|
||||
}
|
||||
unique.add(entity);
|
||||
this.markChange(entity, diff);
|
||||
unique.add(entityId);
|
||||
this.markChange(entityId, diff);
|
||||
}
|
||||
for (const component in inserting) {
|
||||
this.Types[component].insertMany(inserting[component]);
|
||||
|
@ -252,68 +252,68 @@ export default class Ecs {
|
|||
this.reindex(unique.values());
|
||||
}
|
||||
|
||||
markChange(entity, components) {
|
||||
markChange(entityId, components) {
|
||||
// Deleted?
|
||||
if (false === components) {
|
||||
this.diff[entity] = false;
|
||||
this.diff[entityId] = false;
|
||||
}
|
||||
// Created?
|
||||
else if (!this.diff[entity]) {
|
||||
else if (!this.diff[entityId]) {
|
||||
const filtered = {};
|
||||
for (const type in components) {
|
||||
filtered[type] = false === components[type]
|
||||
? false
|
||||
: this.Types[type].constructor.filterDefaults(components[type]);
|
||||
}
|
||||
this.diff[entity] = filtered;
|
||||
this.diff[entityId] = filtered;
|
||||
}
|
||||
// Otherwise, merge.
|
||||
else {
|
||||
for (const type in components) {
|
||||
this.diff[entity][type] = false === components[type]
|
||||
this.diff[entityId][type] = false === components[type]
|
||||
? false
|
||||
: this.Types[type].mergeDiff(
|
||||
this.diff[entity][type] || {},
|
||||
this.diff[entityId][type] || {},
|
||||
components[type],
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rebuild(entity, types) {
|
||||
rebuild(entityId, types) {
|
||||
let existing = [];
|
||||
if (this.$$entities[entity]) {
|
||||
existing.push(...this.$$entities[entity].constructor.types);
|
||||
if (this.$$entities[entityId]) {
|
||||
existing.push(...this.$$entities[entityId].constructor.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++) {
|
||||
this.$$systems[i].reindex(entities);
|
||||
this.$$systems[i].reindex(entityIds);
|
||||
}
|
||||
}
|
||||
|
||||
remove(entity, components) {
|
||||
this.removeMany([[entity, components]]);
|
||||
remove(entityId, components) {
|
||||
this.removeMany([[entityId, components]]);
|
||||
}
|
||||
|
||||
removeMany(entities) {
|
||||
const removing = {};
|
||||
const unique = new Set();
|
||||
for (const [entity, components] of entities) {
|
||||
unique.add(entity);
|
||||
for (const [entityId, components] of entities) {
|
||||
unique.add(entityId);
|
||||
const diff = {};
|
||||
for (const component of components) {
|
||||
diff[component] = false;
|
||||
if (!removing[component]) {
|
||||
removing[component] = [];
|
||||
}
|
||||
removing[component].push(entity);
|
||||
removing[component].push(entityId);
|
||||
}
|
||||
this.markChange(entity, diff);
|
||||
this.rebuild(entity, (types) => types.filter((type) => !components.includes(type)));
|
||||
this.markChange(entityId, diff);
|
||||
this.rebuild(entityId, (types) => types.filter((type) => !components.includes(type)));
|
||||
}
|
||||
for (const component in removing) {
|
||||
this.Types[component].destroyMany(removing[component]);
|
||||
|
@ -336,10 +336,10 @@ export default class Ecs {
|
|||
let entitiesWritten = 0;
|
||||
cursor += 4;
|
||||
const keys = Object.keys(ecs.Types);
|
||||
for (const id of ecs.entities) {
|
||||
const entity = ecs.get(id);
|
||||
for (const entityId of ecs.entities) {
|
||||
const entity = ecs.get(entityId);
|
||||
entitiesWritten += 1;
|
||||
view.setUint32(cursor, id, true);
|
||||
view.setUint32(cursor, entityId, true);
|
||||
cursor += 4;
|
||||
const entityComponents = entity.constructor.types;
|
||||
view.setUint16(cursor, entityComponents.length, true);
|
||||
|
@ -349,8 +349,8 @@ export default class Ecs {
|
|||
const instance = ecs.Types[component];
|
||||
view.setUint16(cursor, keys.indexOf(component), true);
|
||||
cursor += 2;
|
||||
instance.serialize(id, view, cursor);
|
||||
cursor += instance.sizeOf(id);
|
||||
instance.serialize(entityId, view, cursor);
|
||||
cursor += instance.sizeOf(entityId);
|
||||
}
|
||||
view.setUint16(componentsWrittenIndex, entityComponents.length, true);
|
||||
}
|
||||
|
@ -365,8 +365,8 @@ export default class Ecs {
|
|||
size() {
|
||||
// # of entities.
|
||||
let size = 4;
|
||||
for (const entity of this.entities) {
|
||||
size += this.get(entity).size();
|
||||
for (const entityId of this.entities) {
|
||||
size += this.get(entityId).size();
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
|
|
@ -22,40 +22,40 @@ export default class Query {
|
|||
return this.$$index.size;
|
||||
}
|
||||
|
||||
deindex(ids) {
|
||||
for (let i = 0; i < ids.length; ++i) {
|
||||
this.$$index.delete(ids[i]);
|
||||
deindex(entityIds) {
|
||||
for (let i = 0; i < entityIds.length; ++i) {
|
||||
this.$$index.delete(entityIds[i]);
|
||||
}
|
||||
}
|
||||
|
||||
reindex(ids) {
|
||||
reindex(entityIds) {
|
||||
if (0 === this.$$criteria.with.length && 0 === this.$$criteria.without.length) {
|
||||
for (const id of ids) {
|
||||
this.$$index.add(id);
|
||||
for (const entityId of entityIds) {
|
||||
this.$$index.add(entityId);
|
||||
}
|
||||
return;
|
||||
}
|
||||
for (const id of ids) {
|
||||
for (const entityId of entityIds) {
|
||||
let should = true;
|
||||
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;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (should) {
|
||||
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;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (should) {
|
||||
this.$$index.add(id);
|
||||
this.$$index.add(entityId);
|
||||
}
|
||||
else if (!should) {
|
||||
this.$$index.delete(id);
|
||||
this.$$index.delete(entityId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,19 +17,19 @@ export default class System {
|
|||
}
|
||||
}
|
||||
|
||||
deindex(entities) {
|
||||
deindex(entityIds) {
|
||||
for (const i in this.queries) {
|
||||
this.queries[i].deindex(entities);
|
||||
this.queries[i].deindex(entityIds);
|
||||
}
|
||||
}
|
||||
|
||||
destroyEntity(entity) {
|
||||
this.destroyManyEntities([entity]);
|
||||
destroyEntity(entityId) {
|
||||
this.destroyManyEntities([entityId]);
|
||||
}
|
||||
|
||||
destroyManyEntities(entities) {
|
||||
for (let i = 0; i < entities.length; i++) {
|
||||
this.destroying.push(entities[i]);
|
||||
destroyManyEntities(entityIds) {
|
||||
for (let i = 0; i < entityIds.length; i++) {
|
||||
this.destroying.push(entityIds[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -52,9 +52,9 @@ export default class System {
|
|||
return {};
|
||||
}
|
||||
|
||||
reindex(entities) {
|
||||
reindex(entityIds) {
|
||||
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;
|
||||
}
|
||||
|
||||
insertComponents(entity, components) {
|
||||
this.ecs.insert(entity, components);
|
||||
insertComponents(entityId, components) {
|
||||
this.ecs.insert(entityId, components);
|
||||
}
|
||||
|
||||
insertManyComponents(components) {
|
||||
this.ecs.insertMany(components);
|
||||
}
|
||||
|
||||
removeComponents(entity, components) {
|
||||
this.ecs.remove(entity, components);
|
||||
removeComponents(entityId, components) {
|
||||
this.ecs.remove(entityId, components);
|
||||
}
|
||||
|
||||
removeManyComponents(entities) {
|
||||
this.ecs.removeMany(entities);
|
||||
removeManyComponents(entityIds) {
|
||||
this.ecs.removeMany(entityIds);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user