#![allow(dead_code)]
use bevy::prelude::*;
use bevy_bones3::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(Bones3CorePlugin::<BlockState>::default())
.add_systems(Startup, init)
.add_systems(Update, update)
.run();
}
#[derive(Debug, Default, Reflect, Clone, Copy)]
struct BlockState {
pub furniture: FurnitureValue,
pub air_density: f32,
pub light_value: i32,
}
#[derive(Debug, Default, Reflect, Clone, Copy)]
enum FurnitureValue {
#[default]
None,
Chair,
Table,
Stool,
}
#[derive(Component)]
struct LightSource {
pub world_id: Entity,
pub pos: IVec3,
}
fn init(mut commands: VoxelCommands) {
let mut world_cmd = commands.spawn_world(());
let world_id = world_cmd.id();
let mut voxel_storage = VoxelStorage::<BlockState>::default();
voxel_storage.set_block(IVec3::new(12, 6, 2), BlockState {
furniture: FurnitureValue::Chair,
air_density: 1.2,
light_value: 13,
});
world_cmd
.spawn_chunk(IVec3::new(1, 2, 3), voxel_storage)
.unwrap();
commands.commands().spawn(LightSource {
world_id,
pos: IVec3::new(1, 1, 1),
});
}
fn update(
light_sources: Query<&LightSource>,
mut query: VoxelQuery<&mut VoxelStorage<BlockState>>,
) {
for mut storage in query.iter_mut() {
let pos = IVec3::new(
rand::random::<i32>() % 16,
rand::random::<i32>() % 16,
rand::random::<i32>() % 16,
);
let mut state = storage.get_block(pos);
state.air_density = rand::random::<f32>();
storage.set_block(pos, state);
}
for light_source in light_sources.iter() {
let mut world = query.get_world_mut(light_source.world_id).unwrap();
let chunk = world.get_chunk_at_block_mut(light_source.pos);
if let Some(mut storage) = chunk {
let mut state = storage.get_block(light_source.pos);
state.light_value += 1;
storage.set_block(light_source.pos, state);
}
}
}