|
18 | 18 | * |
19 | 19 | * @class Phaser.RandomDataGenerator |
20 | 20 | * @constructor |
21 | | -* @param {any[]|String} [seeds] - An array of values to use as the seed or a generator state (from {#state}). |
| 21 | +* @param {any[]|string} [seeds] - An array of values to use as the seed, or a generator state (from {#state}). |
22 | 22 | */ |
23 | 23 | Phaser.RandomDataGenerator = function (seeds) { |
24 | 24 |
|
@@ -48,9 +48,12 @@ Phaser.RandomDataGenerator = function (seeds) { |
48 | 48 | */ |
49 | 49 | this.s2 = 0; |
50 | 50 |
|
51 | | - if((typeof seeds === 'string' || seeds instanceof String) && seeds.match(/^!rnd/)){ |
| 51 | + if (typeof seeds === 'string') |
| 52 | + { |
52 | 53 | this.state(seeds); |
53 | | - }else{ |
| 54 | + } |
| 55 | + else |
| 56 | + { |
54 | 57 | this.sow(seeds); |
55 | 58 | } |
56 | 59 |
|
@@ -305,21 +308,34 @@ Phaser.RandomDataGenerator.prototype = { |
305 | 308 | }, |
306 | 309 |
|
307 | 310 | /** |
308 | | - * Sets or gets state of the generator |
| 311 | + * Gets or Sets the state of the generator. This allows you to retain the values |
| 312 | + * that the generator is using between games, i.e. in a game save file. |
| 313 | + * |
| 314 | + * To seed this generator with a previously saved state you can pass it as the |
| 315 | + * `seed` value in your game config, or call this method directly after Phaser has booted. |
| 316 | + * |
| 317 | + * Call this method with no parameters to return the current state. |
| 318 | + * |
| 319 | + * If providing a state it should match the same format that this method |
| 320 | + * returns, which is a string with a header `!rnd` followed by the `c`, |
| 321 | + * `s0`, `s1` and `s2` values respectively, each comma-delimited. |
309 | 322 | * |
310 | 323 | * @method Phaser.RandomDataGenerator#state |
311 | | - * @param {String} [state] Generator state to be set. |
312 | | - * @return {String} Actual generator state. |
| 324 | + * @param {string} [state] - Generator state to be set. |
| 325 | + * @return {string} The current state of the generator. |
313 | 326 | */ |
314 | 327 | state: function (state) { |
315 | 328 |
|
316 | | - if(state){ |
| 329 | + if (typeof state === 'string' && seeds.match(/^!rnd/)) |
| 330 | + { |
317 | 331 | state = state.split(','); |
| 332 | + |
318 | 333 | this.c = parseFloat(state[1]); |
319 | 334 | this.s0 = parseFloat(state[2]); |
320 | 335 | this.s1 = parseFloat(state[3]); |
321 | 336 | this.s2 = parseFloat(state[4]); |
322 | 337 | } |
| 338 | + |
323 | 339 | return ['!rnd', this.c, this.s0, this.s1, this.s2].join(','); |
324 | 340 |
|
325 | 341 | } |
|
0 commit comments