refactor: inline fromJSON for resources

This commit is contained in:
cha0s 2019-05-16 19:42:35 -05:00
parent ad317f1cd6
commit ab4fd16b37
4 changed files with 14 additions and 7 deletions

View File

@ -12,8 +12,7 @@ export class Resource extends decorate(class {}) {
static load(uri) {
return this.read(uri).then((json) => {
const instance = new this();
return instance.fromJSON(json);
return new this(json);
});
}

View File

@ -19,8 +19,7 @@ export class Sound extends Resource {
return this.loadCache[uri];
}
return this.loadCache[uri] = this.read(uri).then((json) => {
const instance = new this();
instance.fromJSON(json);
const instance = new Sound(json);
let lastTime = performance.now();
instance.tickHandle = setInterval(() => {
const now = performance.now();
@ -36,7 +35,7 @@ export class Sound extends Resource {
});
}
constructor() {
constructor(json) {
super();
this.interval = 0;
this.lastPlayHandle = undefined;
@ -45,6 +44,9 @@ export class Sound extends Resource {
this.remaining = 0;
this.sound = undefined;
this.tickHandle = undefined;
if ('undefined' !== typeof json) {
this.fromJSON(json);
}
}
destroy() {

View File

@ -21,9 +21,12 @@ const decorate = compose(
export class Animation extends decorate(Resource) {
constructor() {
constructor(json) {
super();
this.imageUri = undefined;
if ('undefined' !== typeof json) {
this.fromJSON(json);
}
}
get sourceRectangle() {

View File

@ -16,10 +16,13 @@ const decorate = compose(
export class Tileset extends decorate(Resource) {
constructor() {
constructor(json) {
super();
this._geometry = {};
this.subimages = [];
if ('undefined' !== typeof json) {
this.fromJSON(json);
}
}
destroy() {