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
{
files: ['**/*.{js,jsx,ts,tsx}'],
files: ['**/*.{jsx,tsx}'],
plugins: ['react', 'jsx-a11y'],
extends: [
'plugin:react/recommended',

View File

@ -1,16 +1,18 @@
import Component from '@/ecs/component.js';
import Script from '@/util/script.js';
export default class Wielder extends Component {
instanceFromSchema() {
const {ecs} = this;
const Instance = super.instanceFromSchema();
const Component = this;
Instance.prototype.activeItem = async function () {
const {Inventory, Wielder} = Component.ecs.get(this.entity);
return class WielderInstance extends Instance {
async activeItem() {
const {Inventory, Wielder} = 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);
}
project(position, projection) {
const {TileLayers: {layers: [layer]}} = ecs.get(1);
const {Direction: {direction}} = ecs.get(this.entity);
let startX = position.x;
let startY = position.y;
switch (direction) {
@ -63,7 +65,28 @@ export default class Wielder extends Component {
}
return projected;
}
return Instance;
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));
}
});
}
}
}
}
static properties = {
activeSlot: {type: 'uint16'},

View File

@ -5,7 +5,6 @@ import Ecs from '@/ecs/ecs.js';
import Components from '@/ecs-components/index.js';
import Systems from '@/ecs-systems/index.js';
import {decode, encode} from '@/packets/index.js';
import Script from '@/util/script.js';
function join(...parts) {
return parts.join('/');
@ -48,7 +47,7 @@ export default class Engine {
entity,
payload,
] of this.incomingActions) {
const {Controlled, Inventory, Ticking, Wielder} = entity;
const {Controlled, Inventory, Wielder} = entity;
switch (payload.type) {
case 'changeSlot': {
if (!Controlled.locked) {
@ -71,25 +70,7 @@ export default class Engine {
}
case 'use': {
if (!Controlled.locked) {
Inventory.item(Wielder.activeSlot + 1).then(async (item) => {
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));
}
}
});
Wielder.useActiveItem(payload.value);
}
break;
}