feat: impassable
This commit is contained in:
parent
5f1ae54cd9
commit
9671e1e75c
|
@ -7,53 +7,60 @@ import PolygonShape from '../../shape/polygon';
|
|||
|
||||
export default (Layer) => class PhysicsLayer extends Layer {
|
||||
|
||||
#impassable = {};
|
||||
|
||||
#tileBodies = [];
|
||||
|
||||
#world;
|
||||
|
||||
addTileBodies() {
|
||||
if (!this.hasImpassableLayerZero()) {
|
||||
return;
|
||||
}
|
||||
if (!this.#world) {
|
||||
return;
|
||||
}
|
||||
const hulls = this.tiles.indexHulls(0);
|
||||
if (!hulls.length > 0) {
|
||||
return;
|
||||
}
|
||||
const {tileset} = this;
|
||||
if (!tileset) {
|
||||
return;
|
||||
}
|
||||
for (let i = 0; i < hulls.length; ++i) {
|
||||
const scaled = [];
|
||||
for (let j = 0; j < hulls[i].length; j++) {
|
||||
scaled.push(Vector.mul(hulls[i][j], tileset.tileSize));
|
||||
const indices = Object.keys(this.#impassable).map((i) => parseInt(i, 10));
|
||||
for (let i = 0; i < indices.length; i++) {
|
||||
const hulls = this.tiles.indexHulls(indices[i]);
|
||||
if (0 === hulls.length) {
|
||||
// eslint-disable-next-line no-continue
|
||||
continue;
|
||||
}
|
||||
for (let j = 0; j < hulls.length; ++j) {
|
||||
const scaled = [];
|
||||
for (let k = 0; k < hulls[j].length; ++k) {
|
||||
scaled.push(Vector.mul(hulls[j][k], tileset.tileSize));
|
||||
}
|
||||
const [vertices, position] = Vertice.localize(scaled);
|
||||
const shape = new PolygonShape({position, vertices});
|
||||
const body = this.#world.createBody(shape);
|
||||
body.static = true;
|
||||
this.#tileBodies.push(body);
|
||||
this.#world.addBody(body);
|
||||
}
|
||||
const [vertices, position] = Vertice.localize(scaled);
|
||||
const shape = new PolygonShape({position, vertices});
|
||||
const body = this.#world.createBody(shape);
|
||||
body.static = true;
|
||||
this.#tileBodies.push(body);
|
||||
this.#world.addBody(body);
|
||||
}
|
||||
}
|
||||
|
||||
hasImpassableLayerZero() {
|
||||
return 1 === this.index;
|
||||
async load(json = {}) {
|
||||
await super.load(json);
|
||||
const {impassable = []} = json;
|
||||
this.#impassable = impassable.reduce((r, index) => ({...r, [index]: true}), {});
|
||||
}
|
||||
|
||||
onTileDataChanged() {
|
||||
this.removeTileBodies();
|
||||
this.addTileBodies();
|
||||
}
|
||||
|
||||
removeTileBodies() {
|
||||
if (!this.hasImpassableLayerZero()) {
|
||||
return;
|
||||
}
|
||||
if (this.#tileBodies.length > 0 && this.#world) {
|
||||
for (let i = 0; i < this.#tileBodies.length; i++) {
|
||||
this.#world.removeBody(this.#tileBodies[i]);
|
||||
}
|
||||
this.#tileBodies = [];
|
||||
}
|
||||
this.#tileBodies = [];
|
||||
}
|
||||
|
||||
set world(world) {
|
||||
|
|
|
@ -2,18 +2,12 @@ export default (Layers) => class PhysicsLayers extends Layers {
|
|||
|
||||
addLayer(layer) {
|
||||
super.addLayer(layer);
|
||||
layer.on('tile-data-changed', this.onTileDataChanged, this);
|
||||
}
|
||||
|
||||
onTileDataChanged() {
|
||||
const layer = this.layers[1];
|
||||
layer.removeTileBodies();
|
||||
layer.addTileBodies();
|
||||
layer.on('tile-data-changed', layer.onTileDataChanged, layer);
|
||||
}
|
||||
|
||||
removeLayer(layer) {
|
||||
if (-1 !== this.layers.indexOf(layer)) {
|
||||
layer.off('tile-data-changed', this.onTileDataChanged);
|
||||
layer.off('tile-data-changed', layer.onTileDataChanged);
|
||||
}
|
||||
super.removeLayer(layer);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user