From bf299e718e3f24dc00ab41ffb46fb13ec07b092e Mon Sep 17 00:00:00 2001 From: cha0s Date: Thu, 1 Aug 2024 21:10:16 -0500 Subject: [PATCH] refactor: initialization --- app/ecs/component.js | 32 ++++++++++---------------------- app/ecs/components/ttl.js | 6 +++--- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/app/ecs/component.js b/app/ecs/component.js index 7a47715..227d625 100644 --- a/app/ecs/component.js +++ b/app/ecs/component.js @@ -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) { diff --git a/app/ecs/components/ttl.js b/app/ecs/components/ttl.js index 1e03c20..c5b5e4c 100644 --- a/app/ecs/components/ttl.js +++ b/app/ecs/components/ttl.js @@ -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) {