1 | "use strict";
|
2 | var __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 | })();
|
12 | Object.defineProperty(exports, "__esModule", { value: true });
|
13 | var SideralObject_1 = require("./../SideralObject");
|
14 | var Tool_1 = require("./../Tool");
|
15 |
|
16 |
|
17 |
|
18 | var Timer = (function (_super) {
|
19 | __extends(Timer, _super);
|
20 |
|
21 | |
22 |
|
23 |
|
24 | function Timer() {
|
25 | var _this = _super.call(this) || this;
|
26 | |
27 |
|
28 |
|
29 | _this.pause = false;
|
30 | |
31 |
|
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 |
|
39 | |
40 |
|
41 |
|
42 |
|
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 |
|
49 |
|
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 |
|
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 |
|
68 | |
69 |
|
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 |
|
98 |
|
99 | Timer.prototype.onDurationChange = function () {
|
100 | this.restart();
|
101 | if (this.props.init) {
|
102 | this.props.init();
|
103 | }
|
104 | };
|
105 |
|
106 | |
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | Timer.frameToMs = function (frame, fps) {
|
113 | return (frame / fps) * 1000;
|
114 | };
|
115 | |
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | Timer.msToFrame = function (ms, fps) {
|
122 | return (ms / 1000) * fps;
|
123 | };
|
124 | return Timer;
|
125 | }(SideralObject_1.SideralObject));
|
126 | exports.Timer = Timer;
|