opty mesh update

This commit is contained in:
Arkitu 2025-01-15 19:04:22 +01:00
parent 3e5e4d5254
commit 72d6a678bd

View File

@ -45,6 +45,9 @@ struct MapColors (Vec<[f32; 4]>);
#[derive(Component)]
pub struct CellsEntities (Vec<Entity>);
#[derive(Component)]
pub struct MeshNeedsUpdate(bool);
fn setup(
mut cmds: Commands,
mut meshes: ResMut<Assets<Mesh>>,
@ -134,17 +137,21 @@ fn setup(
Transform::default(),
Voronoi(voronoi),
MapColors(colors),
MeshNeedsUpdate(true),
MapMarker
)).with_children(|parent| {
for cd in cells_data {
let mut cmd = parent.spawn((cd, LastUpdate(0)));
cmd.observe(|trigger: Trigger<Pointer<Click>>, mut cells: Query<&mut CellData>| {
cmd.observe(|trigger: Trigger<Pointer<Click>>, mut cells: Query<&mut CellData>, mut map_needs_update: Query<&mut MeshNeedsUpdate, With<MapMarker>>| {
if trigger.duration > Duration::from_millis(100) {
return
}
let mut cd = cells.get_mut(trigger.target).unwrap();
match cd.kind {
CellKind::Dirt | CellKind::Grass => {cd.kind = CellKind::Forest;},
CellKind::Dirt | CellKind::Grass => {
cd.kind = CellKind::Forest;
map_needs_update.single_mut().0 = true;
},
_ => {}
}
dbg!(trigger.duration);
@ -159,8 +166,10 @@ fn setup(
pub struct LastUpdate(usize);
fn update_cells(
mut cells: Query<(&mut CellData, &mut LastUpdate)>
mut cells: Query<(&mut CellData, &mut LastUpdate)>,
mut map_needs_update: Query<&mut MeshNeedsUpdate, With<MapMarker>>
) {
let mut map_needs_update = map_needs_update.single_mut();
for (mut cd, mut lu) in cells.iter_mut() {
lu.0 += 1;
if lu.0 > match cd.kind {
@ -170,16 +179,18 @@ fn update_cells(
} {
lu.0 = 0;
cd.resource = (cd.resource + 1).clamp(0, 4);
map_needs_update.0 = true;
}
}
}
fn update_map_mesh(
cells: Query<&CellData>,
mut map: Query<(&Mesh2d, &mut MapColors), With<MapMarker>>,
mut map: Query<(&Mesh2d, &mut MapColors, &mut MeshNeedsUpdate), With<MapMarker>>,
mut meshes: ResMut<Assets<Mesh>>
) {
let (mesh, mut cols) = map.single_mut();
let (mesh, mut cols, mut needs_update) = map.single_mut();
if needs_update.0 {
if let Some(mesh) = meshes.get_mut(mesh) {
let mut modified = false;
for cd in cells.iter() {
@ -193,4 +204,6 @@ fn update_map_mesh(
mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, cols.0.clone());
}
}
needs_update.0 = false;
}
}