refactor: initialization

This commit is contained in:
cha0s 2024-08-01 21:10:16 -05:00
parent c4f2c4e7d4
commit bf299e718e
2 changed files with 13 additions and 25 deletions

View File

@ -40,23 +40,21 @@ export default class Component {
const allocated = this.allocateMany(entries.length);
const {properties} = this.constructor.schema.specification.concrete;
const Schema = this.constructor.schema.constructor;
const keys = Object.keys(properties);
const keys = new Set(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];
}
else {
const defaultValue = Schema.defaultValue(properties[j]);
if ('undefined' !== typeof defaultValue) {
instance[j] = defaultValue;
}
const instance = this.data[allocated[i]];
for (const key in values) {
keys.delete(key);
instance[key] = values[key];
}
for (const key of keys) {
const defaultValue = Schema.defaultValue(properties[key]);
if ('undefined' !== typeof defaultValue) {
instance[key] = defaultValue;
}
}
promises.push(this.load(this.data[allocated[i]]));
@ -127,17 +125,8 @@ export default class Component {
instanceFromSchema() {
const Component = this;
const {concrete} = Component.constructor.schema.specification;
const Schema = Component.constructor.schema.constructor;
const Instance = class {
$$entity = 0;
constructor() {
this.$$reset();
}
$$reset() {
for (const key in concrete.properties) {
this[`$$${key}`] = Schema.defaultValue(concrete.properties[key]);
}
}
destroy() {}
toNet(recipient, data) {
return data || Component.constructor.filterDefaults(this);
@ -163,7 +152,6 @@ export default class Component {
},
set: function set(v) {
this.$$entity = v;
this.$$reset();
},
};
for (const key in concrete.properties) {

View File

@ -5,15 +5,15 @@ export default class Ttl extends Component {
const {ecs} = this;
return class TtlInstance extends super.instanceFromSchema() {
$$elapsed = 0;
destroy() {
this.$$elapsed = 0;
}
get elapsed() {
return this.$$elapsed;
}
set elapsed(elapsed) {
this.$$elapsed = elapsed;
}
$$reset() {
this.$$elapsed = 0;
}
tick(elapsed) {
this.$$elapsed += elapsed;
if (this.$$elapsed >= this.ttl) {