refactor: instance schema

This commit is contained in:
cha0s 2024-11-14 22:53:10 -06:00
parent 9dec5d15a3
commit e3d0cc2477
3 changed files with 16 additions and 26 deletions

View File

@ -40,7 +40,7 @@ export default class Component {
const {
constructor: Schema,
specification: {concrete: {properties}},
} = this.constructor.schema;
} = this.schema;
const defaults = {};
for (const key in properties) {
defaults[key] = ((key) => () => Schema.defaultValue(properties[key]))(key);
@ -83,9 +83,9 @@ export default class Component {
}
deserialize(entityId, view, offset) {
const {properties} = this.constructor.schema.specification.concrete;
const {properties} = this.schema.specification.concrete;
const instance = this.get(entityId);
const deserialized = this.constructor.schema.deserialize(view, offset);
const deserialized = this.schema.deserialize(view, offset);
for (const key in properties) {
instance[key] = deserialized[key];
}
@ -107,18 +107,6 @@ export default class Component {
}
}
static filterDefaults(instance) {
const {properties} = this.schema.specification.concrete;
const Schema = this.schema.constructor;
const json = {};
for (const key in properties) {
if (key in instance && instance[key] !== Schema.defaultValue(properties[key])) {
json[key] = instance[key];
}
}
return json;
}
get(entityId) {
return this.instances[entityId];
}
@ -129,7 +117,7 @@ export default class Component {
instanceFromSchema() {
const Component = this;
const {concrete} = Component.constructor.schema.specification;
const {concrete} = Component.schema.specification;
const Instance = class {
$$entity = 0;
destroy() {}
@ -235,22 +223,24 @@ export default class Component {
return {...original, ...update};
}
static get schema() {
get schema() {
if (!this.$$schema) {
this.$$schema = new Schema({
type: 'object',
properties: this.properties,
});
this.$$schema = new Schema(
{
type: 'object',
properties: this.constructor.properties,
},
);
}
return this.$$schema;
}
serialize(entityId, view, offset) {
this.constructor.schema.serialize(this.get(entityId), view, offset);
this.schema.serialize(this.get(entityId), view, offset);
}
sizeOf(entityId) {
return this.constructor.schema.sizeOf(this.get(entityId));
return this.schema.sizeOf(this.get(entityId));
}
updateMany(entities) {

View File

@ -262,8 +262,8 @@ export default class Collider extends Component {
load(instance) {
for (const i in instance.bodies) {
instance.bodies[i] = {
...this.constructor.schema.constructor.defaultValue(
this.constructor.schema.specification.concrete.properties.bodies.concrete.subtype,
...this.schema.constructor.defaultValue(
this.schema.specification.concrete.properties.bodies.concrete.subtype,
),
...instance.bodies[i],
};

View File

@ -31,7 +31,7 @@ export default class EntityFactory {
let size = 0;
for (const componentName of this.constructor.componentNames) {
const instance = Components[componentName];
size += 2 + 4 + instance.constructor.schema.sizeOf(instance.get(this.id));
size += 2 + 4 + instance.schema.sizeOf(instance.get(this.id));
}
// ID + # of components.
return size + 4 + 2;