refactor: item use
This commit is contained in:
parent
15674fb1d7
commit
f89c94b619
|
@ -41,7 +41,7 @@ module.exports = {
|
|||
|
||||
// React
|
||||
{
|
||||
files: ['**/*.{js,jsx,ts,tsx}'],
|
||||
files: ['**/*.{jsx,tsx}'],
|
||||
plugins: ['react', 'jsx-a11y'],
|
||||
extends: [
|
||||
'plugin:react/recommended',
|
||||
|
|
|
@ -1,69 +1,92 @@
|
|||
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 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;
|
||||
return class WielderInstance extends Instance {
|
||||
async activeItem() {
|
||||
const {Inventory, Wielder} = ecs.get(this.entity);
|
||||
return Inventory.item(Wielder.activeSlot + 1);
|
||||
}
|
||||
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;
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
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 = {
|
||||
activeSlot: {type: 'uint16'},
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user