fix: tile updates
This commit is contained in:
parent
a2b5583d44
commit
529d308b8b
|
@ -14,6 +14,10 @@ import {ShapeList} from '@avocado/physics';
|
|||
import {
|
||||
LayerUpdateTilesetUriPacket,
|
||||
} from './packets/layer-update-tileset-uri.packet';
|
||||
import {
|
||||
TilesUpdatePacket,
|
||||
} from './packets/tiles-update.packet';
|
||||
|
||||
|
||||
import {Tiles} from './tiles';
|
||||
import {Tileset} from './tileset';
|
||||
|
@ -68,6 +72,9 @@ export class Layer extends decorate(class {}) {
|
|||
if (packet instanceof SynchronizedDestroyPacket) {
|
||||
this.entityList.acceptPacket(packet);
|
||||
}
|
||||
if (packet instanceof TilesUpdatePacket) {
|
||||
this.tiles.acceptPacket(packet);
|
||||
}
|
||||
}
|
||||
|
||||
addEntity(entity) {
|
||||
|
@ -119,6 +126,7 @@ export class Layer extends decorate(class {}) {
|
|||
cleanPackets() {
|
||||
this._tilesetUriChanged = false;
|
||||
this.entityList.cleanPackets();
|
||||
this.tiles.cleanPackets();
|
||||
}
|
||||
|
||||
destroy() {
|
||||
|
@ -225,6 +233,10 @@ export class Layer extends decorate(class {}) {
|
|||
for (let i = 0; i < entityListPackets.length; i++) {
|
||||
packets.push(entityListPackets[i]);
|
||||
}
|
||||
const tilesPackets = this.tiles.packets(informed);
|
||||
for (let i = 0; i < tilesPackets.length; i++) {
|
||||
packets.push(tilesPackets[i]);
|
||||
}
|
||||
return packets;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,16 +0,0 @@
|
|||
import {Packet} from '@avocado/net';
|
||||
|
||||
export class TileUpdatePacket extends Packet {
|
||||
|
||||
static get schema() {
|
||||
return {
|
||||
...super.schema,
|
||||
data: {
|
||||
...super.schema.data,
|
||||
position: 'uint32',
|
||||
tile: 'uint16',
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
}
|
28
packages/topdown/packets/tiles-update.packet.js
Normal file
28
packages/topdown/packets/tiles-update.packet.js
Normal file
|
@ -0,0 +1,28 @@
|
|||
import {Vector} from '@avocado/math';
|
||||
import {Packet} from '@avocado/net';
|
||||
|
||||
export class TilesUpdatePacket extends Packet {
|
||||
|
||||
static pack(packet) {
|
||||
const data = packet.data[1];
|
||||
data.position = Vector.packToUint32(data.position);
|
||||
return super.pack(packet);
|
||||
}
|
||||
|
||||
static get schema() {
|
||||
return {
|
||||
...super.schema,
|
||||
data: {
|
||||
position: 'uint32',
|
||||
tile: 'uint16',
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
static unpack(packet) {
|
||||
const data = super.unpack(packet);
|
||||
data.position = Vector.unpackFromUint32(data.position);
|
||||
return data;
|
||||
}
|
||||
|
||||
}
|
|
@ -3,7 +3,7 @@ import * as I from 'immutable';
|
|||
import {compose, EventEmitter} from '@avocado/core';
|
||||
import {Rectangle, Vector} from '@avocado/math';
|
||||
|
||||
import {TileUpdatePacket} from './packets/tile-update.packet';
|
||||
import {TilesUpdatePacket} from './packets/tiles-update.packet';
|
||||
|
||||
const CHUNK_AXIS = 10;
|
||||
|
||||
|
@ -18,51 +18,22 @@ export class Tiles extends decorate(class {}) {
|
|||
|
||||
constructor(json) {
|
||||
super();
|
||||
// this.chunks = [];
|
||||
this.data = [];
|
||||
this._packets = [];
|
||||
if ('undefined' !== typeof json) {
|
||||
this.fromJSON(json);
|
||||
}
|
||||
}
|
||||
|
||||
acceptPacket(packet) {
|
||||
// if (packet instanceof TileUpdatePacket) {
|
||||
// const unpackedPosition = [
|
||||
// packet.data.position & 0xFFFF,
|
||||
// packet.data.position >> 16,
|
||||
// ];
|
||||
// this.setTileAt(unpackedPosition, packet.data.tile);
|
||||
// }
|
||||
if (packet instanceof TilesUpdatePacket) {
|
||||
this.setTileAt(packet.data.position, packet.data.tile);
|
||||
}
|
||||
}
|
||||
|
||||
// chunkIndexAtPosition(position) {
|
||||
// const chunkCount = Vector.ceil(Vector.scale(this.size, 1 / CHUNK_AXIS));
|
||||
// const chunkPosition = Vector.floor(Vector.scale(position, 1 / CHUNK_AXIS));
|
||||
// const chunkIndex = chunkPosition[1] * chunkCount[0] + chunkPosition[0];
|
||||
// return chunkIndex;
|
||||
// }
|
||||
|
||||
// static createChunkFromData(position, size, data) {
|
||||
// const chunk = Array(CHUNK_AXIS * CHUNK_AXIS).fill(0);
|
||||
// let chunkIndex = 0;
|
||||
// const chunkPosition = Vector.scale(position, CHUNK_AXIS);
|
||||
// let dataIndex = size[0] * chunkPosition[1] + chunkPosition[0];
|
||||
// for (let y = 0; y < CHUNK_AXIS; ++y) {
|
||||
// for (let x = 0; x < CHUNK_AXIS; ++x) {
|
||||
// if (chunkPosition[0] < size[0] && chunkPosition[1] < size[1]) {
|
||||
// chunk[chunkIndex] = data[dataIndex];
|
||||
// }
|
||||
// chunkIndex += 1;
|
||||
// chunkPosition[0] += 1;
|
||||
// dataIndex += 1;
|
||||
// }
|
||||
// chunkPosition[0] -= CHUNK_AXIS;
|
||||
// chunkPosition[1] += 1;
|
||||
// dataIndex -= CHUNK_AXIS;
|
||||
// dataIndex += size[0];
|
||||
// }
|
||||
// return chunk;
|
||||
// }
|
||||
cleanPackets() {
|
||||
this._packets = [];
|
||||
}
|
||||
|
||||
// forEachTile(fn) {
|
||||
// let [x, y] = [0, 0];
|
||||
|
@ -83,22 +54,6 @@ export class Tiles extends decorate(class {}) {
|
|||
if (json.size) {
|
||||
super.size = json.size;
|
||||
}
|
||||
// this.chunks = [];
|
||||
// const chunkCount = Vector.ceil(Vector.scale(this.size, 1 / CHUNK_AXIS));
|
||||
// if (json.data && json.data.length > 0) {
|
||||
// for (let y = 0; y < chunkCount[1]; ++y) {
|
||||
// for (let x = 0; x < chunkCount[0]; ++x) {
|
||||
// this.chunks.push(Tiles.createChunkFromData(
|
||||
// [x, y],
|
||||
// this.size,
|
||||
// json.data
|
||||
// ));
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// if (json.chunks && json.chunks.length > 0) {
|
||||
|
||||
// }
|
||||
if (json.data && json.data.length > 0) {
|
||||
this.data = json.data.slice(0);
|
||||
}
|
||||
|
@ -109,15 +64,13 @@ export class Tiles extends decorate(class {}) {
|
|||
return this;
|
||||
}
|
||||
|
||||
// indexAt(position) {
|
||||
// return this.width * position[1] + position[0];
|
||||
// }
|
||||
indexAt(position) {
|
||||
return this.width * position[1] + position[0];
|
||||
}
|
||||
|
||||
// packetsForUpdate() {
|
||||
// const packetsForUpdate = this.updatePackets;
|
||||
// this.updatePackets = [];
|
||||
// return packetsForUpdate;
|
||||
// }
|
||||
packets(informed) {
|
||||
return this._packets;
|
||||
}
|
||||
|
||||
get rectangle() {
|
||||
return Rectangle.compose([0, 0], this.size);
|
||||
|
@ -128,38 +81,18 @@ export class Tiles extends decorate(class {}) {
|
|||
if (oldTile === tile) {
|
||||
return;
|
||||
}
|
||||
const index = position[1] * this.width + position[0];
|
||||
const index = this.indexAt(position);
|
||||
if (index < 0 || index >= this.data.length) {
|
||||
return;
|
||||
}
|
||||
this.data[index] = tile;
|
||||
const packedPosition = position[1] << 16 | position[0];
|
||||
// this.updatePackets.push(new TileUpdatePacket({
|
||||
// position: packedPosition,
|
||||
// tile,
|
||||
// }));
|
||||
this._packets.push(new TilesUpdatePacket({
|
||||
position,
|
||||
tile,
|
||||
}));
|
||||
this.emit('dataChanged');
|
||||
}
|
||||
|
||||
get size() {
|
||||
return super.size;
|
||||
}
|
||||
|
||||
set size(size) {
|
||||
super.size = size;
|
||||
// const newChunks = [];
|
||||
// const chunkCount = Vector.ceil(Vector.div(size, [CHUNK_AXIS, CHUNK_AXIS]));
|
||||
// const chunkLinearCount = Vector.area(chunkCount);
|
||||
// for (let i = 0; i < chunkLinearCount; ++i) {
|
||||
// newChunks.
|
||||
// }
|
||||
// for (let y = 0; y < chunkCount[1]; y++) {
|
||||
// for (let x = 0; x < chunkCount[0]; x++) {
|
||||
// newChunks[y][x]
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
slice(rectangle) {
|
||||
const tilesRectangle = this.rectangle;
|
||||
// Get intersection.
|
||||
|
|
Loading…
Reference in New Issue
Block a user