update map colors

This commit is contained in:
Arkitu 2024-12-22 22:02:45 +01:00
parent 072521144b
commit 2d793f5f6c
2 changed files with 46 additions and 17 deletions

View File

@ -100,13 +100,15 @@ pub fn main() {
// (event_loop, window) // (event_loop, window)
// }; // };
use bevy::DefaultPlugins; use bevy::{diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, DefaultPlugins};
App::new() App::new()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_plugins(( .add_plugins((
camera::Plugin, camera::Plugin,
map::Plugin map::Plugin,
FrameTimeDiagnosticsPlugin,
LogDiagnosticsPlugin::default()
)) ))
.run(); .run();
} }

View File

@ -7,6 +7,7 @@ pub struct Plugin;
impl bevy::prelude::Plugin for Plugin { impl bevy::prelude::Plugin for Plugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(Startup, setup) app.add_systems(Startup, setup)
.add_systems(Update, update)
.insert_resource(ClearColor(Color::srgb(0., 0., 1.))); .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 SIZE: usize = 10000;
pub const seed: u32 = 0; pub const seed: u32 = 0;
#[derive(Resource)] #[derive(Component)]
struct Voronoi (voronoice::Voronoi); struct Voronoi (voronoice::Voronoi);
#[derive(Component)]
struct MapMarker;
#[derive(Component)]
struct MapColors (Vec<[f32; 4]>);
fn setup( fn setup(
mut cmds: Commands, mut cmds: Commands,
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
@ -62,7 +69,7 @@ fn setup(
} else { } else {
CellKind::Stone 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 colors = Vec::new();
let mut indices = 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(); let mut color = cd.color();
// if c.site() == selected_tile { // if c.site() == selected_tile {
// color[0] = (color[0]+0.4).clamp(0., 1.); // color[0] = (color[0]+0.4).clamp(0., 1.);
@ -78,16 +85,15 @@ fn setup(
// color[2] = (color[2]+0.4).clamp(0., 1.); // color[2] = (color[2]+0.4).clamp(0., 1.);
// } // }
let vs = c.iter_vertices().collect::<Vec<_>>(); let vs = c.iter_vertices().collect::<Vec<_>>();
let i = poss.len() as u32; let i = poss.len();
for v in vs.iter() { 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(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)); // poss.push(Vertex::new_col([v.x as f32, v.y as f32], color, 1));
colors.push(color); colors.push(color);
} }
for v in 1..(vs.len()-1) as u32 { for v in 1..(vs.len()-1) {
indices.push(i); indices.extend_from_slice(&[i as u32, (i+v) as u32, (i+v+1) as u32]);
indices.push(i+v); cd.vertices.extend_from_slice(&[i, i+v, i+v+1]);
indices.push(i+v+1);
} }
} }
@ -100,17 +106,36 @@ fn setup(
) )
.with_inserted_attribute( .with_inserted_attribute(
Mesh::ATTRIBUTE_COLOR, Mesh::ATTRIBUTE_COLOR,
colors colors.clone()
) )
.with_inserted_indices(Indices::U32(indices)); .with_inserted_indices(Indices::U32(indices));
cmds.spawn(( cmds.spawn((
Mesh2d(meshes.add(mesh)), Mesh2d(meshes.add(mesh)),
MeshMaterial2d(materials.add(ColorMaterial::default())), 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<MapMarker>>,
mut meshes: ResMut<Assets<Mesh>>
) {
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 kind: CellKind,
pub cid: usize, pub cid: usize,
z: f32, z: f32,
pub moisture: f32, pub moisture: f32,
pub resource: f32 // How much resource there is (between 0 and 1) pub resource: f32, // How much resource there is (between 0 and 1)
pub vertices: Vec<usize>
} }
impl CellData { 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<usize>) -> Self {
Self { Self {
kind, kind,
cid: cell, cid: cell,
z, z,
moisture, moisture,
resource resource,
vertices
} }
} }
// pub fn pos<'a>(&self, map: &'a Map) -> &'a Point { // pub fn pos<'a>(&self, map: &'a Map) -> &'a Point {