opty mesh update
This commit is contained in:
parent
3e5e4d5254
commit
72d6a678bd
43
src/map.rs
43
src/map.rs
@ -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,27 +179,31 @@ 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();
|
||||
if let Some(mesh) = meshes.get_mut(mesh) {
|
||||
let mut modified = false;
|
||||
for cd in cells.iter() {
|
||||
let col = cd.color();
|
||||
for id in cd.vertices.iter() {
|
||||
modified = modified || cols.0[*id] != col;
|
||||
cols.0[*id] = col.clone();
|
||||
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() {
|
||||
let col = cd.color();
|
||||
for id in cd.vertices.iter() {
|
||||
modified = modified || cols.0[*id] != col;
|
||||
cols.0[*id] = col.clone();
|
||||
}
|
||||
}
|
||||
if modified {
|
||||
mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, cols.0.clone());
|
||||
}
|
||||
}
|
||||
if modified {
|
||||
mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, cols.0.clone());
|
||||
}
|
||||
needs_update.0 = false;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user