silphius/app/ecs/components/position.js
2024-07-20 05:07:39 -05:00

39 lines
884 B
JavaScript

import Component from '@/ecs/component.js';
export default class Position extends Component {
instanceFromSchema() {
const {ecs} = this;
return class PositionInstance extends super.instanceFromSchema() {
lastX;
lastY;
get x() {
return super.x;
}
set x(x) {
this.lastX = super.x;
super.x = x;
}
get y() {
return super.y;
}
set y(y) {
this.lastY = super.y;
super.y = y;
}
get tile() {
const {TileLayers} = ecs.get(1);
const {Position: {x, y}} = ecs.get(this.entity);
const {tileSize} = TileLayers.layers[0];
return {
x: (x - (x % tileSize.x)) / tileSize.x,
y: (y - (y % tileSize.y)) / tileSize.y,
}
}
};
}
static properties = {
x: {type: 'float32'},
y: {type: 'float32'},
};
}