feat: graphical trait
This commit is contained in:
parent
f22ea42b54
commit
9e74aaf7e7
52
packages/entity/traits/graphical.js
Normal file
52
packages/entity/traits/graphical.js
Normal file
|
@ -0,0 +1,52 @@
|
|||
import {compose} from '@avocado/core';
|
||||
import {Container} from '@avocado/graphics';
|
||||
|
||||
import {simpleState, Trait} from '../trait';
|
||||
|
||||
const decorate = compose(
|
||||
);
|
||||
|
||||
class GraphicalBase extends Trait {
|
||||
|
||||
static defaultParams() {
|
||||
return {
|
||||
trackPosition: true,
|
||||
};
|
||||
}
|
||||
|
||||
static defaultState() {
|
||||
return {};
|
||||
}
|
||||
|
||||
constructor(entity) {
|
||||
super(entity);
|
||||
this._container = new Container();
|
||||
}
|
||||
|
||||
get container() {
|
||||
return this._container;
|
||||
}
|
||||
|
||||
listeners() {
|
||||
const trackPosition = this.params.get('trackPosition');
|
||||
if (!trackPosition || !this._container.isValid) {
|
||||
return {};
|
||||
}
|
||||
return {
|
||||
traitAdded: (trait) => {
|
||||
if ('positioned' === trait.constructor.type()) {
|
||||
this._container.position = this.entity.position;
|
||||
}
|
||||
},
|
||||
xChanged: (x) => {
|
||||
this._container.x = x;
|
||||
},
|
||||
yChanged: (y) => {
|
||||
this._container.y = y;
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export class Graphical extends decorate(GraphicalBase) {}
|
|
@ -22,6 +22,9 @@ registerTrait(Directional);
|
|||
import {Existent} from './existent';
|
||||
registerTrait(Existent);
|
||||
|
||||
import {Graphical} from './graphical';
|
||||
registerTrait(Graphical);
|
||||
|
||||
import {Mobile} from './mobile';
|
||||
registerTrait(Mobile);
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user