Set falling sand chunk texure to random colours each frame

This commit is contained in:
Jarrod Doyle 2024-01-26 17:43:13 +00:00
parent d40e05da3a
commit 9e103c0c05
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
3 changed files with 81 additions and 6 deletions

37
Cargo.lock generated
View File

@ -1893,6 +1893,7 @@ name = "haranae-rs"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"bevy", "bevy",
"rand",
] ]
[[package]] [[package]]
@ -2671,6 +2672,12 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "ppv-lite86"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]] [[package]]
name = "proc-macro-crate" name = "proc-macro-crate"
version = "1.3.1" version = "1.3.1"
@ -2711,6 +2718,36 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17fd96390ed3feda12e1dfe2645ed587e0bea749e319333f104a33ff62f77a0b" 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]] [[package]]
name = "range-alloc" name = "range-alloc"
version = "0.1.3" version = "0.1.3"

View File

@ -7,6 +7,7 @@ edition = "2021"
[dependencies] [dependencies]
bevy = "0.12.1" bevy = "0.12.1"
rand = "0.8.5"
[profile.dev.package."*"] [profile.dev.package."*"]
opt-level = 3 opt-level = 3

View File

@ -11,12 +11,14 @@ use bevy::{
sprite::SpriteBundle, sprite::SpriteBundle,
transform::components::Transform, transform::components::Transform,
}; };
use rand::random;
pub struct FallingSandPlugin; pub struct FallingSandPlugin;
impl Plugin for FallingSandPlugin { impl Plugin for FallingSandPlugin {
fn build(&self, app: &mut bevy::prelude::App) { fn build(&self, app: &mut bevy::prelude::App) {
app.add_systems(Startup, setup); 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<Assets<Image>>) {
let image_handle = images.add(image); let image_handle = images.add(image);
commands.spawn(SpriteBundle { commands
.spawn(Chunk {
width: 256,
height: 256,
})
.insert(SpriteBundle {
texture: image_handle, texture: image_handle,
transform: Transform::from_translation(Vec3::new(256., 0., 0.)), transform: Transform::from_translation(Vec3::new(256., 0., 0.)),
..default() ..default()
}); });
} }
#[derive(Component)]
pub struct Chunk {
width: usize,
height: usize,
}
pub fn update_chunk_texture_system(
mut images: ResMut<Assets<Image>>,
mut chunk: Query<(&mut Chunk, &Handle<Image>)>,
) {
// 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::<u8>();
image.data[index + 1] = random::<u8>();
image.data[index + 2] = random::<u8>();
image.data[index + 3] = 255;
}
}
}