picking things + random seed
This commit is contained in:
parent
62dd037957
commit
108d6afbd0
47
src/map.rs
47
src/map.rs
@ -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,25 +154,30 @@ 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)>
|
||||||
|
) {
|
||||||
|
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 map: Query<(&Mesh2d, &mut MapColors), With<MapMarker>>,
|
||||||
mut meshes: ResMut<Assets<Mesh>>
|
mut meshes: ResMut<Assets<Mesh>>
|
||||||
) {
|
) {
|
||||||
let (mesh, mut cols) = map.single_mut();
|
let (mesh, mut cols) = map.single_mut();
|
||||||
if let Some(mesh) = meshes.get_mut(mesh) {
|
if let Some(mesh) = meshes.get_mut(mesh) {
|
||||||
// let cols = mesh.attribute_mut(Mesh::ATTRIBUTE_COLOR).unwrap();
|
|
||||||
let mut modified = false;
|
let mut modified = false;
|
||||||
for (mut cd, mut lu) in cells.iter_mut() {
|
for cd in cells.iter() {
|
||||||
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();
|
|
||||||
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;
|
||||||
|
18
src/ui.rs
18
src/ui.rs
@ -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();
|
||||||
|
Loading…
Reference in New Issue
Block a user