silphius/app/ecs-components/wielder.js

72 lines
2.2 KiB
JavaScript
Raw Normal View History

2024-06-26 21:08:09 -05:00
import Component from '@/ecs/component.js';
2024-06-25 05:46:03 -05:00
2024-06-26 21:08:09 -05:00
export default class Wielder extends Component {
instanceFromSchema() {
const Instance = super.instanceFromSchema();
const Component = this;
Instance.prototype.activeItem = async function () {
const {Inventory, Wielder} = Component.ecs.get(this.entity);
return Inventory.item(Wielder.activeSlot + 1);
};
Instance.prototype.project = function(position, projection) {
const {TileLayers: {layers: [layer]}} = Component.ecs.get(1);
const {Direction: {direction}} = Component.ecs.get(this.entity);
let startX = position.x;
let startY = position.y;
switch (direction) {
case 0:
startX += projection.distance[1];
startY -= projection.distance[0];
break;
case 1:
startX += projection.distance[0];
startY += projection.distance[1];
break;
case 2:
startX -= projection.distance[1];
startY += projection.distance[0];
break;
case 3:
startX -= projection.distance[0];
startY -= projection.distance[1];
break;
}
const projected = [];
for (const row in projection.grid) {
const columns = projection.grid[row];
for (const column in columns) {
const targeted = projection.grid[row][column];
if (targeted) {
let axe;
switch (direction) {
case 0:
axe = [column, row];
break;
case 1:
axe = [-row, column];
break;
case 2:
axe = [-column, -row];
break;
case 3:
axe = [row, -column];
break;
}
const x = startX + parseInt(axe[0]);
const y = startY + parseInt(axe[1]);
if (x < 0 || y < 0 || x >= layer.area.x || y >= layer.area.y) {
continue;
2024-06-25 06:20:09 -05:00
}
2024-06-26 21:08:09 -05:00
projected.push({x, y});
2024-06-25 06:20:09 -05:00
}
}
}
2024-06-26 21:08:09 -05:00
return projected;
2024-06-25 05:46:03 -05:00
}
2024-06-26 21:08:09 -05:00
return Instance;
2024-06-25 05:46:03 -05:00
}
2024-06-26 21:08:09 -05:00
static properties = {
activeSlot: {type: 'uint16'},
};
2024-06-25 05:46:03 -05:00
}