refactor: trait params and state passed as args, Trait::fromJSON removed

This commit is contained in:
cha0s 2019-03-19 10:02:18 -05:00
parent b9cbccede8
commit 701623ea31
5 changed files with 27 additions and 39 deletions

View File

@ -5,29 +5,27 @@ import {Property} from '@avocado/mixins';
export class Trait {
constructor(entity) {
constructor(entity, params, state) {
this.entity = entity;
this.params = I.fromJS(this.constructor.defaultParams());
this.state = I.fromJS(this.constructor.defaultState());
this.params = I.fromJS(this.constructor.defaultParams()).merge(params);
this.state = I.fromJS(this.constructor.defaultState()).merge(state);
}
acceptStateChange(change) {
if (change.params) {
this.params = this.params.merge(I.fromJS(change.params));
if (!change.state) {
return;
}
if (change.state) {
const undefinedProperties = {};
for (const key in change.state) {
const value = change.state[key];
if (key in this.entity) {
this.entity[key] = value;
}
else {
undefinedProperties[key] = value;
}
const undefinedProperties = {};
for (const key in change.state) {
const value = change.state[key];
if (key in this.entity) {
this.entity[key] = value;
}
else {
undefinedProperties[key] = value;
}
this.state = this.state.merge(undefinedProperties);
}
this.state = this.state.merge(undefinedProperties);
}
actions() {
@ -36,12 +34,6 @@ export class Trait {
destroy() {}
fromJSON({params = {}, state = {}}) {
this.params = I.fromJS(this.constructor.defaultParams()).merge(params);
this.state = I.fromJS(this.constructor.defaultState()).merge(state);
return this;
}
hooks() {
return {};
}

View File

@ -27,9 +27,14 @@ class AnimatedBase extends Trait {
};
}
constructor(entity) {
super(entity);
constructor(entity, params, state) {
super(entity, params, state);
this.sprite = undefined;
this.frameCount = this.params.get('frameCount');
this.frameRate = this.params.get('frameRate');
this.frameSize = this.params.get('frameSize');
this.image = this.params.get('image');
this.frameCaret = this.frameRate;
}
get frameRect() {
@ -42,16 +47,6 @@ class AnimatedBase extends Trait {
];
}
fromJSON(json) {
super.fromJSON(json);
this.frameCount = json.params.frameCount;
this.frameRate = json.params.frameRate;
this.frameSize = json.params.frameSize;
this.image = json.params.image;
this.frameCaret = this.frameRate;
return this;
}
listeners() {
return {
currentFrameChanged: () => {

View File

@ -18,8 +18,8 @@ class GraphicalBase extends Trait {
return {};
}
constructor(entity) {
super(entity);
constructor(...args) {
super(...args);
this._container = new Container();
}

View File

@ -86,7 +86,8 @@ export class Traits {
return;
}
// Instantiate.
const instance = (new Trait(this.entity_PRIVATE)).fromJSON(json);
const {params, state} = json;
const instance = new Trait(this.entity_PRIVATE, params, state);
// Attach listeners.
const listeners = instance.listeners();
for (const eventName in listeners) {

View File

@ -17,8 +17,8 @@ class MobileBase extends Trait {
};
}
constructor(entity) {
super(entity);
constructor(...args) {
super(...args);
this.requestedMovement = [0, 0];
}