refactor: compression

This commit is contained in:
cha0s 2021-04-28 11:01:44 -05:00
parent 813e4a1cda
commit 9d6eb3c2cf
2 changed files with 29 additions and 18 deletions

View File

@ -59,8 +59,8 @@ export default (latus) => {
tilesetUri,
world,
} = json;
const {EntityList, Tiles, Tileset} = latus.get('%resources');
this.setTiles(new Tiles(tiles));
const {EntityList, Tileset} = latus.get('%resources');
await this.tiles.load(tiles);
this.tileset = tilesetUri
? await Tileset.load({extends: tilesetUri})
: new Tileset();

View File

@ -4,7 +4,13 @@ import {
Vector,
Vertice,
} from '@avocado/math';
import {Class, compose, EventEmitter} from '@latus/core';
import {
Class,
compose,
deflate,
EventEmitter,
inflate,
} from '@latus/core';
import {Synchronized} from '@avocado/s13n';
// TODO: rows, with nulls
@ -19,20 +25,14 @@ export default (latus) => {
);
return class Tiles extends decorate(Class) {
#data = [];
#hasUpdates = false;
#packets = [];
#updates = [];
constructor({data, size} = {}) {
super();
if (size) {
super.size = size;
}
this.data = data && data.length > 0 ? data.concat() : Array(Vector.area(this.size)).fill(0);
}
acceptPacket(packet) {
if ('TilesUpdate' === packet.constructor.type) {
const {position: [x, y], size: [w, h], tiles} = packet.data;
@ -108,7 +108,18 @@ export default (latus) => {
}
indexHulls(indices) {
return this.constructor.indexHulls(indices, this.data, this.size);
return this.constructor.indexHulls(indices, this.#data, this.size);
}
async load(json) {
const {data, size} = json;
if (size) {
this.size = size;
}
if (data) {
const {buffer, byteOffset, length} = inflate(Buffer.from(data, 'base64'));
this.#data = new Uint16Array(buffer, byteOffset, length / 2);
}
}
packetsFor() {
@ -139,10 +150,10 @@ export default (latus) => {
setTileAt([x, y], tile) {
const [w, h] = this.size;
const index = y * w + x;
if (x < 0 || x >= w || y < 0 || y >= h || this.data[index] === tile) {
if (x < 0 || x >= w || y < 0 || y >= h || this.#data[index] === tile) {
return;
}
this.data[index] = tile;
this.#data[index] = tile;
if ('client' !== process.env.SIDE) {
this.#updates.push([[x, y], [1, 1], [tile]]);
this.#hasUpdates = true;
@ -161,7 +172,7 @@ export default (latus) => {
let j = 0;
for (let l = 0; l < h; ++l) {
for (let k = 0; k < w; ++k) {
slice[j] = x < 0 || x >= fw || y < 0 || y >= fh ? 0 : this.data[i];
slice[j] = x < 0 || x >= fw || y < 0 || y >= fh ? 0 : this.#data[i];
x += 1;
i += 1;
j += 1;
@ -190,7 +201,7 @@ export default (latus) => {
for (let yy = 0; yy < h; ++yy) {
for (let xx = 0; xx < w; ++xx) {
if (sx >= 0 && sx < fw && sy >= 0 && sy < fh) {
this.data[i] = tiles[j];
this.#data[i] = tiles[j];
isDirty = true;
}
i += 1;
@ -217,7 +228,7 @@ export default (latus) => {
tileAt([x, y]) {
const [w, h] = this.size;
return x < 0 || x >= w || y < 0 || y >= h ? undefined : this.data[y * w + x];
return x < 0 || x >= w || y < 0 || y >= h ? undefined : this.#data[y * w + x];
}
toNetwork() {
@ -227,7 +238,7 @@ export default (latus) => {
toJSON() {
return {
size: this.size,
data: this.data,
data: deflate(this.#data).toString('base64'),
};
}