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 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::<Fixed>::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<Pointer<Drag>>, mut cells: Query<&mut CellData>| {
cells.get_mut(trigger.target).unwrap().kind = CellKind::Sea;
cmd.observe(|trigger: Trigger<Pointer<Click>>, 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<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() {
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;

View File

@ -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<Pointer<Click>>, map: Query<(&map::Voronoi, &map::CellsEntities), With<MapMarker>>| {
// let event = trigger.event();