behaviortree
Version:
A JavaScript implementation of Behavior Trees. They are useful for implementing AIs. For Browsers and NodeJS.
35 lines (34 loc) • 1.03 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const constants_1 = require("../constants");
const Decorator_1 = __importDefault(require("../Decorator"));
const helper_1 = require("../helper");
class CooldownDecorator extends Decorator_1.default {
constructor() {
super(...arguments);
this.lock = false;
this.nodeType = 'CooldownDecorator';
}
setConfig({ cooldown = 5 }) {
this.config = {
cooldown
};
}
decorate(run) {
if (this.lock) {
return constants_1.FAILURE;
}
const result = run();
if (!(0, helper_1.isRunning)(result)) {
this.lock = true;
setTimeout(() => {
this.lock = false;
}, this.config.cooldown * 1000);
}
return result;
}
}
exports.default = CooldownDecorator;