fun: real-life honest-to-goodness tool usage!

This commit is contained in:
cha0s 2019-05-28 16:54:55 -05:00
parent 03f3bf3dc5
commit ced4a1bedd

View File

@ -1,4 +1,5 @@
import {compose} from '@avocado/core';
import {behaviorItemFromJSON, createContext} from '@avocado/behavior';
import {compose, TickingPromise} from '@avocado/core';
import {StateProperty, Trait} from '@avocado/entity';
import {Color, Primitives} from '@avocado/graphics';
import {Rectangle, Vector} from '@avocado/math';
@ -11,6 +12,15 @@ export class Tool extends decorate(Trait) {
static defaultParams() {
return {
condition: {
type: 'condition',
operator: 'or',
operands: [],
},
actions: {
type: 'actions',
traversals: [],
},
target: {
type: 'projection',
distance: 1,
@ -36,6 +46,8 @@ export class Tool extends decorate(Trait) {
throw new Error("Tool::params.target.width must be odd!");
}
this.targets = [];
this.toolCondition = behaviorItemFromJSON(this.params.condition);
this.toolActions = behaviorItemFromJSON(this.params.actions);
}
calculateTargetStart() {
@ -91,16 +103,48 @@ export class Tool extends decorate(Trait) {
}
}
createTargetContext(target) {
const context = createContext();
context.add('item', this.entity);
context.add('target', target);
context.add('user', this.entity.wielder);
return context;
}
onWielderActiveSlotIndexChanged() {
this.calculateTargets();
if (AVOCADO_SERVER) {
return;
}
const wielder = this.entity.wielder;
if (!wielder) {
return;
}
if (!wielder.is('visible')) {
return;
}
if (this.entity === wielder.itemInActiveSlot()) {
wielder.container.addChild(this.guidePrimitives);
}
else {
wielder.container.removeChild(this.guidePrimitives);
}
}
onWielderDirectionChanged() {
this.calculateTargets();
this.renderPrimitives();
}
onWielderPositionChanged() {
this.calculateTargets();
this.repositionPrimitives();
}
renderPrimitives() {
if (AVOCADO_SERVER) {
return;
}
this.guidePrimitives.clear();
const wielder = this.entity.wielder;
if (!wielder) {
@ -131,6 +175,9 @@ export class Tool extends decorate(Trait) {
}
repositionPrimitives() {
if (AVOCADO_SERVER) {
return;
}
const wielder = this.entity.wielder;
if (!wielder) {
return;
@ -154,7 +201,10 @@ export class Tool extends decorate(Trait) {
'directionChanged',
this.onWielderDirectionChanged,
);
oldWielder.container.removeChild(this.guidePrimitives);
oldWielder.off(
'activeSlotIndexChanged',
this.onWielderActiveSlotIndexChanged,
);
}
if (newWielder && newWielder.is('visible')) {
newWielder.on(
@ -167,7 +217,11 @@ export class Tool extends decorate(Trait) {
this.onWielderDirectionChanged,
this,
);
newWielder.container.addChild(this.guidePrimitives);
newWielder.on(
'activeSlotIndexChanged',
this.onWielderActiveSlotIndexChanged,
this,
);
}
this.calculateTargets();
this.renderPrimitives();
@ -177,5 +231,59 @@ export class Tool extends decorate(Trait) {
};
}
methods() {
return {
useTool: () => {
this.calculateTargets();
const promises = [];
const toolActions = [];
const toolActionsContexts = [];
for (let i = 0; i < this.targets.length; ++i) {
const target = this.targets[i];
const context = this.createTargetContext(target);
// Each target checks condition.
if (!this.toolCondition.get(context)) {
continue;
}
// Clone actions for each valid target.
const Actions = this.toolActions.constructor;
const actions = new Actions();
actions.clone(this.toolActions);
toolActions.push(actions);
toolActionsContexts.push(context);
// Push action finished promise.
promises.push(new Promise((resolve) => {
actions.once('actionsFinished', () => {
toolActions.splice(
toolActions.indexOf(actions),
1
);
toolActionsContexts.splice(
toolActionsContexts.indexOf(context),
1
);
resolve();
});
}));
}
// Resolve on all promises.
const tickingPromise = new TickingPromise((resolve) => {
resolve(Promise.all(promises));
});
// Tick actions for each target.
tickingPromise.ticker = (elapsed) => {
for (let i = 0; i < toolActions.length; ++i) {
const actions = toolActions[i];
const context = toolActionsContexts[i];
actions.tick(context, elapsed);
}
};
return tickingPromise;
},
};
}
}