UNPKG

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