113 lines
2.4 KiB
JavaScript
113 lines
2.4 KiB
JavaScript
import * as I from 'immutable';
|
|
|
|
import {compose} from '@avocado/core';
|
|
import {simpleState, Trait} from '@avocado/entity';
|
|
|
|
const decorate = compose(
|
|
simpleState('informSize'),
|
|
);
|
|
|
|
class InformedBase extends Trait {
|
|
|
|
static defaultState() {
|
|
return {
|
|
informSize: [1280, 720],
|
|
};
|
|
}
|
|
|
|
initialize() {
|
|
this._sentSelf = false;
|
|
this._socket = undefined;
|
|
this.lastNearby = I.Set();
|
|
}
|
|
|
|
destroy() {
|
|
if (this._socket) {
|
|
delete this._socket.entity;
|
|
delete this._socket;
|
|
}
|
|
}
|
|
|
|
reduceStateDiffForEntityList(diff, position) {
|
|
// Only if entity list exists.
|
|
if (!diff.entityList) {
|
|
return diff;
|
|
}
|
|
// Reduce the entity list.
|
|
const informSize = this.entity.informSize.toJS();
|
|
const nearbyEntities = this.entity.nearbyEntities(informSize, position);
|
|
const reducedEntityList = {};
|
|
let nearby = I.Set();
|
|
for (const entity of nearbyEntities) {
|
|
nearby = nearby.add(entity);
|
|
const uuid = entity.instanceUuid;
|
|
if (this.lastNearby.has(entity)) {
|
|
// Keep diff.
|
|
if (diff.entityList[uuid]) {
|
|
reducedEntityList[uuid] = diff.entityList[uuid];
|
|
}
|
|
}
|
|
else {
|
|
// Added.
|
|
reducedEntityList[uuid] = entity.state().toJS();
|
|
}
|
|
}
|
|
for (const entity of this.lastNearby.values()) {
|
|
const uuid = entity.instanceUuid;
|
|
if (!nearby.has(entity)) {
|
|
// Removed.
|
|
reducedEntityList[uuid] = false;
|
|
}
|
|
}
|
|
// Track who's nearby.
|
|
this.lastNearby = nearby;
|
|
// Merge the reduction.
|
|
diff = {
|
|
...diff,
|
|
entityList: reducedEntityList,
|
|
};
|
|
// Remove dead update.
|
|
if (0 === Object.keys(diff.entityList).length) {
|
|
delete diff.entityList;
|
|
}
|
|
return diff;
|
|
}
|
|
|
|
get socket() {
|
|
return this._socket;
|
|
}
|
|
|
|
set socket(socket) {
|
|
socket.entity = this.entity;
|
|
this._socket = socket;
|
|
}
|
|
|
|
methods() {
|
|
return {
|
|
|
|
inform: (diff) => {
|
|
// Remove dead updates.
|
|
if (0 === Object.keys(diff).length) {
|
|
return;
|
|
}
|
|
if (!this._sentSelf) {
|
|
diff.selfEntity = this.entity.instanceUuid;
|
|
this._sentSelf = true;
|
|
}
|
|
this._socket.send({
|
|
type: 'state-update',
|
|
payload: diff,
|
|
});
|
|
},
|
|
|
|
reduceStateDiff: (diff, position) => {
|
|
return this.reduceStateDiffForEntityList(diff, position);
|
|
},
|
|
|
|
};
|
|
}
|
|
|
|
}
|
|
|
|
export class Informed extends decorate(InformedBase) {}
|