refactor: item use

This commit is contained in:
cha0s 2024-06-27 11:06:58 -05:00
parent 15674fb1d7
commit f89c94b619
3 changed files with 81 additions and 77 deletions

View File

@ -41,7 +41,7 @@ module.exports = {
// React // React
{ {
files: ['**/*.{js,jsx,ts,tsx}'], files: ['**/*.{jsx,tsx}'],
plugins: ['react', 'jsx-a11y'], plugins: ['react', 'jsx-a11y'],
extends: [ extends: [
'plugin:react/recommended', 'plugin:react/recommended',

View File

@ -1,69 +1,92 @@
import Component from '@/ecs/component.js'; import Component from '@/ecs/component.js';
import Script from '@/util/script.js';
export default class Wielder extends Component { export default class Wielder extends Component {
instanceFromSchema() { instanceFromSchema() {
const {ecs} = this;
const Instance = super.instanceFromSchema(); const Instance = super.instanceFromSchema();
const Component = this; return class WielderInstance extends Instance {
Instance.prototype.activeItem = async function () { async activeItem() {
const {Inventory, Wielder} = Component.ecs.get(this.entity); const {Inventory, Wielder} = ecs.get(this.entity);
return Inventory.item(Wielder.activeSlot + 1); 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 = []; project(position, projection) {
for (const row in projection.grid) { const {TileLayers: {layers: [layer]}} = ecs.get(1);
const columns = projection.grid[row]; const {Direction: {direction}} = ecs.get(this.entity);
for (const column in columns) { let startX = position.x;
const targeted = projection.grid[row][column]; let startY = position.y;
if (targeted) { switch (direction) {
let axe; case 0:
switch (direction) { startX += projection.distance[1];
case 0: startY -= projection.distance[0];
axe = [column, row]; break;
break; case 1:
case 1: startX += projection.distance[0];
axe = [-row, column]; startY += projection.distance[1];
break; break;
case 2: case 2:
axe = [-column, -row]; startX -= projection.distance[1];
break; startY += projection.distance[0];
case 3: break;
axe = [row, -column]; case 3:
break; 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;
}
projected.push({x, y});
} }
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});
} }
} }
return projected;
}
async useActiveItem(state) {
const entity = ecs.get(this.entity);
const {Ticking} = entity;
const activeItem = await this.activeItem();
if (activeItem) {
ecs.readAsset([activeItem.source, state ? 'start.js' : 'stop.js'].join('/'))
.then((script) => (script.ok ? script.text() : ''))
.then((code) => {
if (code) {
const context = {
ecs,
item: activeItem,
wielder: entity,
};
Ticking.addTickingPromise(Script.tickingPromise(code, context));
}
});
}
} }
return projected;
} }
return Instance;
} }
static properties = { static properties = {
activeSlot: {type: 'uint16'}, activeSlot: {type: 'uint16'},

View File

@ -5,7 +5,6 @@ import Ecs from '@/ecs/ecs.js';
import Components from '@/ecs-components/index.js'; import Components from '@/ecs-components/index.js';
import Systems from '@/ecs-systems/index.js'; import Systems from '@/ecs-systems/index.js';
import {decode, encode} from '@/packets/index.js'; import {decode, encode} from '@/packets/index.js';
import Script from '@/util/script.js';
function join(...parts) { function join(...parts) {
return parts.join('/'); return parts.join('/');
@ -48,7 +47,7 @@ export default class Engine {
entity, entity,
payload, payload,
] of this.incomingActions) { ] of this.incomingActions) {
const {Controlled, Inventory, Ticking, Wielder} = entity; const {Controlled, Inventory, Wielder} = entity;
switch (payload.type) { switch (payload.type) {
case 'changeSlot': { case 'changeSlot': {
if (!Controlled.locked) { if (!Controlled.locked) {
@ -71,25 +70,7 @@ export default class Engine {
} }
case 'use': { case 'use': {
if (!Controlled.locked) { if (!Controlled.locked) {
Inventory.item(Wielder.activeSlot + 1).then(async (item) => { Wielder.useActiveItem(payload.value);
if (item) {
const code = await(
this.server.readAsset([
item.source,
payload.value ? 'start.js' : 'stop.js',
].join('/'))
.then((script) => (script.ok ? script.text() : ''))
);
if (code) {
const context = {
ecs: this.ecses[entity.Ecs.path],
item,
wielder: entity,
};
Ticking.addTickingPromise(Script.tickingPromise(code, context));
}
}
});
} }
break; break;
} }