refactor: Entity children

This commit is contained in:
cha0s 2021-02-10 13:12:30 -06:00
parent 4ef7aaf36a
commit 34a54cdc59
13 changed files with 65 additions and 53 deletions

View File

@ -66,7 +66,7 @@ export default (latus) => class Behaved extends decorate(Trait) {
};
}
static describe(entity) {
static children(entity) {
return {
isBehaving: {
type: 'bool',

View File

@ -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',
},
},
),
),
)
);
}
}

View File

@ -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({

View File

@ -93,7 +93,7 @@ export default (latus) => class Alive extends decorate(Trait) {
};
}
static describe() {
static children() {
return {
die: {
type: 'void',

View File

@ -31,7 +31,7 @@ export default () => class Directional extends decorate(Trait) {
};
}
static describe() {
static children() {
return {
changeToRandomDirection: {
type: 'void',

View File

@ -31,7 +31,7 @@ export default () => class Mobile extends decorate(Trait) {
};
}
static describe() {
static children() {
return {
moveFor: {
type: 'void',

View File

@ -36,7 +36,7 @@ export default () => class Positioned extends decorate(Trait) {
};
}
static describe() {
static children() {
return {
position: {
type: 'vector',

View File

@ -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: () => ({

View File

@ -68,7 +68,7 @@ export default (latus) => class Collider extends decorate(Trait) {
];
}
static describe() {
static children() {
return {
collidesWith: {
advanced: true,

View File

@ -56,7 +56,7 @@ export default (latus) => class Emitter extends decorate(Trait) {
};
}
static describe(entity) {
static children(entity) {
return {
emitParticleEntity: {
type: 'entity',

View File

@ -41,7 +41,7 @@ export default (latus) => class Audible extends Trait {
};
}
static describe(entity) {
static children(entity) {
return {
hasSound: {
args: [

View File

@ -58,7 +58,7 @@ export default (latus) => class Animated extends decorate(Trait) {
];
}
static describe() {
static children() {
return {
isAnimating: {
type: 'bool',

View File

@ -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() {}