feat: tile calcs to layered
This commit is contained in:
parent
005cb837ad
commit
6aec1875f7
|
@ -1,4 +1,5 @@
|
|||
import {Trait} from '@avocado/entity';
|
||||
import {Vector} from '@avocado/math';
|
||||
|
||||
export class Layered extends Trait {
|
||||
|
||||
|
@ -9,12 +10,63 @@ export class Layered extends Trait {
|
|||
constructor(entity, params, state) {
|
||||
super(entity, params, state);
|
||||
this.entity.layer = null;
|
||||
this._tile = [0, 0];
|
||||
this._tileOffset = [0, 0];
|
||||
this.setCurrentTileFromLayer();
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this.entity.removeFromLayer();
|
||||
}
|
||||
|
||||
get tile() {
|
||||
return this._tile;
|
||||
}
|
||||
|
||||
get tileOffset() {
|
||||
return this._tileOffset;
|
||||
}
|
||||
|
||||
onLayerTilesetChanged() {
|
||||
this.setCurrentTileFromLayer();
|
||||
}
|
||||
|
||||
setCurrentTileFromLayer() {
|
||||
const layer = this.entity.layer;
|
||||
if (!layer) {
|
||||
return;
|
||||
}
|
||||
if (!this.entity.is('positioned')) {
|
||||
return;
|
||||
}
|
||||
const tileset = layer.tileset;
|
||||
if (!tileset) {
|
||||
return;
|
||||
}
|
||||
this._tile = Vector.div(
|
||||
this.entity.position,
|
||||
tileset.tileSize,
|
||||
);
|
||||
this._tileOffset = Vector.mod(
|
||||
this.entity.position,
|
||||
tileset.tileSize,
|
||||
);
|
||||
}
|
||||
|
||||
listeners() {
|
||||
return {
|
||||
|
||||
positionChanged: () => {
|
||||
this.setCurrentTileFromLayer();
|
||||
},
|
||||
|
||||
traitAdded: () => {
|
||||
this.setCurrentTileFromLayer();
|
||||
},
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
methods() {
|
||||
return {
|
||||
|
||||
|
@ -23,12 +75,14 @@ export class Layered extends Trait {
|
|||
if (!layer) {
|
||||
return;
|
||||
}
|
||||
layer.off('tilesetChanged', this.onLayerTilesetChanged);
|
||||
this.entity.layer = null;
|
||||
this.entity.emit('removedFromLayer', layer);
|
||||
},
|
||||
|
||||
setIntoLayer: (layer) => {
|
||||
this.entity.layer = layer;
|
||||
layer.on('tilesetChanged', this.onLayerTilesetChanged, this);
|
||||
this.entity.emit('addedToLayer');
|
||||
},
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user