Skip to content

Commit 63f5df9

Browse files
committed
Feature: Allow random generator to get/set state
1 parent 37fc327 commit 63f5df9

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

src/math/RandomDataGenerator.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* @class Phaser.RandomDataGenerator
2020
* @constructor
21-
* @param {any[]} [seeds] - An array of values to use as the seed.
21+
* @param {any[]|String} [seeds] - An array of values to use as the seed or a generator state (from {#state}).
2222
*/
2323
Phaser.RandomDataGenerator = function (seeds) {
2424

@@ -48,7 +48,11 @@ Phaser.RandomDataGenerator = function (seeds) {
4848
*/
4949
this.s2 = 0;
5050

51-
this.sow(seeds);
51+
if((typeof seeds === 'string' || seeds instanceof String) && seeds.match(/^!rnd/)){
52+
this.state(seeds);
53+
}else{
54+
this.sow(seeds);
55+
}
5256

5357
};
5458

@@ -298,6 +302,26 @@ Phaser.RandomDataGenerator.prototype = {
298302

299303
return this.integerInRange(-180, 180);
300304

305+
},
306+
307+
/**
308+
* Sets or gets state of the generator
309+
*
310+
* @method Phaser.RandomDataGenerator#state
311+
* @param {String} [state] Generator state to be set.
312+
* @return {String} Actual generator state.
313+
*/
314+
state: function (state) {
315+
316+
if(state){
317+
state = state.split(',');
318+
this.c = parseFloat(state[1]);
319+
this.s0 = parseFloat(state[2]);
320+
this.s1 = parseFloat(state[3]);
321+
this.s2 = parseFloat(state[4]);
322+
}
323+
return ['!rnd', this.c, this.s0, this.s1, this.s2].join(',');
324+
301325
}
302326

303327
};

0 commit comments

Comments
 (0)