feat: redone visualization to be human-generated
This commit is contained in:
parent
9edb79e970
commit
5b4ad9a47a
9 changed files with 214 additions and 24 deletions
142
src/view/game.rs
Normal file
142
src/view/game.rs
Normal file
|
|
@ -0,0 +1,142 @@
|
|||
use bevy::prelude::*;
|
||||
use crate::grid::Grid;
|
||||
use crate::MAX_FLUID_AMOUNT;
|
||||
|
||||
const FLUID_FULLNES_STAGES: usize = 7;
|
||||
|
||||
pub struct GameViewPlugin;
|
||||
|
||||
impl Plugin for GameViewPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(PreStartup, debug_setup_grid)
|
||||
.init_resource::<CubeStages>()
|
||||
.init_resource::<TileMaterials>()
|
||||
.add_systems(Startup, (setup_view_cubes, scene.spawn()))
|
||||
.add_systems(Update, update_cubes);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Resource, Default)]
|
||||
pub struct CubeStages(pub Vec<Handle<Mesh>>);
|
||||
|
||||
#[derive(Component, FromTemplate)]
|
||||
pub struct TileWatch(pub usize);
|
||||
|
||||
#[derive(Resource, Default)]
|
||||
pub struct TileMaterials {
|
||||
pub solid: Handle<StandardMaterial>,
|
||||
pub water: Handle<StandardMaterial>,
|
||||
}
|
||||
|
||||
pub fn debug_setup_grid(mut grid: ResMut<Grid>) {
|
||||
use crate::grid::GridElement;
|
||||
let mut tool = grid.manipulate();
|
||||
tool.fill_xyz(5, 0, 0, 5, 0, 14, GridElement::Solid);
|
||||
tool.fill_xyz(0, 0, 0, 4, 0, 15, GridElement::Water { amount: MAX_FLUID_AMOUNT });
|
||||
}
|
||||
|
||||
pub fn setup_view_cubes(
|
||||
mut commands: Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||
mut stages: ResMut<CubeStages>,
|
||||
mut tile_materials: ResMut<TileMaterials>,
|
||||
grid: Res<Grid>) {
|
||||
for i in 0..FLUID_FULLNES_STAGES {
|
||||
let tile_height = ((i+1) as f32)/(FLUID_FULLNES_STAGES as f32);
|
||||
let mesh = meshes.add(Cuboid::new(1., tile_height, 1.));
|
||||
stages.0.push(mesh);
|
||||
}
|
||||
|
||||
tile_materials.water = materials.add(StandardMaterial {
|
||||
base_color: Color::hsl(172.,0.502,0.571),
|
||||
..default()
|
||||
});
|
||||
|
||||
tile_materials.solid = materials.add(StandardMaterial {
|
||||
base_color: Color::hsl(268., 0.778, 0.499),
|
||||
..default()
|
||||
});
|
||||
|
||||
let mesh = stages.0[FLUID_FULLNES_STAGES-1].clone();
|
||||
|
||||
for i in 0..grid.cell_amount() {
|
||||
commands.spawn_scene(
|
||||
cube(
|
||||
grid.deindexify(i as u32).as_vec3(),
|
||||
i as usize,
|
||||
mesh.clone()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn scene() -> impl SceneList {
|
||||
bsn_list![
|
||||
camera(),
|
||||
sun()
|
||||
]
|
||||
}
|
||||
|
||||
pub fn camera() -> impl Scene {
|
||||
bsn! {
|
||||
Camera3d
|
||||
template(
|
||||
|ctx| {
|
||||
let grid = ctx.resource::<Grid>();
|
||||
let center = grid.size.as_vec3() * 0.5;
|
||||
Ok(Transform::from_xyz(
|
||||
center.x + grid.size.x as f32 * 1.5,
|
||||
grid.size.y as f32 * 1.5,
|
||||
center.z + grid.size.z as f32 * 1.5).looking_at(center, Vec3::Y))
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sun() -> impl Scene {
|
||||
bsn!{
|
||||
DirectionalLight {
|
||||
illuminance: 8_000.0,
|
||||
shadow_maps_enabled: false,
|
||||
}
|
||||
template_value(Transform::from_xyz(10.0, 30.0, 10.0).looking_at(Vec3::ZERO, Vec3::Y))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn cube(position: Vec3,index: usize,mesh: Handle<Mesh>) -> impl Scene {
|
||||
bsn! {
|
||||
Mesh3d(mesh)
|
||||
MeshMaterial3d<StandardMaterial>
|
||||
Transform::from_translation(position)
|
||||
TileWatch(index)
|
||||
Visibility::Hidden
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_cubes(query: Query<(&mut Mesh3d,&mut Visibility,&mut MeshMaterial3d<StandardMaterial>,&TileWatch)>, materials: Res<TileMaterials>, stages: Res<CubeStages>, grid: Res<Grid>) {
|
||||
if grid.is_changed() == false { return; }
|
||||
|
||||
for (mut mesh,mut visibility, mut material, index) in query {
|
||||
let state = grid.data[index.0];
|
||||
|
||||
match state {
|
||||
crate::grid::GridElement::Air => {
|
||||
*visibility = Visibility::Hidden;
|
||||
},
|
||||
crate::grid::GridElement::Water { amount } => {
|
||||
*visibility = Visibility::Visible;
|
||||
material.0 = materials.water.clone();
|
||||
let stage = ((amount as f32)/(MAX_FLUID_AMOUNT as f32) * (FLUID_FULLNES_STAGES-1) as f32).round() as usize;
|
||||
mesh.0 = stages.0[stage].clone();
|
||||
},
|
||||
crate::grid::GridElement::Solid => {
|
||||
*visibility = Visibility::Visible;
|
||||
material.0 = materials.solid.clone();
|
||||
mesh.0 = stages.0[FLUID_FULLNES_STAGES-1].clone();
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue