diff --git a/src/lib.rs b/src/lib.rs index e1dc1be..d55386c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -100,13 +100,15 @@ pub fn main() { // (event_loop, window) // }; - use bevy::DefaultPlugins; + use bevy::{diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, DefaultPlugins}; App::new() .add_plugins(DefaultPlugins) .add_plugins(( camera::Plugin, - map::Plugin + map::Plugin, + FrameTimeDiagnosticsPlugin, + LogDiagnosticsPlugin::default() )) .run(); } diff --git a/src/map.rs b/src/map.rs index 6d42c5a..9e68047 100644 --- a/src/map.rs +++ b/src/map.rs @@ -7,6 +7,7 @@ pub struct Plugin; impl bevy::prelude::Plugin for Plugin { fn build(&self, app: &mut App) { app.add_systems(Startup, setup) + .add_systems(Update, update) .insert_resource(ClearColor(Color::srgb(0., 0., 1.))); } } @@ -19,9 +20,15 @@ pub const CELL_AREA: f32 = REAL_HEIGHT * REAL_WIDTH / SIZE as f32; pub const SIZE: usize = 10000; pub const seed: u32 = 0; -#[derive(Resource)] +#[derive(Component)] struct Voronoi (voronoice::Voronoi); +#[derive(Component)] +struct MapMarker; + +#[derive(Component)] +struct MapColors (Vec<[f32; 4]>); + fn setup( mut cmds: Commands, mut meshes: ResMut>, @@ -62,7 +69,7 @@ fn setup( } else { CellKind::Stone }; - cells_data.push(CellData::new(k, i, z as f32, m, 1.)); + cells_data.push(CellData::new(k, i, z as f32, m, 1., vec![])); } @@ -70,7 +77,7 @@ fn setup( let mut colors = Vec::new(); let mut indices = Vec::new(); - for (c, cd) in voronoi.iter_cells().zip(cells_data.iter()).filter(|(_,cd)| cd.kind != CellKind::Forest) { + for (c, cd) in voronoi.iter_cells().zip(cells_data.iter_mut()).filter(|(_,cd)| cd.kind != CellKind::Forest) { let mut color = cd.color(); // if c.site() == selected_tile { // color[0] = (color[0]+0.4).clamp(0., 1.); @@ -78,16 +85,15 @@ fn setup( // color[2] = (color[2]+0.4).clamp(0., 1.); // } let vs = c.iter_vertices().collect::>(); - let i = poss.len() as u32; + let i = poss.len(); for v in vs.iter() { poss.push(Vec3::new(v.x as f32, v.y as f32, 0.));// [v.x as f32, v.y as f32, 0.]); // poss.push(Vertex::new_col([v.x as f32, v.y as f32], color, 1)); colors.push(color); } - for v in 1..(vs.len()-1) as u32 { - indices.push(i); - indices.push(i+v); - indices.push(i+v+1); + for v in 1..(vs.len()-1) { + indices.extend_from_slice(&[i as u32, (i+v) as u32, (i+v+1) as u32]); + cd.vertices.extend_from_slice(&[i, i+v, i+v+1]); } } @@ -100,17 +106,36 @@ fn setup( ) .with_inserted_attribute( Mesh::ATTRIBUTE_COLOR, - colors + colors.clone() ) .with_inserted_indices(Indices::U32(indices)); cmds.spawn(( Mesh2d(meshes.add(mesh)), MeshMaterial2d(materials.add(ColorMaterial::default())), - Transform::default() + Transform::default(), + Voronoi(voronoi), + MapColors(colors), + MapMarker )); +} - cmds.insert_resource(Voronoi(voronoi)); +fn update( + cells: Query<&CellData>, + mut map: Query<(&Mesh2d, &mut MapColors), With>, + mut meshes: ResMut> +) { + let (mesh, mut cols) = map.single_mut(); + if let Some(mesh) = meshes.get_mut(mesh) { + // let cols = mesh.attribute_mut(Mesh::ATTRIBUTE_COLOR).unwrap(); + for cd in cells.iter() { + let col = cd.color(); + for id in cd.vertices.iter() { + cols.0[*id] = col.clone(); + } + } + mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, cols.0.clone()); + } } @@ -130,17 +155,19 @@ pub struct CellData { pub kind: CellKind, pub cid: usize, z: f32, - pub moisture: f32, - pub resource: f32 // How much resource there is (between 0 and 1) + pub moisture: f32, + pub resource: f32, // How much resource there is (between 0 and 1) + pub vertices: Vec } impl CellData { - pub fn new(kind: CellKind, cell: usize, z: f32, moisture: f32, resource: f32) -> Self { + pub fn new(kind: CellKind, cell: usize, z: f32, moisture: f32, resource: f32, vertices: Vec) -> Self { Self { kind, cid: cell, z, moisture, - resource + resource, + vertices } } // pub fn pos<'a>(&self, map: &'a Map) -> &'a Point {