refactor: Entity children
This commit is contained in:
parent
4ef7aaf36a
commit
34a54cdc59
|
@ -66,7 +66,7 @@ export default (latus) => class Behaved extends decorate(Trait) {
|
|||
};
|
||||
}
|
||||
|
||||
static describe(entity) {
|
||||
static children(entity) {
|
||||
return {
|
||||
isBehaving: {
|
||||
type: 'bool',
|
||||
|
|
45
packages/entity/src/base-entity.js
Normal file
45
packages/entity/src/base-entity.js
Normal 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',
|
||||
},
|
||||
},
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -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({
|
||||
|
|
|
@ -93,7 +93,7 @@ export default (latus) => class Alive extends decorate(Trait) {
|
|||
};
|
||||
}
|
||||
|
||||
static describe() {
|
||||
static children() {
|
||||
return {
|
||||
die: {
|
||||
type: 'void',
|
||||
|
|
|
@ -31,7 +31,7 @@ export default () => class Directional extends decorate(Trait) {
|
|||
};
|
||||
}
|
||||
|
||||
static describe() {
|
||||
static children() {
|
||||
return {
|
||||
changeToRandomDirection: {
|
||||
type: 'void',
|
||||
|
|
|
@ -31,7 +31,7 @@ export default () => class Mobile extends decorate(Trait) {
|
|||
};
|
||||
}
|
||||
|
||||
static describe() {
|
||||
static children() {
|
||||
return {
|
||||
moveFor: {
|
||||
type: 'void',
|
||||
|
|
|
@ -36,7 +36,7 @@ export default () => class Positioned extends decorate(Trait) {
|
|||
};
|
||||
}
|
||||
|
||||
static describe() {
|
||||
static children() {
|
||||
return {
|
||||
position: {
|
||||
type: 'vector',
|
||||
|
|
|
@ -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: () => ({
|
||||
|
|
|
@ -68,7 +68,7 @@ export default (latus) => class Collider extends decorate(Trait) {
|
|||
];
|
||||
}
|
||||
|
||||
static describe() {
|
||||
static children() {
|
||||
return {
|
||||
collidesWith: {
|
||||
advanced: true,
|
||||
|
|
|
@ -56,7 +56,7 @@ export default (latus) => class Emitter extends decorate(Trait) {
|
|||
};
|
||||
}
|
||||
|
||||
static describe(entity) {
|
||||
static children(entity) {
|
||||
return {
|
||||
emitParticleEntity: {
|
||||
type: 'entity',
|
||||
|
|
|
@ -41,7 +41,7 @@ export default (latus) => class Audible extends Trait {
|
|||
};
|
||||
}
|
||||
|
||||
static describe(entity) {
|
||||
static children(entity) {
|
||||
return {
|
||||
hasSound: {
|
||||
args: [
|
||||
|
|
|
@ -58,7 +58,7 @@ export default (latus) => class Animated extends decorate(Trait) {
|
|||
];
|
||||
}
|
||||
|
||||
static describe() {
|
||||
static children() {
|
||||
return {
|
||||
isAnimating: {
|
||||
type: 'bool',
|
||||
|
|
|
@ -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() {}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user