refactor: Synchronized is a mixin
This commit is contained in:
parent
81a6ecf5c4
commit
e63766b045
2
TODO.md
2
TODO.md
|
@ -1,7 +1,7 @@
|
|||
# TODO
|
||||
|
||||
- ✔ Remove dependency on decorators
|
||||
- ❌ Synchronized should be a mixin
|
||||
- ✔ Synchronized should be a mixin
|
||||
- ❌ Behavior items should derive type/name
|
||||
- ✔ Precompile behavior traversals
|
||||
- ❌ Abstract physics world tick (no destructuring)
|
||||
|
|
|
@ -5,7 +5,7 @@ import without from 'lodash.without';
|
|||
import {compose} from '@avocado/core';
|
||||
import {EventEmitter} from '@avocado/mixins';
|
||||
import {Resource} from '@avocado/resource';
|
||||
import {SynchronizedMixin} from '@avocado/state';
|
||||
import {Synchronized} from '@avocado/state';
|
||||
|
||||
import {hasTrait, lookupTrait} from './trait-registry';
|
||||
|
||||
|
@ -47,7 +47,7 @@ function enumerateTraitProperties(prototype) {
|
|||
|
||||
const decorate = compose(
|
||||
EventEmitter,
|
||||
SynchronizedMixin,
|
||||
Synchronized,
|
||||
);
|
||||
|
||||
export class Entity extends decorate(Resource) {
|
||||
|
|
|
@ -10,9 +10,10 @@ import {Entity} from '../index';
|
|||
|
||||
const decorate = compose(
|
||||
EventEmitter,
|
||||
Synchronized,
|
||||
);
|
||||
|
||||
export class EntityList extends decorate(Synchronized) {
|
||||
export class EntityList extends decorate(class {}) {
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
|
|
@ -1,11 +1,16 @@
|
|||
import * as I from 'immutable';
|
||||
|
||||
import {compose} from '@avocado/core';
|
||||
import {Vector} from '@avocado/math';
|
||||
import {Property} from '@avocado/mixins';
|
||||
import {Resource} from '@avocado/resource';
|
||||
import {Synchronized} from '@avocado/state';
|
||||
|
||||
export class Trait extends Synchronized {
|
||||
const decorate = compose(
|
||||
Synchronized,
|
||||
);
|
||||
|
||||
export class Trait extends decorate(class {}) {
|
||||
|
||||
constructor(entity, params, state) {
|
||||
super();
|
||||
|
|
|
@ -2,5 +2,5 @@ export {nextStep} from './next-step';
|
|||
export {Packer, Unpacker} from './packer';
|
||||
export {StatePacket} from './packet/state.packet';
|
||||
export {StateKeysPacket} from './packet/state-keys.packet';
|
||||
export {Synchronized, SynchronizedMixin} from './synchronized';
|
||||
export {Synchronized} from './synchronized';
|
||||
export {Synchronizer} from './synchronizer';
|
||||
|
|
|
@ -9,7 +9,7 @@ const decorate = compose(
|
|||
Property('state'),
|
||||
);
|
||||
|
||||
export function SynchronizedMixin(Superclass) {
|
||||
export function Synchronized(Superclass) {
|
||||
return class Synchronized extends decorate(Superclass) {
|
||||
|
||||
constructor(...args) {
|
||||
|
@ -48,7 +48,10 @@ export function SynchronizedMixin(Superclass) {
|
|||
if (!(key in this)) {
|
||||
return;
|
||||
}
|
||||
if (this[key] instanceof Synchronized) {
|
||||
if (
|
||||
'undefined' !== typeof this[key]
|
||||
&& 'undefined' !== typeof this[key].patchState
|
||||
) {
|
||||
this[key].patchState([step]);
|
||||
}
|
||||
else {
|
||||
|
@ -59,13 +62,19 @@ export function SynchronizedMixin(Superclass) {
|
|||
tick(elapsed) {
|
||||
const children = this.synchronizedChildren();
|
||||
for (const key of children) {
|
||||
if (this[key] instanceof Synchronized) {
|
||||
if (
|
||||
'undefined' !== typeof this[key]
|
||||
&& 'undefined' !== typeof this[key].tick
|
||||
) {
|
||||
this[key].tick(elapsed);
|
||||
}
|
||||
}
|
||||
this.state = this.state.withMutations((state) => {
|
||||
for (const key of children) {
|
||||
if (this[key] instanceof Synchronized) {
|
||||
if (
|
||||
'undefined' !== typeof this[key]
|
||||
&& 'undefined' !== typeof this[key].state
|
||||
) {
|
||||
state.set(key, this[key].state);
|
||||
}
|
||||
else {
|
||||
|
@ -77,5 +86,3 @@ export function SynchronizedMixin(Superclass) {
|
|||
|
||||
}
|
||||
}
|
||||
|
||||
export class Synchronized extends SynchronizedMixin(class {}) {}
|
|
@ -21,9 +21,10 @@ const decorate = compose(
|
|||
Property('world', {
|
||||
track: true,
|
||||
}),
|
||||
Synchronized,
|
||||
);
|
||||
|
||||
export class Layer extends decorate(Synchronized) {
|
||||
export class Layer extends decorate(class {}) {
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
|
|
@ -8,9 +8,10 @@ import {Layer} from './layer';
|
|||
|
||||
const decorate = compose(
|
||||
EventEmitter,
|
||||
Synchronized,
|
||||
);
|
||||
|
||||
export class Layers extends decorate(Synchronized) {
|
||||
export class Layers extends decorate(class {}) {
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
|
|
@ -16,10 +16,11 @@ const decorate = compose(
|
|||
Vector.Mixin('size', 'width', 'height', {
|
||||
default: [0, 0],
|
||||
track: true,
|
||||
})
|
||||
}),
|
||||
Synchronized,
|
||||
);
|
||||
|
||||
export class Room extends decorate(Synchronized) {
|
||||
export class Room extends decorate(class {}) {
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
|
|
@ -10,9 +10,10 @@ const decorate = compose(
|
|||
Vector.Mixin('size', 'width', 'height', {
|
||||
default: [0, 0],
|
||||
}),
|
||||
Synchronized,
|
||||
);
|
||||
|
||||
export class Tiles extends decorate(Synchronized) {
|
||||
export class Tiles extends decorate(class {}) {
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
|
Loading…
Reference in New Issue
Block a user