Skip to content

Commit

Permalink
Add set_voxel example
Browse files Browse the repository at this point in the history
  • Loading branch information
splashdust committed Nov 3, 2023
1 parent cd38ebc commit 44944fd
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,3 @@ rand = "0.8.5"

[dev-dependencies]
noise = "0.8.2"

[[example]]
name = "noise_terrain"
path = "examples/noise_terrain.rs"
55 changes: 55 additions & 0 deletions examples/set_voxel.rs
Original file line number Diff line number Diff line change
@@ -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<Time>, mut query: Query<&mut Transform, With<VoxelWorldCamera>>) {
let mut transform = query.single_mut();
let time_seconds = time.elapsed_seconds();
transform.translation.x = 25.0 * (time_seconds * 0.1).sin();
transform.translation.z = 25.0 * (time_seconds * 0.1).cos();
transform.look_at(Vec3::ZERO, Vec3::Y);
}

0 comments on commit 44944fd

Please sign in to comment.