picking things + random seed

This commit is contained in:
Arkitu 2025-01-11 15:09:32 +01:00
parent 62dd037957
commit 108d6afbd0
2 changed files with 40 additions and 25 deletions

View File

@ -1,6 +1,6 @@
use bevy::{asset::RenderAssetUsages, picking::PickSet, prelude::*, render::mesh::{Indices, PrimitiveTopology}, utils::HashMap}; use bevy::{asset::RenderAssetUsages, picking::PickSet, prelude::*, render::mesh::{Indices, PrimitiveTopology}, utils::HashMap};
use noise::{Fbm, MultiFractal, NoiseFn, Perlin}; use noise::{Fbm, MultiFractal, NoiseFn, Perlin};
use rand::{Rng, SeedableRng}; use rand::{thread_rng, Rng, SeedableRng};
use voronoice::{BoundingBox, Point, VoronoiBuilder}; use voronoice::{BoundingBox, Point, VoronoiBuilder};
mod cells; mod cells;
@ -15,8 +15,9 @@ impl bevy::prelude::Plugin for Plugin {
.insert_resource(Time::<Fixed>::from_seconds(0.25)) // Time for a day .insert_resource(Time::<Fixed>::from_seconds(0.25)) // Time for a day
.add_systems(FixedUpdate, update_cells) .add_systems(FixedUpdate, update_cells)
.add_systems(PreUpdate, picking_backend.in_set(PickSet::Backend)) .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(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 { } else if z <= 0.52 {
CellKind::Beach CellKind::Beach
} else if z < 0.8 { } else if z < 0.8 {
CellKind::Grass CellKind::Dirt
} else { } else {
CellKind::Stone CellKind::Stone
}; };
@ -135,8 +136,13 @@ fn setup(
)).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<Drag>>, mut cells: Query<&mut CellData>| { cmd.observe(|trigger: Trigger<Pointer<Click>>, mut cells: Query<&mut CellData>| {
cells.get_mut(trigger.target).unwrap().kind = CellKind::Sea; 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()); cells_entities.push(cmd.id());
} }
@ -148,14 +154,8 @@ 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: Query<(&Mesh2d, &mut MapColors), With<MapMarker>>,
mut meshes: ResMut<Assets<Mesh>>
) { ) {
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() { 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 {
@ -166,7 +166,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);
} }
// cd.update(); }
}
fn update_map_mesh(
cells: Query<&CellData>,
mut map: Query<(&Mesh2d, &mut MapColors), With<MapMarker>>,
mut meshes: ResMut<Assets<Mesh>>
) {
let (mesh, mut cols) = map.single_mut();
if let Some(mesh) = meshes.get_mut(mesh) {
let mut modified = false;
for cd in cells.iter() {
let col = cd.color(); let col = cd.color();
for id in cd.vertices.iter() { for id in cd.vertices.iter() {
modified = modified || cols.0[*id] != col; modified = modified || cols.0[*id] != col;

View File

@ -16,13 +16,17 @@ fn setup(
mut cmds: Commands mut cmds: Commands
) { ) {
cmds.spawn(( cmds.spawn((
// Node { Node {
// width: Val::Percent(100.0), width: Val::Percent(100.0),
// height: Val::Percent(100.0), height: Val::Percent(100.0),
// align_items: AlignItems::Center, align_items: AlignItems::Center,
// justify_content: JustifyContent::Center, justify_content: JustifyContent::Center,
// ..default() ..default()
// }, },
PickingBehavior {
should_block_lower: false,
is_hoverable: true
},
MapUIComponent)); MapUIComponent));
// )).observe(|mut trigger: Trigger<Pointer<Click>>, map: Query<(&map::Voronoi, &map::CellsEntities), With<MapMarker>>| { // )).observe(|mut trigger: Trigger<Pointer<Click>>, map: Query<(&map::Voronoi, &map::CellsEntities), With<MapMarker>>| {
// let event = trigger.event(); // let event = trigger.event();