silphius/app/ecs/component.js

204 lines
4.9 KiB
JavaScript
Raw Normal View History

2024-06-26 10:33:31 -05:00
import Schema from './schema.js';
2024-06-10 22:42:30 -05:00
2024-06-26 10:33:31 -05:00
export default class Component {
2024-06-10 22:42:30 -05:00
data = [];
2024-06-26 10:33:31 -05:00
ecs;
Instance;
map = {};
pool = [];
2024-06-26 21:08:09 -05:00
static properties = {};
static $$schema;
2024-06-26 10:33:31 -05:00
constructor(ecs) {
this.ecs = ecs;
2024-06-27 02:05:10 -05:00
this.Instance = this.instanceFromSchema();
2024-06-26 10:33:31 -05:00
}
2024-06-10 22:42:30 -05:00
allocateMany(count) {
2024-06-26 10:33:31 -05:00
const results = [];
while (count > 0) {
results.push(
this.pool.length > 0
? this.pool.pop()
: this.data.push(new this.Instance()) - 1,
)
count -= 1;
2024-06-10 22:42:30 -05:00
}
return results;
}
2024-06-27 06:28:00 -05:00
async create(entityId, values) {
2024-06-26 10:33:31 -05:00
this.createMany([[entityId, values]]);
}
2024-06-27 06:28:00 -05:00
async createMany(entries) {
2024-06-10 22:42:30 -05:00
if (entries.length > 0) {
const allocated = this.allocateMany(entries.length);
2024-06-26 21:08:09 -05:00
const {properties} = this.constructor.schema.specification;
const keys = Object.keys(properties);
2024-06-27 06:28:00 -05:00
const promises = [];
2024-06-10 22:42:30 -05:00
for (let i = 0; i < entries.length; ++i) {
2024-06-11 19:10:57 -05:00
const [entityId, values = {}] = entries[i];
this.map[entityId] = allocated[i];
this.data[allocated[i]].entity = entityId;
2024-06-10 22:42:30 -05:00
for (let k = 0; k < keys.length; ++k) {
const j = keys[k];
2024-06-26 21:08:09 -05:00
const {defaultValue} = properties[j];
2024-06-10 22:42:30 -05:00
if (j in values) {
this.data[allocated[i]][j] = values[j];
}
else if ('undefined' !== typeof defaultValue) {
this.data[allocated[i]][j] = defaultValue;
}
}
2024-06-27 06:28:00 -05:00
promises.push(this.load(this.data[allocated[i]]));
2024-06-10 22:42:30 -05:00
}
2024-06-27 06:28:00 -05:00
await Promise.all(promises);
2024-06-10 22:42:30 -05:00
}
}
2024-06-11 19:10:57 -05:00
deserialize(entityId, view, offset) {
2024-06-26 21:08:09 -05:00
const {properties} = this.constructor.schema.specification;
2024-06-12 01:38:05 -05:00
const instance = this.get(entityId);
const deserialized = this.constructor.schema.deserialize(view, offset);
for (const key in properties) {
instance[key] = deserialized[key];
}
2024-06-10 22:42:30 -05:00
}
2024-06-26 10:33:31 -05:00
destroy(entityId) {
this.destroyMany([entityId]);
}
destroyMany(entities) {
this.freeMany(
entities
.map((entityId) => {
if ('undefined' !== typeof this.map[entityId]) {
return this.map[entityId];
}
throw new Error(`can't free for non-existent id ${entityId}`);
}),
);
for (let i = 0; i < entities.length; i++) {
this.map[entities[i]] = undefined;
}
}
static filterDefaults(instance) {
2024-06-26 21:08:09 -05:00
const {properties} = this.schema.specification;
2024-06-26 10:33:31 -05:00
const json = {};
2024-06-26 21:08:09 -05:00
for (const key in properties) {
const {defaultValue} = properties[key];
2024-06-26 10:33:31 -05:00
if (key in instance && instance[key] !== defaultValue) {
json[key] = instance[key];
}
}
return json;
}
freeMany(indices) {
for (let i = 0; i < indices.length; ++i) {
this.pool.push(indices[i]);
}
2024-06-10 22:42:30 -05:00
}
2024-06-11 19:10:57 -05:00
get(entityId) {
return this.data[this.map[entityId]];
2024-06-10 22:42:30 -05:00
}
2024-06-27 06:28:00 -05:00
async insertMany(entities) {
2024-06-26 10:33:31 -05:00
const creating = [];
for (let i = 0; i < entities.length; i++) {
const [entityId, values] = entities[i];
if (!this.get(entityId)) {
creating.push([entityId, values]);
}
else {
const instance = this.get(entityId);
for (const i in values) {
instance[i] = values[i];
}
}
}
2024-06-27 06:28:00 -05:00
await this.createMany(creating);
2024-06-26 10:33:31 -05:00
}
2024-06-10 22:42:30 -05:00
instanceFromSchema() {
const Component = this;
2024-06-26 21:08:09 -05:00
const {specification} = Component.constructor.schema;
2024-06-10 22:42:30 -05:00
const Instance = class {
$$entity = 0;
constructor() {
this.$$reset();
}
$$reset() {
2024-06-26 21:08:09 -05:00
for (const key in specification.properties) {
const {defaultValue} = specification.properties[key];
2024-06-12 01:38:05 -05:00
this[`$$${key}`] = defaultValue;
2024-06-10 22:42:30 -05:00
}
}
toJSON() {
return Component.constructor.filterDefaults(this);
}
};
const properties = {};
properties.entity = {
get: function get() {
return this.$$entity;
},
set: function set(v) {
this.$$entity = v;
this.$$reset();
},
};
2024-06-26 21:08:09 -05:00
for (const key in specification.properties) {
2024-06-12 01:38:05 -05:00
properties[key] = {
2024-06-10 22:42:30 -05:00
get: function get() {
2024-06-12 01:38:05 -05:00
return this[`$$${key}`];
2024-06-10 22:42:30 -05:00
},
2024-06-12 01:38:05 -05:00
set: function set(value) {
if (this[`$$${key}`] !== value) {
this[`$$${key}`] = value;
Component.markChange(this.entity, key, value);
2024-06-10 22:42:30 -05:00
}
},
};
}
Object.defineProperties(Instance.prototype, properties);
return Instance;
}
2024-06-27 06:28:00 -05:00
async load(instance) {
return instance;
}
2024-06-26 10:33:31 -05:00
markChange(entityId, key, value) {
this.ecs.markChange(entityId, {[this.constructor.componentName]: {[key]: value}})
}
mergeDiff(original, update) {
return {...original, ...update};
}
2024-06-26 21:08:09 -05:00
static get schema() {
if (!this.$$schema) {
this.$$schema = new Schema({
type: 'object',
properties: this.properties,
});
}
return this.$$schema;
2024-06-26 10:33:31 -05:00
}
serialize(entityId, view, offset) {
this.constructor.schema.serialize(this.get(entityId), view, offset);
}
sizeOf(entityId) {
return this.constructor.schema.sizeOf(this.get(entityId));
}
2024-06-10 22:42:30 -05:00
}