From 44944fd8a7ddf229fbe4076251c81a8870f91c07 Mon Sep 17 00:00:00 2001 From: Joacim Magnusson Date: Fri, 3 Nov 2023 19:09:24 +0100 Subject: [PATCH] Add `set_voxel` example --- Cargo.toml | 4 ---- examples/set_voxel.rs | 55 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 examples/set_voxel.rs diff --git a/Cargo.toml b/Cargo.toml index 21f9329..e34b018 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,3 @@ rand = "0.8.5" [dev-dependencies] noise = "0.8.2" - -[[example]] -name = "noise_terrain" -path = "examples/noise_terrain.rs" diff --git a/examples/set_voxel.rs b/examples/set_voxel.rs new file mode 100644 index 0000000..a78a34c --- /dev/null +++ b/examples/set_voxel.rs @@ -0,0 +1,55 @@ +use bevy::prelude::*; +use bevy_voxel_world::prelude::*; +use rand::Rng; + +fn main() { + App::new() + .add_plugins(DefaultPlugins) + .add_plugins(VoxelWorldPlugin::default()) + .add_systems(Startup, setup) + .add_systems(Update, (set_solid_voxel, move_camera)) + .run(); +} + +fn setup(mut commands: Commands) { + // Camera + commands.spawn(( + Camera3dBundle { + transform: Transform::from_xyz(20.0, 20.0, 20.0).looking_at(Vec3::ZERO, Vec3::Y), + ..default() + }, + // This tells bevy_voxel_world tos use this cameras transform to calculate spawning area + VoxelWorldCamera, + )); + + // Ambient light + commands.insert_resource(AmbientLight { + color: Color::rgb(0.98, 0.95, 0.82), + brightness: 1.0, + }); +} + +fn set_solid_voxel(mut voxel_world: VoxelWorld) { + // Gnerate some random values + let size = 10; + let mut rng = rand::thread_rng(); + let x = rng.gen_range(-size..size); + let y = rng.gen_range(-size..size); + let z = rng.gen_range(-size..size); + let voxel_type = rng.gen_range(0..4); + let pos = IVec3::new(x, y, z); + + // Set a voxel at the random position with the random type + if pos.distance_squared(IVec3::ZERO) < i32::pow(size, 2) { + voxel_world.set_voxel(pos, WorldVoxel::Solid(voxel_type)); + } +} + +// animate camera back and forth +fn move_camera(time: Res