UNPKG

4.15 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 Tool_1 = require("./../Tool");
15/**
16 * The timer object (replace window.setTimeout and window.setInterval)
17 */
18var Timer = (function (_super) {
19 __extends(Timer, _super);
20 /* LIFECYCLE */
21 /**
22 * @constructor
23 */
24 function Timer() {
25 var _this = _super.call(this) || this;
26 /**
27 * Know if the timer is paused
28 */
29 _this.pause = false;
30 /**
31 * Know if the timer is finished
32 */
33 _this.finished = false;
34 _this.signals.propChange.bind("duration", _this.onDurationChange.bind(_this));
35 _this.signals.update.add(_this.updateTimer.bind(_this));
36 return _this;
37 }
38 /* METHODS */
39 /**
40 * Get value with ration (0 to 1)
41 * @param reversed - To 0 to 1 or to 1 to 0
42 * @returns The value rationed
43 */
44 Timer.prototype.getValueRationed = function (reversed) {
45 return Tool_1.Util.limit(reversed ? (this.props.duration - this.value) / this.props.duration : this.value / this.props.duration, 0, 1);
46 };
47 /**
48 * Stop the timer
49 * @param bypassComplete - if true, the event complete won't be fired
50 */
51 Timer.prototype.stop = function (bypassComplete) {
52 if (bypassComplete === void 0) { bypassComplete = false; }
53 this.finished = true;
54 if (this.props.complete && !bypassComplete) {
55 this.props.complete();
56 }
57 };
58 /**
59 * Restart the timer with it default value
60 */
61 Timer.prototype.restart = function () {
62 this.value = this.props.duration;
63 this.tendance = this.props.duration < 0 ? 1 : -1;
64 this.pause = false;
65 this.finished = false;
66 };
67 /* EVENTS */
68 /**
69 * update the timer
70 */
71 Timer.prototype.updateTimer = function (tick) {
72 if (this.pause || this.finished || isNaN(this.value)) {
73 return null;
74 }
75 this.value = this.value + (tick * this.tendance * 1000);
76 var finished = this.value <= 0 || (Math.abs(this.value) >= Math.abs(this.props.duration));
77 if (this.props.update) {
78 this.props.update(tick, this.props.duration - this.value, this.getValueRationed(true), this.props.duration);
79 }
80 if (finished && this.props.recurrence) {
81 this.props.recurrence--;
82 if (this.props.reversible) {
83 this.tendance = -this.tendance;
84 }
85 else {
86 this.value = this.props.duration;
87 }
88 if (this.props.complete) {
89 this.props.complete();
90 }
91 }
92 else if (finished && !this.props.recurrence) {
93 this.stop();
94 }
95 };
96 /**
97 * When "duration" property has changed
98 */
99 Timer.prototype.onDurationChange = function () {
100 this.restart();
101 if (this.props.init) {
102 this.props.init();
103 }
104 };
105 /* STATICS */
106 /**
107 * Convert frame to ms
108 * @param frame - Number of frame
109 * @param fps - Current fps (provided by the Game Object)
110 * @returns number of ms
111 */
112 Timer.frameToMs = function (frame, fps) {
113 return (frame / fps) * 1000;
114 };
115 /**
116 * Convert ms to frame
117 * @param ms - Number of milliseconds
118 * @param fps - Current fps (provided by the Game object)
119 * @returns Number of frames
120 */
121 Timer.msToFrame = function (ms, fps) {
122 return (ms / 1000) * fps;
123 };
124 return Timer;
125}(SideralObject_1.SideralObject));
126exports.Timer = Timer;