From 108d6afbd0295ed14acfefd77527de279626a05c Mon Sep 17 00:00:00 2001 From: Arkitu <85173315+Arkitu@users.noreply.github.com> Date: Sat, 11 Jan 2025 15:09:32 +0100 Subject: [PATCH] picking things + random seed --- src/map.rs | 47 +++++++++++++++++++++++++++++------------------ src/ui.rs | 18 +++++++++++------- 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/src/map.rs b/src/map.rs index 043d0b3..768e384 100644 --- a/src/map.rs +++ b/src/map.rs @@ -1,6 +1,6 @@ use bevy::{asset::RenderAssetUsages, picking::PickSet, prelude::*, render::mesh::{Indices, PrimitiveTopology}, utils::HashMap}; use noise::{Fbm, MultiFractal, NoiseFn, Perlin}; -use rand::{Rng, SeedableRng}; +use rand::{thread_rng, Rng, SeedableRng}; use voronoice::{BoundingBox, Point, VoronoiBuilder}; mod cells; @@ -15,8 +15,9 @@ impl bevy::prelude::Plugin for Plugin { .insert_resource(Time::::from_seconds(0.25)) // Time for a day .add_systems(FixedUpdate, update_cells) .add_systems(PreUpdate, picking_backend.in_set(PickSet::Backend)) + .add_systems(Update, update_map_mesh) .insert_resource(ClearColor(Color::srgb(0., 0., 1.))) - .insert_resource(Seed(0)); + .insert_resource(Seed(thread_rng().gen())); } } @@ -79,7 +80,7 @@ fn setup( } else if z <= 0.52 { CellKind::Beach } else if z < 0.8 { - CellKind::Grass + CellKind::Dirt } else { CellKind::Stone }; @@ -135,8 +136,13 @@ fn setup( )).with_children(|parent| { for cd in cells_data { let mut cmd = parent.spawn((cd, LastUpdate(0))); - cmd.observe(|trigger: Trigger>, mut cells: Query<&mut CellData>| { - cells.get_mut(trigger.target).unwrap().kind = CellKind::Sea; + cmd.observe(|trigger: Trigger>, mut cells: Query<&mut CellData>| { + let mut cd = cells.get_mut(trigger.target).unwrap(); + match cd.kind { + CellKind::Dirt | CellKind::Grass => {cd.kind = CellKind::Forest;}, + _ => {} + } + dbg!(trigger.target); }); cells_entities.push(cmd.id()); } @@ -148,25 +154,30 @@ fn setup( pub struct LastUpdate(usize); fn update_cells( - mut cells: Query<(&mut CellData, &mut LastUpdate)>, + mut cells: Query<(&mut CellData, &mut LastUpdate)> +) { + for (mut cd, mut lu) in cells.iter_mut() { + lu.0 += 1; + if lu.0 > match cd.kind { + CellKind::Void | CellKind::Sea | CellKind::Beach | CellKind::Dirt | CellKind::Stone => usize::MAX, + CellKind::Forest => 100*365/4, // Let's say that a forest takes 100 years to mature + CellKind::Grass => 7*7/4 // Let's say that grass takes 7 weaks to reach its max + } { + lu.0 = 0; + cd.resource = (cd.resource + 1).clamp(0, 4); + } + } +} + +fn update_map_mesh( + 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(); let mut modified = false; - for (mut cd, mut lu) in cells.iter_mut() { - lu.0 += 1; - if lu.0 > match cd.kind { - CellKind::Void | CellKind::Sea | CellKind::Beach | CellKind::Dirt | CellKind::Stone => usize::MAX, - CellKind::Forest => 100*365/4, // Let's say that a forest takes 100 years to mature - CellKind::Grass => 7*7/4 // Let's say that grass takes 7 weaks to reach its max - } { - lu.0 = 0; - cd.resource = (cd.resource + 1).clamp(0, 4); - } - // cd.update(); + for cd in cells.iter() { let col = cd.color(); for id in cd.vertices.iter() { modified = modified || cols.0[*id] != col; diff --git a/src/ui.rs b/src/ui.rs index a5e0721..8bf61bc 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -16,13 +16,17 @@ fn setup( mut cmds: Commands ) { cmds.spawn(( - // Node { - // width: Val::Percent(100.0), - // height: Val::Percent(100.0), - // align_items: AlignItems::Center, - // justify_content: JustifyContent::Center, - // ..default() - // }, + Node { + width: Val::Percent(100.0), + height: Val::Percent(100.0), + align_items: AlignItems::Center, + justify_content: JustifyContent::Center, + ..default() + }, + PickingBehavior { + should_block_lower: false, + is_hoverable: true + }, MapUIComponent)); // )).observe(|mut trigger: Trigger>, map: Query<(&map::Voronoi, &map::CellsEntities), With>| { // let event = trigger.event();