UNPKG

4.31 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})();
12var __assign = (this && this.__assign) || Object.assign || function(t) {
13 for (var s, i = 1, n = arguments.length; i < n; i++) {
14 s = arguments[i];
15 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
16 t[p] = s[p];
17 }
18 return t;
19};
20Object.defineProperty(exports, "__esModule", { value: true });
21var SideralObject_1 = require("./../SideralObject");
22var Tool_1 = require("./../Tool");
23var Timer_1 = require("./Timer");
24/**
25 * A manager for all timers
26 */
27var TimerManager = (function (_super) {
28 __extends(TimerManager, _super);
29 function TimerManager() {
30 /* ATTRIBUTES */
31 var _this = _super !== null && _super.apply(this, arguments) || this;
32 /**
33 * List of all current timers
34 */
35 _this.timers = {};
36 return _this;
37 }
38 /* METHODS */
39 /**
40 * Add a new timer
41 * @param name: Name of the timer
42 * @param duration: Duration of the timer in ms
43 * @param onComplete: Callback function when finished
44 * @param options: Options to implement to the timer
45 * @returns Rhe timer created
46 */
47 TimerManager.prototype.addTimer = function (name, duration, onComplete, options) {
48 if (options === void 0) { options = {}; }
49 var timer = this.add(new Timer_1.Timer(), __assign({ duration: duration, complete: onComplete }, options));
50 timer.name = name;
51 var oldTimer = this.timers[name];
52 if (oldTimer) {
53 oldTimer.kill();
54 }
55 return this.timers[name] = timer;
56 };
57 /**
58 * Get a timer by its name
59 * @param {string} name: name of the timer
60 * @returns {Timer} The timer
61 */
62 TimerManager.prototype.getTimer = function (name) {
63 return this.timers[name];
64 };
65 /**
66 * Get a duration by its type
67 * @param duration - The duration
68 * @param type - The type of duration
69 * @param animation - The sprite animation used when you use Enum.DURATION_TYPE.ANIMATION_LOOP
70 * @returns The duration converted by its type
71 */
72 TimerManager.prototype.getDuration = function (duration, type, animation) {
73 var ms = duration;
74 switch (type) {
75 case Tool_1.Enum.DURATION_TYPE.ANIMATION_LOOP:
76 if (!animation) {
77 throw new Error("Timers.getDuration: You must provide an 'animation' object when you use Enum.DURATION_TYPE.ANIMATION_LOOP");
78 }
79 ms = animation.duration * duration * animation.frames.length;
80 break;
81 case Tool_1.Enum.DURATION_TYPE.FRAME:
82 ms = Timer_1.Timer.frameToMs(duration, this.context.game.fps);
83 break;
84 }
85 return ms;
86 };
87 /**
88 * Remove a timer by its name
89 * @param {string} name: name of the timer
90 * @returns {void}
91 */
92 TimerManager.prototype.removeTimer = function (name) {
93 var timer = this.timers[name];
94 if (timer) {
95 timer.kill();
96 }
97 delete this.timers[name];
98 return this;
99 };
100 /**
101 * Remove all timers
102 */
103 TimerManager.prototype.removeAllTimer = function () {
104 var _this = this;
105 Object.keys(this.timers).forEach(function (key) { return _this.timers[key] && _this.timers[key].kill(); });
106 this.timers = {};
107 return this;
108 };
109 /**
110 * Check if a timer is finished or not
111 * @acess public
112 * @param {string} name - The name of the timer
113 * @returns {boolean} The timer is finished ?
114 */
115 TimerManager.prototype.isFinished = function (name) {
116 var timer = this.timers[name];
117 return timer ? timer.finished : true;
118 };
119 return TimerManager;
120}(SideralObject_1.SideralObject));
121exports.TimerManager = TimerManager;