UNPKG

6.63 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");
14var SideralObject_2 = require("./../SideralObject");
15var Tool_1 = require("./../Tool");
16/**
17 * The skill algorithm
18 */
19var Skill = (function (_super) {
20 __extends(Skill, _super);
21 /* LIFECYCLE */
22 /**
23 * @constructor
24 */
25 function Skill() {
26 var _this = _super.call(this) || this;
27 /**
28 * Props of the Hitbox Class
29 */
30 _this.hitboxProps = {};
31 /**
32 * Duration of the skill
33 */
34 _this.duration = 0;
35 /**
36 * Type of duration
37 */
38 _this.durationType = Tool_1.Enum.DURATION_TYPE.MS;
39 /**
40 * Duration of the preparation
41 */
42 _this.preparation = 0;
43 /**
44 * Type of duration for the preparation
45 */
46 _this.preparationType = Tool_1.Enum.DURATION_TYPE.MS;
47 /**
48 * Duration of the reload
49 */
50 _this.reload = 0;
51 /**
52 * Type of duration for the reload
53 */
54 _this.reloadType = Tool_1.Enum.DURATION_TYPE.MS;
55 /**
56 * If true, the skill cannot be stopable
57 */
58 _this.unstoppable = true;
59 /**
60 * If true, the owner willnot move during the skill
61 */
62 _this.movable = true;
63 /**
64 * Know if the skill is currently active or not
65 * @readonly
66 */
67 _this.active = false;
68 /**
69 * Know if the skill is ready to launch
70 * @readonly
71 */
72 _this.ready = true;
73 _this.timers = _this.add(new SideralObject_2.TimerManager());
74 _this.signals.preparationStart = new Tool_1.SignalEvent();
75 _this.signals.preparationUpdate = new Tool_1.SignalEvent();
76 _this.signals.preparationComplete = new Tool_1.SignalEvent();
77 _this.signals.skillStart = new Tool_1.SignalEvent();
78 _this.signals.skillUpdate = new Tool_1.SignalEvent();
79 _this.signals.skillComplete = new Tool_1.SignalEvent();
80 _this.signals.reloadStart = new Tool_1.SignalEvent();
81 _this.signals.reloadUpdate = new Tool_1.SignalEvent();
82 _this.signals.reloadComplete = new Tool_1.SignalEvent();
83 return _this;
84 }
85 /* METHODS */
86 /**
87 * Instanciate a hitbox item during the time of the skill
88 * @param hitbox - The hitbox to add
89 * @param hitboxProps - The props to transmit to the hitbox instance
90 * @returns The hitbox instance
91 */
92 Skill.prototype.addHitbox = function (hitbox, hitboxProps) {
93 if (hitboxProps === void 0) { hitboxProps = {}; }
94 var x = typeof hitboxProps.x === "undefined" ? this.owner.props.x : hitboxProps.x, y = typeof hitboxProps.y === "undefined" ? this.owner.props.y : hitboxProps.y;
95 delete hitboxProps.x;
96 delete hitboxProps.y;
97 hitboxProps.owner = this.owner;
98 return this.owner.context.scene.spawn(hitbox, x, y, Object.assign({}, hitboxProps));
99 };
100 /**
101 * Run the skill
102 * @param params - Change attributes for the run
103 */
104 Skill.prototype.run = function (params) {
105 var _this = this;
106 if (params === void 0) { params = {}; }
107 var startSkill = function () {
108 Object.assign(_this, params);
109 _this.signals.preparationComplete.dispatch();
110 _this.timers.addTimer("skill", _this.timers.getDuration(_this.duration, _this.durationType, _this.animation && _this.owner.sprite.getAnimation(_this.animation)), _this._onSkillComplete.bind(_this), {
111 init: _this.signals.skillStart.dispatch.bind(_this),
112 update: _this.updateSkill.bind(_this)
113 });
114 if (_this.animation) {
115 try {
116 _this.owner.sprite.setAnimation(_this.animation, true);
117 }
118 catch (e) {
119 throw Error(e);
120 }
121 }
122 if (_this.hitboxClass) {
123 _this.hitbox = _this.addHitbox(new (_this.hitboxClass)(), _this.hitboxProps);
124 }
125 };
126 this.active = true;
127 this.ready = !this.unstoppable;
128 if (this.preparation) {
129 this.timers.addTimer("preparation", this.timers.getDuration(this.preparation, this.preparationType), startSkill.bind(this), {
130 init: this.signals.preparationStart.dispatch.bind(this),
131 update: this.signals.preparationUpdate.dispatch.bind(this)
132 });
133 }
134 else {
135 startSkill();
136 }
137 };
138 /**
139 * Stop the skill
140 */
141 Skill.prototype.stop = function () {
142 this.active = false;
143 this.timers.removeAllTimer();
144 };
145 /* EVENTS */
146 /**
147 * Skill update
148 * @param tick - The current tick of the timer
149 * @param value - The current value of the timer
150 * @param ratio - The ratio of the Timer (from 0 to 1)
151 * @param duration - The total duration of the timer
152 */
153 Skill.prototype.updateSkill = function (tick, value, ratio, duration) {
154 if (!this.movable) {
155 this.owner.props.vy = this.owner.props.accelY = 0;
156 this.owner.props.vx = this.owner.props.accelX = 0;
157 }
158 this.signals.skillUpdate.dispatch(tick, value, ratio, duration);
159 };
160 /**
161 * When the skill is complete
162 * @private
163 */
164 Skill.prototype._onSkillComplete = function () {
165 var _this = this;
166 this.signals.skillComplete.dispatch(this);
167 if (this.reload) {
168 this.timers.addTimer("reload", this.timers.getDuration(this.reload, this.reloadType), function () {
169 _this.ready = true;
170 _this.signals.reloadComplete.dispatch;
171 }, {
172 update: this.signals.reloadUpdate.dispatch.bind(this)
173 });
174 }
175 this.ready = !this.reload;
176 if (this.hitbox) {
177 this.hitbox.kill();
178 this.hitbox = null;
179 }
180 };
181 return Skill;
182}(SideralObject_1.SideralObject));
183exports.Skill = Skill;