silphius/app/ecs-components/wielder.js

77 lines
2.4 KiB
JavaScript
Raw Normal View History

2024-06-25 05:46:03 -05:00
import Schema from '@/ecs/schema.js';
export default function(Component) {
return 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);
};
2024-06-25 06:20:09 -05:00
Instance.prototype.project = function(position, projection) {
2024-06-26 09:17:35 -05:00
const {TileLayers: {layers: [layer]}} = Component.ecs.get(1);
2024-06-25 06:20:09 -05:00
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;
}
2024-06-26 09:17:35 -05:00
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;
}
projected.push({x, y});
2024-06-25 06:20:09 -05:00
}
}
}
return projected;
}
2024-06-25 05:46:03 -05:00
return Instance;
}
static schema = new Schema({
type: 'object',
properties: {
activeSlot: {type: 'uint16'},
},
});
}
}