@@ -9,7 +9,7 @@ use rand_chacha::ChaCha8Rng;
9
9
10
10
fn main ( ) {
11
11
App :: new ( )
12
- . add_plugins ( ( DefaultPlugins . set ( ImagePlugin :: default_nearest ( ) ) , ) )
12
+ . add_plugins ( DefaultPlugins . set ( ImagePlugin :: default_nearest ( ) ) )
13
13
. add_systems ( Startup , setup)
14
14
. add_systems ( Update , ( update_tileset_image, update_tilemap) )
15
15
. run ( ) ;
@@ -18,8 +18,14 @@ fn main() {
18
18
#[ derive( Component , Deref , DerefMut ) ]
19
19
struct UpdateTimer ( Timer ) ;
20
20
21
+ #[ derive( Resource , Deref , DerefMut ) ]
22
+ struct SeededRng ( ChaCha8Rng ) ;
23
+
21
24
fn setup ( mut commands : Commands , assets : Res < AssetServer > ) {
25
+ // We're seeding the PRNG here to make this example deterministic for testing purposes.
26
+ // This isn't strictly required in practical use unless you need your app to be deterministic.
22
27
let mut rng = ChaCha8Rng :: seed_from_u64 ( 42 ) ;
28
+
23
29
let chunk_size = UVec2 :: splat ( 64 ) ;
24
30
let tile_display_size = UVec2 :: splat ( 8 ) ;
25
31
let indices: Vec < Option < u16 > > = ( 0 ..chunk_size. element_product ( ) )
@@ -39,6 +45,8 @@ fn setup(mut commands: Commands, assets: Res<AssetServer>) {
39
45
) ) ;
40
46
41
47
commands. spawn ( Camera2d ) ;
48
+
49
+ commands. insert_resource ( SeededRng ( rng) ) ;
42
50
}
43
51
44
52
fn update_tileset_image (
@@ -55,12 +63,15 @@ fn update_tileset_image(
55
63
}
56
64
}
57
65
58
- fn update_tilemap ( time : Res < Time > , mut query : Query < ( & mut TilemapChunkIndices , & mut UpdateTimer ) > ) {
66
+ fn update_tilemap (
67
+ time : Res < Time > ,
68
+ mut query : Query < ( & mut TilemapChunkIndices , & mut UpdateTimer ) > ,
69
+ mut rng : ResMut < SeededRng > ,
70
+ ) {
59
71
for ( mut indices, mut timer) in query. iter_mut ( ) {
60
72
timer. tick ( time. delta ( ) ) ;
61
73
62
74
if timer. just_finished ( ) {
63
- let mut rng = ChaCha8Rng :: from_entropy ( ) ;
64
75
for _ in 0 ..50 {
65
76
let index = rng. gen_range ( 0 ..indices. len ( ) ) ;
66
77
indices[ index] = Some ( rng. gen_range ( 0 ..5 ) ) ;
0 commit comments