forest button (functional)

This commit is contained in:
Arkitu 2025-01-23 21:41:32 +01:00
parent 7bd31de293
commit a9e711f3b9
2 changed files with 48 additions and 24 deletions

View File

@ -1,6 +1,6 @@
use std::time::Duration; 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 noise::{Fbm, MultiFractal, NoiseFn, Perlin};
use rand::{thread_rng, Rng, SeedableRng}; use rand::{thread_rng, Rng, SeedableRng};
use voronoice::{BoundingBox, Point, VoronoiBuilder}; use voronoice::{BoundingBox, Point, VoronoiBuilder};
@ -9,8 +9,9 @@ mod cells;
mod picking; mod picking;
use picking::*; use picking::*;
use cells::*; use cells::*;
pub use cells::CellKind;
use crate::time::GameTime; use crate::{time::GameTime, ui::CurrentAction};
pub struct Plugin; pub struct Plugin;
impl bevy::prelude::Plugin for Plugin { impl bevy::prelude::Plugin for Plugin {
@ -154,23 +155,33 @@ fn setup(
mut cells: Query<&mut Cell>, mut cells: Query<&mut Cell>,
mut map_needs_update: Query<&mut MeshNeedsUpdate, With<MapMarker>>, mut map_needs_update: Query<&mut MeshNeedsUpdate, With<MapMarker>>,
mut cmds: Commands, mut cmds: Commands,
gt: Res<GameTime> ca: Res<CurrentAction>,
gt: Res<GameTime>,
| { | {
if trigger.duration > Duration::from_millis(200) { if trigger.duration > Duration::from_millis(200) {
return return
} }
let mut cell = cells.get_mut(trigger.target).unwrap(); match *ca {
match cell.kind { CurrentAction::ChangeCell(ck) => {
CellKind::Dirt | CellKind::Grass => { let mut cell = cells.get_mut(trigger.target).unwrap();
cmds.entity(trigger.target).insert((Wealth(0), Regeneration { match ck {
last_update: gt.current, CellKind::Forest => match cell.kind {
full_growth_duration: CellKind::Forest.regen_full_growth_duration() CellKind::Dirt | CellKind::Grass => {
})); cmds.entity(trigger.target).insert((Wealth(0), Regeneration {
cell.kind = CellKind::Forest; last_update: gt.current,
map_needs_update.single_mut().0 = true; 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()); cells_entities.push(cmd.id());
} }

View File

@ -1,17 +1,25 @@
use mevy::*; 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 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; pub struct Plugin;
impl bevy::prelude::Plugin for Plugin { impl bevy::prelude::Plugin for Plugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(Startup, setup) app.init_resource::<CurrentAction>()
.add_systems(Startup, setup)
.add_systems(Update, zoom_with_scroll); .add_systems(Update, zoom_with_scroll);
embedded_asset!(app, "../assets/ui/tree.png"); embedded_asset!(app, "../assets/ui/tree.png");
} }
} }
#[derive(Resource, Default)]
pub enum CurrentAction {
#[default]
None,
ChangeCell(CellKind)
}
#[derive(Component, Debug)] #[derive(Component, Debug)]
struct PointersDragging(HashMap<PointerId, Vec2>); struct PointersDragging(HashMap<PointerId, Vec2>);
@ -22,20 +30,14 @@ fn setup(
mut world: Commands, mut world: Commands,
asset_server: Res<AssetServer> asset_server: Res<AssetServer>
) { ) {
// Spawn all ui elements as children of this one
spawn!{ spawn!{
Node {width: 100%, height: 100%, display: Display::Flex, flex_direction: FlexDirection::Column, !}; Node {width: 100%, height: 100%, display: Display::Flex, flex_direction: FlexDirection::Column, !};
PickingBehavior { PickingBehavior {
should_block_lower: false, should_block_lower: false,
is_hoverable: true is_hoverable: true
}; };
.observe(move |trigger: Trigger<Pointer<DragStart>>, mut ptrs: Query<&mut PointersDragging>| { .observe(|trigger: Trigger<Pointer<DragEnd>>, 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<Pointer<DragEnd>>, mut ptrs: Query<&mut PointersDragging>| {
if trigger.button == PointerButton::Primary { if trigger.button == PointerButton::Primary {
ptrs.single_mut().0.remove(&trigger.pointer_id); ptrs.single_mut().0.remove(&trigger.pointer_id);
} }
@ -78,6 +80,13 @@ fn setup(
should_block_lower: false, should_block_lower: false,
is_hoverable: true is_hoverable: true
}; };
.observe(|trigger: Trigger<Pointer<DragStart>>, 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{ Node{
@ -94,10 +103,14 @@ fn setup(
height: 80%, height: 80%,
margin: [>1vh], margin: [>1vh],
!}; !};
.observe(|trigger: Trigger<Pointer<Click>>, mut ca: ResMut<CurrentAction>| {
if trigger.button == PointerButton::Primary {
*ca = CurrentAction::ChangeCell(CellKind::Forest);
}
});
] ]
] ]
} }
// Spawn all ui elements as children of this one
} }
fn zoom_with_scroll( fn zoom_with_scroll(