opty mesh update
This commit is contained in:
parent
3e5e4d5254
commit
72d6a678bd
23
src/map.rs
23
src/map.rs
@ -45,6 +45,9 @@ struct MapColors (Vec<[f32; 4]>);
|
|||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct CellsEntities (Vec<Entity>);
|
pub struct CellsEntities (Vec<Entity>);
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct MeshNeedsUpdate(bool);
|
||||||
|
|
||||||
fn setup(
|
fn setup(
|
||||||
mut cmds: Commands,
|
mut cmds: Commands,
|
||||||
mut meshes: ResMut<Assets<Mesh>>,
|
mut meshes: ResMut<Assets<Mesh>>,
|
||||||
@ -134,17 +137,21 @@ fn setup(
|
|||||||
Transform::default(),
|
Transform::default(),
|
||||||
Voronoi(voronoi),
|
Voronoi(voronoi),
|
||||||
MapColors(colors),
|
MapColors(colors),
|
||||||
|
MeshNeedsUpdate(true),
|
||||||
MapMarker
|
MapMarker
|
||||||
)).with_children(|parent| {
|
)).with_children(|parent| {
|
||||||
for cd in cells_data {
|
for cd in cells_data {
|
||||||
let mut cmd = parent.spawn((cd, LastUpdate(0)));
|
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) {
|
if trigger.duration > Duration::from_millis(100) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
let mut cd = cells.get_mut(trigger.target).unwrap();
|
let mut cd = cells.get_mut(trigger.target).unwrap();
|
||||||
match cd.kind {
|
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);
|
dbg!(trigger.duration);
|
||||||
@ -159,8 +166,10 @@ fn setup(
|
|||||||
pub struct LastUpdate(usize);
|
pub struct LastUpdate(usize);
|
||||||
|
|
||||||
fn update_cells(
|
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() {
|
for (mut cd, mut lu) in cells.iter_mut() {
|
||||||
lu.0 += 1;
|
lu.0 += 1;
|
||||||
if lu.0 > match cd.kind {
|
if lu.0 > match cd.kind {
|
||||||
@ -170,16 +179,18 @@ fn update_cells(
|
|||||||
} {
|
} {
|
||||||
lu.0 = 0;
|
lu.0 = 0;
|
||||||
cd.resource = (cd.resource + 1).clamp(0, 4);
|
cd.resource = (cd.resource + 1).clamp(0, 4);
|
||||||
|
map_needs_update.0 = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_map_mesh(
|
fn update_map_mesh(
|
||||||
cells: Query<&CellData>,
|
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>>
|
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) {
|
if let Some(mesh) = meshes.get_mut(mesh) {
|
||||||
let mut modified = false;
|
let mut modified = false;
|
||||||
for cd in cells.iter() {
|
for cd in cells.iter() {
|
||||||
@ -193,4 +204,6 @@ fn update_map_mesh(
|
|||||||
mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, cols.0.clone());
|
mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, cols.0.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
needs_update.0 = false;
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user