From a9e711f3b911f8d94ddf07bf0a29873eec7d0f28 Mon Sep 17 00:00:00 2001 From: Arkitu <85173315+Arkitu@users.noreply.github.com> Date: Thu, 23 Jan 2025 21:41:32 +0100 Subject: [PATCH] forest button (functional) --- src/map.rs | 37 ++++++++++++++++++++++++------------- src/ui.rs | 35 ++++++++++++++++++++++++----------- 2 files changed, 48 insertions(+), 24 deletions(-) diff --git a/src/map.rs b/src/map.rs index 524e657..f886b44 100644 --- a/src/map.rs +++ b/src/map.rs @@ -1,6 +1,6 @@ use std::time::Duration; -use bevy::{asset::RenderAssetUsages, picking::PickSet, prelude::*, render::mesh::{Indices, PrimitiveTopology}, utils::HashMap}; +use bevy::{asset::RenderAssetUsages, picking::PickSet, prelude::*, render::mesh::{Indices, PrimitiveTopology}}; use noise::{Fbm, MultiFractal, NoiseFn, Perlin}; use rand::{thread_rng, Rng, SeedableRng}; use voronoice::{BoundingBox, Point, VoronoiBuilder}; @@ -9,8 +9,9 @@ mod cells; mod picking; use picking::*; use cells::*; +pub use cells::CellKind; -use crate::time::GameTime; +use crate::{time::GameTime, ui::CurrentAction}; pub struct Plugin; impl bevy::prelude::Plugin for Plugin { @@ -154,23 +155,33 @@ fn setup( mut cells: Query<&mut Cell>, mut map_needs_update: Query<&mut MeshNeedsUpdate, With>, mut cmds: Commands, - gt: Res + ca: Res, + gt: Res, | { if trigger.duration > Duration::from_millis(200) { return } - let mut cell = cells.get_mut(trigger.target).unwrap(); - match cell.kind { - CellKind::Dirt | CellKind::Grass => { - cmds.entity(trigger.target).insert((Wealth(0), Regeneration { - last_update: gt.current, - full_growth_duration: CellKind::Forest.regen_full_growth_duration() - })); - cell.kind = CellKind::Forest; - map_needs_update.single_mut().0 = true; - }, + match *ca { + CurrentAction::ChangeCell(ck) => { + let mut cell = cells.get_mut(trigger.target).unwrap(); + match ck { + CellKind::Forest => match cell.kind { + CellKind::Dirt | CellKind::Grass => { + cmds.entity(trigger.target).insert((Wealth(0), Regeneration { + last_update: gt.current, + full_growth_duration: CellKind::Forest.regen_full_growth_duration() + })); + cell.kind = CellKind::Forest; + map_needs_update.single_mut().0 = true; + }, + _ => {} + }, + _ => {} + } + } _ => {} } + }); cells_entities.push(cmd.id()); } diff --git a/src/ui.rs b/src/ui.rs index 9ce9ccd..e7a9a08 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -1,17 +1,25 @@ use mevy::*; use bevy::{asset::embedded_asset, input::mouse::MouseWheel, math::{NormedVectorSpace, VectorSpace}, picking::{focus::HoverMap, pointer::PointerId}, prelude::*, utils::{dbg, HashMap}, window::PrimaryWindow}; -use crate::map::{self, MapMarker}; +use crate::map::{self, CellKind, MapMarker}; pub struct Plugin; impl bevy::prelude::Plugin for Plugin { fn build(&self, app: &mut App) { - app.add_systems(Startup, setup) + app.init_resource::() + .add_systems(Startup, setup) .add_systems(Update, zoom_with_scroll); embedded_asset!(app, "../assets/ui/tree.png"); } } +#[derive(Resource, Default)] +pub enum CurrentAction { + #[default] + None, + ChangeCell(CellKind) +} + #[derive(Component, Debug)] struct PointersDragging(HashMap); @@ -22,20 +30,14 @@ fn setup( mut world: Commands, asset_server: Res ) { + // Spawn all ui elements as children of this one spawn!{ Node {width: 100%, height: 100%, display: Display::Flex, flex_direction: FlexDirection::Column, !}; PickingBehavior { should_block_lower: false, is_hoverable: true }; - .observe(move |trigger: Trigger>, mut ptrs: Query<&mut PointersDragging>| { - if trigger.button == PointerButton::Primary { - if let Ok(mut ptrs) = ptrs.get_mut(trigger.target) { - ptrs.0.insert(trigger.pointer_id, trigger.pointer_location.position); - } - } - }); - .observe(move |trigger: Trigger>, mut ptrs: Query<&mut PointersDragging>| { + .observe(|trigger: Trigger>, mut ptrs: Query<&mut PointersDragging>| { if trigger.button == PointerButton::Primary { ptrs.single_mut().0.remove(&trigger.pointer_id); } @@ -78,6 +80,13 @@ fn setup( should_block_lower: false, is_hoverable: true }; + .observe(|trigger: Trigger>, mut ptrs: Query<&mut PointersDragging>| { + if trigger.button == PointerButton::Primary { + if let Ok(mut ptrs) = ptrs.get_mut(trigger.target) { + ptrs.0.insert(trigger.pointer_id, trigger.pointer_location.position); + } + } + }); ] [ Node{ @@ -94,10 +103,14 @@ fn setup( height: 80%, margin: [>1vh], !}; + .observe(|trigger: Trigger>, mut ca: ResMut| { + if trigger.button == PointerButton::Primary { + *ca = CurrentAction::ChangeCell(CellKind::Forest); + } + }); ] ] } - // Spawn all ui elements as children of this one } fn zoom_with_scroll(