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 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<MapMarker>>,
mut cmds: Commands,
gt: Res<GameTime>
ca: Res<CurrentAction>,
gt: Res<GameTime>,
| {
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());
}

View File

@ -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::<CurrentAction>()
.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<PointerId, Vec2>);
@ -22,20 +30,14 @@ fn setup(
mut world: Commands,
asset_server: Res<AssetServer>
) {
// 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<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);
}
}
});
.observe(move |trigger: Trigger<Pointer<DragEnd>>, mut ptrs: Query<&mut PointersDragging>| {
.observe(|trigger: Trigger<Pointer<DragEnd>>, 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<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{
@ -94,10 +103,14 @@ fn setup(
height: 80%,
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(