diff --git a/packages/behavior/src/traits/behaved.js b/packages/behavior/src/traits/behaved.js index 63cd260..6ac691f 100644 --- a/packages/behavior/src/traits/behaved.js +++ b/packages/behavior/src/traits/behaved.js @@ -66,7 +66,7 @@ export default (latus) => class Behaved extends decorate(Trait) { }; } - static describe(entity) { + static children(entity) { return { isBehaving: { type: 'bool', diff --git a/packages/entity/src/base-entity.js b/packages/entity/src/base-entity.js new file mode 100644 index 0000000..1808986 --- /dev/null +++ b/packages/entity/src/base-entity.js @@ -0,0 +1,45 @@ +import {JsonResource} from '@avocado/resource'; + +export default class BaseEntity extends JsonResource { + + static children() { + return (entity) => ( + Object.fromEntries( + Object.entries( + Object.values(entity.traits) + .reduce( + (r, {constructor}) => ({ + ...r, + ...constructor.children(entity), + }), + { + addTrait: { + label: 'Add trait', + type: 'void', + args: [], + }, + destroy: { + label: 'Destroy', + type: 'void', + args: [], + }, + is: { + type: 'bool', + args: [ + { + type: 'string', + label: 'type', + }, + ], + }, + list: { + type: 'entity-list', + }, + }, + ), + ), + ) + ); + } + +} diff --git a/packages/entity/src/resources/entity.js b/packages/entity/src/resources/entity.js index 8e0d0cb..9eb921d 100644 --- a/packages/entity/src/resources/entity.js +++ b/packages/entity/src/resources/entity.js @@ -7,10 +7,10 @@ import { TickingPromise, } from '@avocado/core'; import {Synchronized} from '@avocado/s13n'; -import {JsonResource} from '@avocado/resource'; import {compose, EventEmitter} from '@latus/core'; import {defineTraitAccessors, enumerateTraitAccessorKeys} from '../accessors'; +import BaseEntity from '../base-entity'; const debug = D('@avocado/entity'); @@ -21,7 +21,7 @@ const decorate = compose( let numericUid = 'client' !== process.env.SIDE ? 1 : 1000000000; -export default (latus) => class Entity extends decorate(JsonResource) { +export default (latus) => class Entity extends decorate(BaseEntity) { #hooks = {}; @@ -105,6 +105,7 @@ export default (latus) => class Entity extends decorate(JsonResource) { console.error( `Tried to add trait "${type}" but lack one or more dependents: "${lacking.join('", "')}"!`, ); + return undefined; } // Instantiate. const trait = await Trait.load({ diff --git a/packages/entity/src/traits/alive.js b/packages/entity/src/traits/alive.js index a0821b5..9f06269 100644 --- a/packages/entity/src/traits/alive.js +++ b/packages/entity/src/traits/alive.js @@ -93,7 +93,7 @@ export default (latus) => class Alive extends decorate(Trait) { }; } - static describe() { + static children() { return { die: { type: 'void', diff --git a/packages/entity/src/traits/directional.js b/packages/entity/src/traits/directional.js index 018f2ec..995aee3 100644 --- a/packages/entity/src/traits/directional.js +++ b/packages/entity/src/traits/directional.js @@ -31,7 +31,7 @@ export default () => class Directional extends decorate(Trait) { }; } - static describe() { + static children() { return { changeToRandomDirection: { type: 'void', diff --git a/packages/entity/src/traits/mobile.js b/packages/entity/src/traits/mobile.js index 6fcf154..43321ea 100644 --- a/packages/entity/src/traits/mobile.js +++ b/packages/entity/src/traits/mobile.js @@ -31,7 +31,7 @@ export default () => class Mobile extends decorate(Trait) { }; } - static describe() { + static children() { return { moveFor: { type: 'void', diff --git a/packages/entity/src/traits/positioned.js b/packages/entity/src/traits/positioned.js index 563f1fb..18c0057 100644 --- a/packages/entity/src/traits/positioned.js +++ b/packages/entity/src/traits/positioned.js @@ -36,7 +36,7 @@ export default () => class Positioned extends decorate(Trait) { }; } - static describe() { + static children() { return { position: { type: 'vector', diff --git a/packages/entity/src/types.js b/packages/entity/src/types.js index e2ac798..516217c 100644 --- a/packages/entity/src/types.js +++ b/packages/entity/src/types.js @@ -1,44 +1,10 @@ +import BaseEntity from './base-entity'; + export default () => ({ entity: { - children: (entity) => ( - Object.fromEntries( - Object.entries( - Object.values(entity.traits) - .reduce( - (r, trait) => ({ - ...r, - ...trait.constructor.describe(entity), - }), - { - addTrait: { - label: 'Add trait', - type: 'void', - args: [], - }, - destroy: { - label: 'Destroy', - type: 'void', - args: [], - }, - is: { - type: 'bool', - args: [ - { - type: 'string', - label: 'type', - }, - ], - }, - list: { - type: 'entity-list', - }, - }, - ), - ), - ) - ), + children: BaseEntity.children, create: () => null, - infer: (v) => !!v?.traits, + infer: (v) => BaseEntity.children === v?.constructor?.children, }, 'entity-list': { children: () => ({ diff --git a/packages/physics/src/traits/collider.js b/packages/physics/src/traits/collider.js index e973418..2a83630 100644 --- a/packages/physics/src/traits/collider.js +++ b/packages/physics/src/traits/collider.js @@ -68,7 +68,7 @@ export default (latus) => class Collider extends decorate(Trait) { ]; } - static describe() { + static children() { return { collidesWith: { advanced: true, diff --git a/packages/physics/src/traits/emitter.js b/packages/physics/src/traits/emitter.js index ec86d81..68e3eb6 100644 --- a/packages/physics/src/traits/emitter.js +++ b/packages/physics/src/traits/emitter.js @@ -56,7 +56,7 @@ export default (latus) => class Emitter extends decorate(Trait) { }; } - static describe(entity) { + static children(entity) { return { emitParticleEntity: { type: 'entity', diff --git a/packages/sound/src/traits/audible.js b/packages/sound/src/traits/audible.js index 6b881b1..188fad5 100644 --- a/packages/sound/src/traits/audible.js +++ b/packages/sound/src/traits/audible.js @@ -41,7 +41,7 @@ export default (latus) => class Audible extends Trait { }; } - static describe(entity) { + static children(entity) { return { hasSound: { args: [ diff --git a/packages/timing/src/traits/animated.js b/packages/timing/src/traits/animated.js index 744e869..a89e2e8 100644 --- a/packages/timing/src/traits/animated.js +++ b/packages/timing/src/traits/animated.js @@ -58,7 +58,7 @@ export default (latus) => class Animated extends decorate(Trait) { ]; } - static describe() { + static children() { return { isAnimating: { type: 'bool', diff --git a/packages/traits/src/trait.js b/packages/traits/src/trait.js index 656b3bd..2047c8a 100644 --- a/packages/traits/src/trait.js +++ b/packages/traits/src/trait.js @@ -31,6 +31,10 @@ export default class Trait extends decorate(JsonResource) { // eslint-disable-next-line class-methods-use-this, no-unused-vars acceptPacket(packet) {} + static children() { + return {}; + } + cleanPackets() { if (!this.#markedAsDirty) { return; @@ -65,10 +69,6 @@ export default class Trait extends decorate(JsonResource) { return []; } - static describe() { - return {}; - } - // eslint-disable-next-line class-methods-use-this destroy() {}