silphius/app/ecs/component.js

220 lines
5.4 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-07-07 17:26:17 -05:00
const [created] = await this.createMany([[entityId, values]]);
return created;
2024-06-26 10:33:31 -05:00
}
2024-06-27 06:28:00 -05:00
async createMany(entries) {
2024-07-07 17:26:17 -05:00
if (0 === entries.length) {
return [];
}
const allocated = this.allocateMany(entries.length);
2024-07-23 10:10:32 -05:00
const {properties} = this.constructor.schema.specification.concrete;
const Schema = this.constructor.schema.constructor;
2024-07-07 17:26:17 -05:00
const keys = Object.keys(properties);
const promises = [];
for (let i = 0; i < entries.length; ++i) {
const [entityId, values = {}] = entries[i];
this.map[entityId] = allocated[i];
this.data[allocated[i]].entity = entityId;
for (let k = 0; k < keys.length; ++k) {
const j = keys[k];
const instance = this.data[allocated[i]];
if (j in values) {
instance[j] = values[j];
}
2024-07-23 10:10:32 -05:00
else {
const defaultValue = Schema.defaultValue(properties[j]);
if ('undefined' !== typeof defaultValue) {
instance[j] = defaultValue;
}
2024-06-10 22:42:30 -05:00
}
}
2024-07-07 17:26:17 -05:00
promises.push(this.load(this.data[allocated[i]]));
}
await Promise.all(promises);
const created = [];
for (let i = 0; i < allocated.length; ++i) {
created.push(this.data[allocated[i]]);
2024-06-10 22:42:30 -05:00
}
2024-07-07 17:26:17 -05:00
return created;
2024-06-10 22:42:30 -05:00
}
2024-06-11 19:10:57 -05:00
deserialize(entityId, view, offset) {
2024-07-23 10:10:32 -05:00
const {properties} = this.constructor.schema.specification.concrete;
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++) {
2024-07-04 09:06:50 -05:00
this.data[this.map[entities[i]]].destroy();
2024-06-26 10:33:31 -05:00
this.map[entities[i]] = undefined;
}
}
static filterDefaults(instance) {
2024-07-23 10:10:32 -05:00
const {properties} = this.schema.specification.concrete;
const Schema = this.schema.constructor;
2024-06-26 10:33:31 -05:00
const json = {};
2024-06-26 21:08:09 -05:00
for (const key in properties) {
2024-07-23 10:10:32 -05:00
if (key in instance && instance[key] !== Schema.defaultValue(properties[key])) {
2024-06-26 10:33:31 -05:00
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-07-23 10:10:32 -05:00
const {concrete} = Component.constructor.schema.specification;
const Schema = Component.constructor.schema.constructor;
2024-06-10 22:42:30 -05:00
const Instance = class {
$$entity = 0;
constructor() {
this.$$reset();
}
$$reset() {
2024-07-23 10:10:32 -05:00
for (const key in concrete.properties) {
this[`$$${key}`] = Schema.defaultValue(concrete.properties[key]);
2024-06-10 22:42:30 -05:00
}
}
2024-07-04 09:06:50 -05:00
destroy() {}
2024-07-13 00:33:41 -05:00
toNet() {
2024-06-10 22:42:30 -05:00
return Component.constructor.filterDefaults(this);
}
2024-07-13 00:33:41 -05:00
toJSON() {
2024-07-21 04:00:55 -05:00
return Component.constructor.filterDefaults(this);
2024-07-13 00:33:41 -05:00
}
2024-06-10 22:42:30 -05:00
};
const properties = {};
properties.entity = {
get: function get() {
return this.$$entity;
},
set: function set(v) {
this.$$entity = v;
this.$$reset();
},
};
2024-07-23 10:10:32 -05:00
for (const key in concrete.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
}