diff --git a/Cargo.lock b/Cargo.lock index 0b5246b..73d400a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1893,6 +1893,7 @@ name = "haranae-rs" version = "0.1.0" dependencies = [ "bevy", + "rand", ] [[package]] @@ -2671,6 +2672,12 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + [[package]] name = "proc-macro-crate" version = "1.3.1" @@ -2711,6 +2718,36 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "17fd96390ed3feda12e1dfe2645ed587e0bea749e319333f104a33ff62f77a0b" +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + [[package]] name = "range-alloc" version = "0.1.3" diff --git a/Cargo.toml b/Cargo.toml index bb0033b..4bfbb06 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" [dependencies] bevy = "0.12.1" +rand = "0.8.5" [profile.dev.package."*"] -opt-level = 3 \ No newline at end of file +opt-level = 3 diff --git a/src/falling_sand/mod.rs b/src/falling_sand/mod.rs index 0f0d0c5..4594e4b 100644 --- a/src/falling_sand/mod.rs +++ b/src/falling_sand/mod.rs @@ -11,12 +11,14 @@ use bevy::{ sprite::SpriteBundle, transform::components::Transform, }; +use rand::random; pub struct FallingSandPlugin; impl Plugin for FallingSandPlugin { fn build(&self, app: &mut bevy::prelude::App) { app.add_systems(Startup, setup); + app.add_systems(Update, update_chunk_texture_system); } } @@ -34,9 +36,44 @@ fn setup(mut commands: Commands, mut images: ResMut>) { let image_handle = images.add(image); - commands.spawn(SpriteBundle { - texture: image_handle, - transform: Transform::from_translation(Vec3::new(256., 0., 0.)), - ..default() - }); + commands + .spawn(Chunk { + width: 256, + height: 256, + }) + .insert(SpriteBundle { + texture: image_handle, + transform: Transform::from_translation(Vec3::new(256., 0., 0.)), + ..default() + }); +} + +#[derive(Component)] +pub struct Chunk { + width: usize, + height: usize, +} + +pub fn update_chunk_texture_system( + mut images: ResMut>, + mut chunk: Query<(&mut Chunk, &Handle)>, +) { + // We know for now there's only one chunk + let chunk = chunk.get_single_mut(); + if chunk.is_err() { + return; + } + + let (chunk, image_handle) = chunk.unwrap(); + let image = images.get_mut(image_handle).unwrap(); + for y in 0..chunk.height { + for x in 0..chunk.width { + // Just set each pixel to random colours for now + let index = (x + y * chunk.width) * 4; + image.data[index] = random::(); + image.data[index + 1] = random::(); + image.data[index + 2] = random::(); + image.data[index + 3] = 255; + } + } }