UNPKG

3.46 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = Object.setPrototypeOf ||
4 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6 return function (d, b) {
7 extendStatics(d, b);
8 function __() { this.constructor = d; }
9 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10 };
11})();
12Object.defineProperty(exports, "__esModule", { value: true });
13var SideralObject_1 = require("./../SideralObject");
14/**
15 * Manager of skill of an Entity
16 */
17var SkillManager = (function (_super) {
18 __extends(SkillManager, _super);
19 function SkillManager() {
20 var _this = _super !== null && _super.apply(this, arguments) || this;
21 /**
22 * List of all skills
23 */
24 _this.skills = {};
25 /**
26 * Current skill launched
27 */
28 _this.currentSkill = null;
29 /**
30 * Last skill launched
31 */
32 _this.lastSkill = null;
33 return _this;
34 }
35 /* METHODS */
36 /**
37 * Add a new skill
38 * @param name - Name of the skill
39 * @param skill - Object instance of Skill corresponding of the name
40 * @returns The skill created
41 */
42 SkillManager.prototype.addSkill = function (name, skill) {
43 skill.name = name;
44 skill.owner = this.parent;
45 skill.signals.skillComplete.add(this._onSkillComplete.bind(this));
46 return this.skills[name] = this.add(skill);
47 };
48 /**
49 * Get a skill
50 * @param name - Name of the skill
51 * @returns The skill (or null if the skill doesn't exist)
52 */
53 SkillManager.prototype.get = function (name) {
54 return this.skills[name];
55 };
56 /**
57 * Check if the skill with the name passed in parameter is running
58 * @param name - Name of the skill
59 * @returns Know if the skill is running
60 */
61 SkillManager.prototype.isRunning = function (name) {
62 return this.currentSkill && this.currentSkill[name] === this.skills[name] && this.currentSkill.active;
63 };
64 /**
65 * Run a skill
66 * @param {string} name: name of the skill
67 * @param {Object=} props: properties to add for the run
68 * @returns {Boolean} Return true if the skill has ben launched
69 */
70 SkillManager.prototype.run = function (name, props) {
71 var skill = this.get(name);
72 if (!skill || (skill && !skill.ready) || (this.currentSkill && this.currentSkill.unstoppable)) {
73 return false;
74 }
75 if (this.currentSkill) {
76 this.currentSkill.stop();
77 }
78 this.currentSkill = skill;
79 this.currentSkill.run(props);
80 return true;
81 };
82 /**
83 * Remove a skill by its name
84 * @param {string} name: name of the skill
85 * @returns {void}
86 */
87 SkillManager.prototype.remove = function (name) {
88 delete this.skills[name];
89 };
90 /* PRIVATE */
91 /**
92 * When a skill is complete
93 * @param {Skill} skill: skill
94 * @returns {void}
95 * @private
96 */
97 SkillManager.prototype._onSkillComplete = function (skill) {
98 if (this.currentSkill.name === skill.name) {
99 this.currentSkill = null;
100 }
101 this.lastSkill = skill;
102 };
103 return SkillManager;
104}(SideralObject_1.SideralObject));
105exports.SkillManager = SkillManager;