refactor: less layer proxy churn
This commit is contained in:
parent
1ee8f206de
commit
64ece0cb86
|
@ -2,55 +2,21 @@ import Component from '@/ecs/component.js';
|
||||||
|
|
||||||
import vector2d from './helpers/vector-2d';
|
import vector2d from './helpers/vector-2d';
|
||||||
|
|
||||||
export default class TileLayers extends Component {
|
|
||||||
insertMany(entities) {
|
|
||||||
for (const [id, {layerChange}] of entities) {
|
|
||||||
if (layerChange) {
|
|
||||||
const component = this.get(id);
|
|
||||||
const {layers} = component;
|
|
||||||
for (const layerIndex in layerChange) {
|
|
||||||
for (const calculated in layerChange[layerIndex]) {
|
|
||||||
const tile = layerChange[layerIndex][calculated];
|
|
||||||
layers[layerIndex].data[calculated] = tile;
|
|
||||||
}
|
|
||||||
layers[layerIndex] = {...layers[layerIndex]};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return super.insertMany(entities);
|
|
||||||
}
|
|
||||||
mergeDiff(original, update) {
|
|
||||||
if (!update.layerChange) {
|
|
||||||
return super.mergeDiff(original, update);
|
|
||||||
}
|
|
||||||
const layerChange = {
|
|
||||||
...original.layerChange,
|
|
||||||
};
|
|
||||||
for (const index in update.layerChange) {
|
|
||||||
layerChange[index] = {
|
|
||||||
...layerChange[index],
|
|
||||||
...update.layerChange[index],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return {layerChange};
|
|
||||||
}
|
|
||||||
instanceFromSchema() {
|
|
||||||
const Instance = super.instanceFromSchema();
|
|
||||||
const Component = this;
|
|
||||||
return class TileLayersInstance extends Instance {
|
|
||||||
layer(index) {
|
|
||||||
const {layers} = this;
|
|
||||||
if (!(index in layers)) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
const instance = this;
|
|
||||||
class LayerProxy {
|
class LayerProxy {
|
||||||
constructor(layer) {
|
constructor(instance, Component, index) {
|
||||||
this.layer = layer;
|
this.instance = instance;
|
||||||
|
this.Component = Component;
|
||||||
|
this.index = index;
|
||||||
}
|
}
|
||||||
get area() {
|
get area() {
|
||||||
return this.layer.area;
|
return this.layer.area;
|
||||||
}
|
}
|
||||||
|
get data() {
|
||||||
|
return this.layer.data;
|
||||||
|
}
|
||||||
|
get layer() {
|
||||||
|
return this.instance.layers[this.index];
|
||||||
|
}
|
||||||
get source() {
|
get source() {
|
||||||
return this.layer.source;
|
return this.layer.source;
|
||||||
}
|
}
|
||||||
|
@ -70,7 +36,7 @@ export default class TileLayers extends Component {
|
||||||
changes[calculated] = tile;
|
changes[calculated] = tile;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Component.markChange(instance.entity, 'layerChange', {[index]: changes});
|
this.Component.markChange(this.instance.entity, 'layerChange', {[this.index]: changes});
|
||||||
}
|
}
|
||||||
tile({x, y}) {
|
tile({x, y}) {
|
||||||
if (x < 0 || y < 0 || x >= this.layer.area.x || y >= this.layer.area.y) {
|
if (x < 0 || y < 0 || x >= this.layer.area.x || y >= this.layer.area.y) {
|
||||||
|
@ -82,7 +48,50 @@ export default class TileLayers extends Component {
|
||||||
return this.layer.tileSize;
|
return this.layer.tileSize;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new LayerProxy(layers[index]);
|
|
||||||
|
export default class TileLayers extends Component {
|
||||||
|
insertMany(entities) {
|
||||||
|
for (const [id, {layerChange}] of entities) {
|
||||||
|
if (layerChange) {
|
||||||
|
const component = this.get(id);
|
||||||
|
const {layers} = component;
|
||||||
|
for (const layerIndex in layerChange) {
|
||||||
|
for (const calculated in layerChange[layerIndex]) {
|
||||||
|
const tile = layerChange[layerIndex][calculated];
|
||||||
|
layers[layerIndex].data[calculated] = tile;
|
||||||
|
}
|
||||||
|
layers[layerIndex] = {...layers[layerIndex]};
|
||||||
|
component.$$layersProxies[layerIndex] = new LayerProxy(component, this, layerIndex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return super.insertMany(entities);
|
||||||
|
}
|
||||||
|
load(instance) {
|
||||||
|
for (const index in instance.layers) {
|
||||||
|
instance.$$layersProxies[index] = new LayerProxy(instance, this, index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mergeDiff(original, update) {
|
||||||
|
if (!update.layerChange) {
|
||||||
|
return super.mergeDiff(original, update);
|
||||||
|
}
|
||||||
|
const layerChange = {
|
||||||
|
...original.layerChange,
|
||||||
|
};
|
||||||
|
for (const index in update.layerChange) {
|
||||||
|
layerChange[index] = {
|
||||||
|
...layerChange[index],
|
||||||
|
...update.layerChange[index],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return {layerChange};
|
||||||
|
}
|
||||||
|
instanceFromSchema() {
|
||||||
|
return class TileLayersInstance extends super.instanceFromSchema() {
|
||||||
|
$$layersProxies = {};
|
||||||
|
layer(index) {
|
||||||
|
return this.$$layersProxies[index];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user