forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinMax2.js
More file actions
194 lines (147 loc) · 3.54 KB
/
MinMax2.js
File metadata and controls
194 lines (147 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
var Between = require('./Between');
var FloatBetween = require('./FloatBetween');
var Class = require('../utils/Class');
var Percent = require('./Percent');
var Wrap = require('./Wrap');
// A Helper Class that allows you to specify a range between min and max, and then
// keep the value within those bounds, or get random ints or floats from the range.
var MinMax2 = new Class({
initialize:
function MinMax2 (min, max, steps)
{
this.min = 0;
this.max = 0;
this.steps = 0;
this._current = 0;
if (min !== undefined)
{
this.set(min, max, steps);
}
},
set: function (min, max, steps)
{
if (Array.isArray(min))
{
steps = min[2];
max = min[1];
min = min[0];
}
else if (typeof min === 'object')
{
var obj = min;
min = (obj.hasOwnProperty('x')) ? obj.x : obj.min;
max = (obj.hasOwnProperty('y')) ? obj.y : obj.max;
steps = obj.steps;
}
if (min === undefined) { min = 0; }
if (max === undefined) { max = min; }
if (steps === undefined) { steps = 0; }
this.min = min;
this.max = max;
this.steps = steps;
this._current = min;
return this;
},
clone: function ()
{
return new MinMax2(this.min, this.max, this.steps);
},
copy: function (dest)
{
dest.min = this.min;
dest.max = this.max;
dest.steps = this.steps;
return this;
},
copyXY: function (dest)
{
dest.x = this.min;
dest.y = this.max;
return this;
},
copyToMinMax: function (dest)
{
dest.min = this.min;
dest.max = this.max;
return this;
},
// Given U (a value between 0 and 1) return the value in the range
getU: function (u)
{
// TODO
},
// Returns a value between 0 and 1 based on value
getPercent: function (value)
{
return Percent(value, this.min, this.max);
},
getRandom: function ()
{
return Between(this.min, this.max);
},
getRandomFloat: function ()
{
return FloatBetween(this.min, this.max);
},
getNext: function ()
{
var value;
if (this.steps > 0)
{
value = this._current;
var i = value + ((this.max - this.min) / this.steps);
this._current = Wrap(i, this.min, this.max);
}
else
{
value = this.getRandom();
}
return value;
},
getNextFloat: function ()
{
var value;
if (this.steps > 0)
{
value = this._current;
var i = value + ((this.max - this.min) / this.steps);
this._current = Wrap(i, this.min, this.max);
}
else
{
value = this.getRandomFloat();
}
return value;
},
current: {
get: function ()
{
return this._current;
},
set: function (value)
{
this._current = Wrap(value, this.min, this.max);
}
},
x: {
get: function ()
{
return this.min;
},
set: function (value)
{
this.min = value;
}
},
y: {
get: function ()
{
return this.max;
},
set: function (value)
{
this.max = value;
}
}
});
module.exports = MinMax2;