save
This commit is contained in:
parent
0699c5da2e
commit
1a68ab5af1
45
src/map.rs
45
src/map.rs
@ -7,6 +7,7 @@ use bevy::{
|
||||
picking::PickSet,
|
||||
prelude::*,
|
||||
render::mesh::{Indices, PrimitiveTopology},
|
||||
tasks::AsyncComputeTaskPool,
|
||||
utils::HashMap,
|
||||
};
|
||||
use noise::{Fbm, MultiFractal, NoiseFn, Perlin};
|
||||
@ -24,6 +25,8 @@ use cells::{
|
||||
};
|
||||
use picking::*;
|
||||
|
||||
use crate::map::cells::grow::{grow_thread, GrowThread};
|
||||
|
||||
pub struct Plugin;
|
||||
impl bevy::prelude::Plugin for Plugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
@ -57,7 +60,7 @@ struct Seed(u32);
|
||||
pub struct Voronoi(voronoice::Voronoi);
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct MapMarker;
|
||||
pub struct MeshMarker;
|
||||
|
||||
#[derive(Component)]
|
||||
struct MeshColors(Vec<[f32; 4]>);
|
||||
@ -68,6 +71,9 @@ pub struct CellsEntities(Vec<Entity>);
|
||||
#[derive(Component)]
|
||||
pub struct MeshNeedsUpdate(bool);
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Chunk(usize);
|
||||
|
||||
fn setup(
|
||||
mut cmds: Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
@ -124,7 +130,6 @@ fn setup(
|
||||
cells.push(Cell {
|
||||
kind: k,
|
||||
voronoi_id: i,
|
||||
vertices: vec![],
|
||||
});
|
||||
}
|
||||
|
||||
@ -190,10 +195,11 @@ fn setup(
|
||||
}
|
||||
|
||||
let mut cells_entities = Vec::with_capacity(cells.len());
|
||||
for ((poss, indices), ch_cells) in poss
|
||||
for (ch_id, ((poss, indices), ch_cells)) in poss
|
||||
.into_iter()
|
||||
.zip(indices.into_iter())
|
||||
.zip(cells.chunks_exact(CELLS_PER_CHUNK))
|
||||
.enumerate()
|
||||
{
|
||||
let colors = vec![[0.; 4]; poss.len()];
|
||||
let mut mesh = Mesh::new(
|
||||
@ -223,7 +229,8 @@ fn setup(
|
||||
Transform::default(),
|
||||
MeshColors(colors),
|
||||
MeshNeedsUpdate(true),
|
||||
MapMarker,
|
||||
MeshMarker,
|
||||
Chunk(ch_id),
|
||||
))
|
||||
.with_children(|parent| {
|
||||
for cell in ch_cells {
|
||||
@ -233,19 +240,7 @@ fn setup(
|
||||
// if let Some(ch) = hybrid_cells.get(&id) {
|
||||
// cmd.insert(GhostCell(*ch));
|
||||
// }
|
||||
match kind {
|
||||
CellKind::Grass | CellKind::Forest => {
|
||||
cmd.insert((
|
||||
Wealth(0),
|
||||
Regeneration {
|
||||
last_update: Duration::ZERO,
|
||||
full_growth_duration: kind.regen_full_growth_duration(),
|
||||
},
|
||||
));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
cmd.observe(self::cells::on_click);
|
||||
cmd.observe(self::cells::input::on_click);
|
||||
cells_entities.push(cmd.id());
|
||||
}
|
||||
});
|
||||
@ -253,12 +248,18 @@ fn setup(
|
||||
|
||||
cmds.insert_resource(Voronoi(voronoi));
|
||||
cmds.insert_resource(CellsEntities(cells_entities));
|
||||
|
||||
let task_pool = AsyncComputeTaskPool::get();
|
||||
cmds.insert_resource(GrowThread(task_pool.spawn(grow_thread())));
|
||||
}
|
||||
|
||||
// TODO: update this to take chunks into account
|
||||
fn update_chunk_mesh(
|
||||
cells: Query<(&Cell, Option<&Wealth>)>,
|
||||
mut chunks: Query<(&Mesh3d, &mut MeshColors, &mut MeshNeedsUpdate, &Children), With<MapMarker>>,
|
||||
cells: Query<&Cell>,
|
||||
mut chunks: Query<
|
||||
(&Mesh3d, &mut MeshColors, &mut MeshNeedsUpdate, &Children),
|
||||
With<MeshMarker>,
|
||||
>,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
) {
|
||||
for (mesh, mut cols, mut needs_update, children) in chunks.iter_mut() {
|
||||
@ -267,9 +268,9 @@ fn update_chunk_mesh(
|
||||
let mut modified = false;
|
||||
// let mut rng = thread_rng();
|
||||
for child in children.iter() {
|
||||
let (cell, wealth) = cells.get(*child).unwrap();
|
||||
let cell = cells.get(*child).unwrap();
|
||||
// let col: [f32; 4] = [rng.gen(), rng.gen(), rng.gen(), 1.];
|
||||
let col = cell.color(wealth.map(|w| w.0).unwrap_or_default());
|
||||
let col = cell.color();
|
||||
modified = modified || cols.0[cell.voronoi_id % CELLS_PER_CHUNK] != col;
|
||||
cols.0[cell.voronoi_id % CELLS_PER_CHUNK] = col;
|
||||
// for id in cell.vertices.iter() {
|
||||
@ -282,6 +283,8 @@ fn update_chunk_mesh(
|
||||
}
|
||||
}
|
||||
needs_update.0 = false;
|
||||
// Update at most one chunk per frame to avoid freezes
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
358
src/map/cells.rs
358
src/map/cells.rs
@ -6,24 +6,26 @@ use rand::{seq::IteratorRandom, thread_rng, Rng};
|
||||
use crate::{time::GameTime, ui::CurrentAction};
|
||||
|
||||
use super::{
|
||||
animals::Animal, AnimalKind, CellsEntities, MapMarker, MeshNeedsUpdate, Voronoi,
|
||||
animals::Animal, AnimalKind, CellsEntities, MeshMarker, MeshNeedsUpdate, Voronoi,
|
||||
AVERAGE_NEIGHBORS_NUMBER, CELLS_PER_CHUNK, CELL_AREA,
|
||||
};
|
||||
|
||||
pub mod grow;
|
||||
pub mod input;
|
||||
pub mod material;
|
||||
use material::CellMaterial;
|
||||
|
||||
pub struct Plugin;
|
||||
impl bevy::prelude::Plugin for Plugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(
|
||||
FixedUpdate,
|
||||
(
|
||||
cells_regeneration.run_if(on_timer(Duration::from_secs(1))),
|
||||
expand.run_if(on_timer(Duration::from_secs(1))),
|
||||
),
|
||||
)
|
||||
.add_plugins(MaterialPlugin::<CellMaterial>::default());
|
||||
// app.add_systems(
|
||||
// FixedUpdate,
|
||||
// (
|
||||
// cells_regeneration.run_if(on_timer(Duration::from_millis(500))),
|
||||
// expand.run_if(on_timer(Duration::from_millis(300))),
|
||||
// ),
|
||||
// )
|
||||
app.add_plugins(MaterialPlugin::<CellMaterial>::default());
|
||||
}
|
||||
}
|
||||
|
||||
@ -31,50 +33,60 @@ impl bevy::prelude::Plugin for Plugin {
|
||||
pub enum CellKind {
|
||||
Sea,
|
||||
Beach,
|
||||
Forest,
|
||||
Forest { wealth: WealthType },
|
||||
Dirt,
|
||||
Stone,
|
||||
Grass,
|
||||
Grass { wealth: WealthType },
|
||||
}
|
||||
impl CellKind {
|
||||
pub fn regen_full_growth_duration(&self) -> Duration {
|
||||
pub const fn regen_full_growth_duration(&self) -> Duration {
|
||||
match self {
|
||||
CellKind::Sea | CellKind::Beach | CellKind::Dirt | CellKind::Stone => unreachable!(),
|
||||
CellKind::Forest => Duration::from_secs(100 * 365 * 24 * 60 * 60), // Let's say that a forest takes 100 years to mature
|
||||
CellKind::Grass => Duration::from_secs(7 * 7 * 24 * 60 * 60), // Let's say that grass takes 7 weeks to reach its max
|
||||
CellKind::Forest { .. } => Duration::from_secs(100 * 365 * 24 * 60 * 60), // Let's say that a forest takes 100 years to mature
|
||||
CellKind::Grass { .. } => Duration::from_secs(7 * 7 * 24 * 60 * 60), // Let's say that grass takes 7 weeks to reach its max
|
||||
}
|
||||
}
|
||||
pub fn can_place_animal(&self, kind: AnimalKind) -> bool {
|
||||
pub const fn can_place_animal(&self, kind: AnimalKind) -> bool {
|
||||
match self {
|
||||
CellKind::Sea => false,
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
pub const fn grass_wealth(&self) -> Option<usize> {
|
||||
match self {
|
||||
CellKind::Grass { wealth } => Some(*wealth),
|
||||
CellKind::Forest { wealth } => Some(
|
||||
wealth.saturating_mul(
|
||||
(CellKind::Forest { wealth: 0 }
|
||||
.regen_full_growth_duration()
|
||||
.as_secs()
|
||||
/ CellKind::Grass { wealth: 0 }
|
||||
.regen_full_growth_duration()
|
||||
.as_secs()) as usize,
|
||||
),
|
||||
),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Component, Clone)]
|
||||
pub struct Cell {
|
||||
pub kind: CellKind,
|
||||
pub voronoi_id: usize,
|
||||
pub vertices: Vec<usize>,
|
||||
}
|
||||
impl Cell {
|
||||
pub const fn color(&self, wealth: u8) -> [f32; 4] {
|
||||
pub const fn color(&self) -> [f32; 4] {
|
||||
match self.kind {
|
||||
CellKind::Sea => [0., 0., 1., 1.],
|
||||
CellKind::Beach => [0.82, 0.84, 0.51, 1.],
|
||||
CellKind::Forest => [0., 0.5 - (wealth as f32 / 255. * 0.4), 0., 1.],
|
||||
CellKind::Dirt => [
|
||||
0.53 - (wealth as f32 / 255. * 0.4),
|
||||
0.38 - (wealth as f32 / 255. * 0.4),
|
||||
0.29 - (wealth as f32 / 255. * 0.4),
|
||||
1.,
|
||||
],
|
||||
CellKind::Forest { wealth } => [0., 0.5 - (wealth_to_unit(wealth) * 0.4), 0., 1.],
|
||||
CellKind::Dirt => [0.53, 0.38, 0.29, 1.],
|
||||
CellKind::Stone => [0.5, 0.5, 0.5, 1.],
|
||||
CellKind::Grass => [
|
||||
(136. / 255.) - (wealth as f32 / 255. * 0.15),
|
||||
(154. / 255.) + (wealth as f32 / 255. * 0.1),
|
||||
(59. / 255.) - (wealth as f32 / 255. * 0.15),
|
||||
CellKind::Grass { wealth } => [
|
||||
(136. / 255.) - (wealth_to_unit(wealth) * 0.15),
|
||||
(154. / 255.) + (wealth_to_unit(wealth) * 0.1),
|
||||
(59. / 255.) - (wealth_to_unit(wealth) * 0.15),
|
||||
1.,
|
||||
],
|
||||
}
|
||||
@ -93,189 +105,113 @@ impl Cell {
|
||||
// pub chunk:
|
||||
// };
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Wealth(pub u8);
|
||||
|
||||
impl Default for Wealth {
|
||||
fn default() -> Self {
|
||||
Wealth(u8::MAX)
|
||||
}
|
||||
pub type WealthType = usize;
|
||||
const fn wealth_to_unit(wealth: WealthType) -> f32 {
|
||||
wealth as f32 / WealthType::MAX as f32
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
#[require(Wealth)]
|
||||
pub struct Regeneration {
|
||||
pub last_update: Duration,
|
||||
pub full_growth_duration: Duration,
|
||||
}
|
||||
// const STEP: WealthType = WealthType::MAX / 4;
|
||||
// pub fn cells_regeneration(
|
||||
// mut cells: Query<(&mut Regeneration, &mut Wealth, &Parent)>,
|
||||
// mut chunks: Query<&mut MeshNeedsUpdate, With<MeshMarker>>,
|
||||
// gt: Res<GameTime>,
|
||||
// ) {
|
||||
// for (mut regen, mut wealth, parent) in cells.iter_mut() {
|
||||
// while gt.current - regen.last_update > regen.full_growth_duration / u8::MAX as u32 {
|
||||
// regen.last_update = gt
|
||||
// .current
|
||||
// .min(regen.last_update + (regen.full_growth_duration / u8::MAX as u32));
|
||||
// wealth.0 = wealth.0.saturating_add(1);
|
||||
// // Not update map each time for optimization
|
||||
// if wealth.0 % STEP == 0 {
|
||||
// chunks.get_mut(parent.get()).unwrap().0 = true;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
const STEP: u8 = u8::MAX / 4;
|
||||
pub fn cells_regeneration(
|
||||
mut cells: Query<(&mut Regeneration, &mut Wealth, &Parent)>,
|
||||
mut chunks: Query<&mut MeshNeedsUpdate, With<MapMarker>>,
|
||||
gt: Res<GameTime>,
|
||||
) {
|
||||
for (mut regen, mut wealth, parent) in cells.iter_mut() {
|
||||
while gt.current - regen.last_update > regen.full_growth_duration / u8::MAX as u32 {
|
||||
regen.last_update = gt
|
||||
.current
|
||||
.min(regen.last_update + (regen.full_growth_duration / u8::MAX as u32));
|
||||
wealth.0 = wealth.0.saturating_add(1);
|
||||
// Not update map each time for optimization
|
||||
if wealth.0 % STEP == 0 {
|
||||
chunks.get_mut(parent.get()).unwrap().0 = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn expand(
|
||||
mut cells: Query<(&mut Cell, Option<&Wealth>)>,
|
||||
voronoi: Res<Voronoi>,
|
||||
cells_entities: Res<CellsEntities>,
|
||||
mut cmds: Commands,
|
||||
t: Res<Time>,
|
||||
gt: Res<GameTime>,
|
||||
) {
|
||||
let mut random = thread_rng();
|
||||
let mut changes = Vec::new();
|
||||
for (cell, wealth) in cells.iter() {
|
||||
// TODO: search beter numbers, for now they are arbitrary
|
||||
if cell.kind == CellKind::Forest || cell.kind == CellKind::Grass {
|
||||
let wealth = if cell.kind == CellKind::Forest {
|
||||
1.
|
||||
} else {
|
||||
wealth.unwrap().0 as f64 / 255.
|
||||
};
|
||||
if random.gen_bool(
|
||||
dbg!(
|
||||
t.elapsed_secs_f64() * (gt.speed as f64) * wealth * 1e-7
|
||||
/ (CELL_AREA as f64).sqrt()
|
||||
)
|
||||
.min(1.),
|
||||
) {
|
||||
// "Grass can expand into adjacent dirt at a rate of 2.5 to 7.5 cm (1 to 3 inches) per month.", DeepSeek
|
||||
// With some (way too complicated and probably false) computation, we get for a good grass an extension rate of (1e-7 / sqrt(cell_area)) cells per seconds
|
||||
let target = voronoi
|
||||
.0
|
||||
.cell(cell.voronoi_id)
|
||||
.iter_neighbors()
|
||||
.choose(&mut random)
|
||||
.unwrap();
|
||||
changes.extend(
|
||||
voronoi
|
||||
.0
|
||||
.cell(cell.voronoi_id)
|
||||
.iter_neighbors()
|
||||
.map(|t| (t, CellKind::Grass)),
|
||||
);
|
||||
}
|
||||
}
|
||||
if cell.kind == CellKind::Forest {
|
||||
if random.gen_bool(
|
||||
dbg!(
|
||||
t.elapsed_secs_f64()
|
||||
* (gt.speed as f64)
|
||||
* (wealth.unwrap().0 as f64 / 255.).sqrt()
|
||||
* 5.6e-7
|
||||
/ (CELL_AREA as f64).sqrt()
|
||||
* AVERAGE_NEIGHBORS_NUMBER
|
||||
)
|
||||
.min(1.),
|
||||
) {
|
||||
// "Forests can expand at a rate of 1 to 10 meters per year", DeepSeek
|
||||
// Same computations as above : (5.6e-7 / sqrt(cell_area)) cells per seconds
|
||||
let target = voronoi
|
||||
.0
|
||||
.cell(cell.voronoi_id)
|
||||
.iter_neighbors()
|
||||
.choose(&mut random)
|
||||
.unwrap();
|
||||
changes.push((target, CellKind::Forest));
|
||||
}
|
||||
}
|
||||
}
|
||||
for (v_id, kind) in changes {
|
||||
let target = cells_entities.0[v_id];
|
||||
let (mut cell, _) = cells.get_mut(target).unwrap();
|
||||
if kind == CellKind::Forest && (cell.kind == CellKind::Dirt || cell.kind == CellKind::Grass)
|
||||
{
|
||||
cell.kind = CellKind::Forest;
|
||||
cmds.entity(target).insert((
|
||||
Wealth(0),
|
||||
Regeneration {
|
||||
full_growth_duration: CellKind::Forest.regen_full_growth_duration(),
|
||||
last_update: gt.current,
|
||||
},
|
||||
));
|
||||
} else if kind == CellKind::Grass && cell.kind == CellKind::Dirt {
|
||||
cell.kind = CellKind::Grass;
|
||||
cmds.entity(target).insert((
|
||||
Wealth(0),
|
||||
Regeneration {
|
||||
full_growth_duration: CellKind::Grass.regen_full_growth_duration(),
|
||||
last_update: gt.current,
|
||||
},
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn on_click(
|
||||
trigger: Trigger<Pointer<Click>>,
|
||||
mut cells: Query<&mut Cell>,
|
||||
voronoi: Res<Voronoi>,
|
||||
mut map_needs_update: Query<&mut MeshNeedsUpdate, With<MapMarker>>,
|
||||
mut cmds: Commands,
|
||||
ca: Res<CurrentAction>,
|
||||
gt: Res<GameTime>,
|
||||
) {
|
||||
if trigger.duration > Duration::from_millis(200) {
|
||||
return;
|
||||
}
|
||||
let mut cell = cells.get_mut(trigger.target).unwrap();
|
||||
match *ca {
|
||||
CurrentAction::ChangeCell(ck) => 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;
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
CellKind::Grass => match cell.kind {
|
||||
CellKind::Dirt => {
|
||||
cmds.entity(trigger.target).insert((
|
||||
Wealth(0),
|
||||
Regeneration {
|
||||
last_update: gt.current,
|
||||
full_growth_duration: CellKind::Grass.regen_full_growth_duration(),
|
||||
},
|
||||
));
|
||||
cell.kind = CellKind::Grass;
|
||||
map_needs_update.single_mut().0 = true;
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
_ => {}
|
||||
},
|
||||
CurrentAction::AddAnimal(ak) => {
|
||||
if cell.kind.can_place_animal(ak) {
|
||||
let v_cell = voronoi.0.cell(cell.voronoi_id);
|
||||
let cell_pos = v_cell.site_position();
|
||||
cmds.spawn((
|
||||
Animal { kind: ak },
|
||||
Transform::from_xyz(cell_pos.x as f32, cell_pos.y as f32, 0.),
|
||||
));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
// pub fn expand(
|
||||
// mut cells: Query<(&mut Cell, Option<&Wealth>)>,
|
||||
// voronoi: Res<Voronoi>,
|
||||
// cells_entities: Res<CellsEntities>,
|
||||
// mut cmds: Commands,
|
||||
// t: Res<Time>,
|
||||
// gt: Res<GameTime>,
|
||||
// ) {
|
||||
// let mut random = thread_rng();
|
||||
// let mut changes = Vec::new();
|
||||
// for (cell, wealth) in cells.iter() {
|
||||
// // TODO: search beter numbers, for now they are arbitrary
|
||||
// if cell.kind == CellKind::Forest || cell.kind == CellKind::Grass {
|
||||
// let wealth = if cell.kind == CellKind::Forest {
|
||||
// 1.
|
||||
// } else {
|
||||
// wealth.unwrap().0 as f64 / 255.
|
||||
// };
|
||||
// if (t.elapsed_secs_f64() * (gt.speed as f64) * wealth * 1e-7
|
||||
// / (CELL_AREA as f64).sqrt())
|
||||
// >= 1.
|
||||
// {
|
||||
// // "Grass can expand into adjacent dirt at a rate of 2.5 to 7.5 cm (1 to 3 inches) per month.", DeepSeek
|
||||
// // With some (way too complicated and probably false) computation, we get for a good grass an extension rate of (1e-7 / sqrt(cell_area)) cells per seconds
|
||||
// let target = voronoi
|
||||
// .0
|
||||
// .cell(cell.voronoi_id)
|
||||
// .iter_neighbors()
|
||||
// .choose(&mut random)
|
||||
// .unwrap();
|
||||
// changes.extend(
|
||||
// voronoi
|
||||
// .0
|
||||
// .cell(cell.voronoi_id)
|
||||
// .iter_neighbors()
|
||||
// .map(|t| (t, CellKind::Grass)),
|
||||
// );
|
||||
// }
|
||||
// }
|
||||
// if cell.kind == CellKind::Forest {
|
||||
// if (t.elapsed_secs_f64()
|
||||
// * (gt.speed as f64)
|
||||
// * (wealth.unwrap().0 as f64 / 255.).sqrt()
|
||||
// * 5.6e-7
|
||||
// / (CELL_AREA as f64).sqrt()
|
||||
// * AVERAGE_NEIGHBORS_NUMBER)
|
||||
// >= 1.
|
||||
// {
|
||||
// // "Forests can expand at a rate of 1 to 10 meters per year", DeepSeek
|
||||
// // Same computations as above : (5.6e-7 / sqrt(cell_area)) cells per seconds
|
||||
// let target = voronoi
|
||||
// .0
|
||||
// .cell(cell.voronoi_id)
|
||||
// .iter_neighbors()
|
||||
// .choose(&mut random)
|
||||
// .unwrap();
|
||||
// changes.push((target, CellKind::Forest));
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// for (v_id, kind) in changes {
|
||||
// let target = cells_entities.0[v_id];
|
||||
// let (mut cell, _) = cells.get_mut(target).unwrap();
|
||||
// if kind == CellKind::Forest && (cell.kind == CellKind::Dirt || cell.kind == CellKind::Grass)
|
||||
// {
|
||||
// cell.kind = CellKind::Forest;
|
||||
// cmds.entity(target).insert((
|
||||
// Wealth(0),
|
||||
// Regeneration {
|
||||
// full_growth_duration: CellKind::Forest.regen_full_growth_duration(),
|
||||
// last_update: gt.current,
|
||||
// },
|
||||
// ));
|
||||
// } else if kind == CellKind::Grass && cell.kind == CellKind::Dirt {
|
||||
// cell.kind = CellKind::Grass;
|
||||
// cmds.entity(target).insert((
|
||||
// Wealth(0),
|
||||
// Regeneration {
|
||||
// full_growth_duration: CellKind::Grass.regen_full_growth_duration(),
|
||||
// last_update: gt.current,
|
||||
// },
|
||||
// ));
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
79
src/map/cells/grow.rs
Normal file
79
src/map/cells/grow.rs
Normal file
@ -0,0 +1,79 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy::tasks::futures_lite::{future, FutureExt};
|
||||
use bevy::tasks::{block_on, AsyncComputeTaskPool, IoTaskPool, Task};
|
||||
use voronoice::Voronoi;
|
||||
|
||||
use crate::map::{CellsEntities, Chunk, MeshNeedsUpdate};
|
||||
|
||||
use super::Cell;
|
||||
|
||||
pub struct Plugin;
|
||||
impl bevy::prelude::Plugin for Plugin {
|
||||
fn build(&self, app: &mut App) {}
|
||||
}
|
||||
|
||||
// Background task that manages terrain
|
||||
#[derive(Resource)]
|
||||
pub struct GrowThread(pub Task<()>);
|
||||
|
||||
#[derive(Component)]
|
||||
struct GetChunkTask(Task<Vec<Cell>>);
|
||||
|
||||
fn start_grow_task(mut cmds: Commands) {}
|
||||
|
||||
fn rx_chunks_updates(
|
||||
mut chunks: Query<(&Chunk, &mut GetChunkTask, &mut MeshNeedsUpdate)>,
|
||||
mut cells: Query<&mut Cell>,
|
||||
cells_entities: Res<CellsEntities>,
|
||||
) {
|
||||
for (ch_id, mut task, mut needs_update) in chunks.iter_mut() {
|
||||
let res = block_on(future::poll_once(&mut task.0));
|
||||
if let Some(updated_cells) = res {
|
||||
if updated_cells.len() > 0 {
|
||||
needs_update.0 = true;
|
||||
}
|
||||
for c in updated_cells.into_iter() {
|
||||
let id = c.voronoi_id;
|
||||
*cells.get_mut(cells_entities.0[id]).unwrap() = c;
|
||||
}
|
||||
let task_pool = IoTaskPool::get();
|
||||
task.0 = task_pool.spawn(async { Vec::new() })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn grow_thread(voronoi: Voronoi, current_frame: Arc<AtomicU>) {}
|
||||
|
||||
// fn begin_generating_map_chunks(mut my_tasks: ResMut<MyMapGenTasks>) {
|
||||
// let task_pool = AsyncComputeTaskPool::get();
|
||||
// for chunk_coord in decide_what_chunks_to_generate(/* ... */) {
|
||||
// // we might have already spawned a task for this `chunk_coord`
|
||||
// if my_tasks.generating_chunks.contains_key(&chunk_coord) {
|
||||
// continue;
|
||||
// }
|
||||
// let task = task_pool.spawn(async move {
|
||||
// // TODO: do whatever you want here!
|
||||
// generate_map_chunk(chunk_coord)
|
||||
// });
|
||||
// my_tasks.generating_chunks.insert(chunk_coord, task);
|
||||
// }
|
||||
// }
|
||||
|
||||
// fn receive_generated_map_chunks(mut my_tasks: ResMut<MyMapGenTasks>) {
|
||||
// my_tasks.generating_chunks.retain(|chunk_coord, task| {
|
||||
// // check on our task to see how it's doing :)
|
||||
// let status = block_on(future::poll_once(task));
|
||||
|
||||
// // keep the entry in our HashMap only if the task is not done yet
|
||||
// let retain = status.is_none();
|
||||
|
||||
// // if this task is done, handle the data it returned!
|
||||
// if let Some(mut chunk_data) = status {
|
||||
// // TODO: do something with the returned `chunk_data`
|
||||
// }
|
||||
|
||||
// retain
|
||||
// });
|
||||
// }
|
60
src/map/cells/input.rs
Normal file
60
src/map/cells/input.rs
Normal file
@ -0,0 +1,60 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::{
|
||||
map::{animals::Animal, MeshMarker, MeshNeedsUpdate, Voronoi},
|
||||
time::GameTime,
|
||||
ui::CurrentAction,
|
||||
};
|
||||
|
||||
use super::{Cell, CellKind, WealthType};
|
||||
|
||||
pub fn on_click(
|
||||
trigger: Trigger<Pointer<Click>>,
|
||||
mut cells: Query<(&mut Cell, &Parent)>,
|
||||
voronoi: Res<Voronoi>,
|
||||
mut chunks: Query<&mut MeshNeedsUpdate, With<MeshMarker>>,
|
||||
mut cmds: Commands,
|
||||
ca: Res<CurrentAction>,
|
||||
gt: Res<GameTime>,
|
||||
) {
|
||||
if trigger.duration > Duration::from_millis(200) {
|
||||
return;
|
||||
}
|
||||
let (mut cell, parent) = cells.get_mut(trigger.target).unwrap();
|
||||
match *ca {
|
||||
CurrentAction::ChangeCell(ck) => match ck {
|
||||
CellKind::Forest { .. } => match cell.kind {
|
||||
CellKind::Dirt | CellKind::Grass { .. } => {
|
||||
cell.kind = CellKind::Forest {
|
||||
wealth: WealthType::MAX / 2,
|
||||
};
|
||||
chunks.get_mut(parent.get()).unwrap().0 = true;
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
CellKind::Grass { .. } => match cell.kind {
|
||||
CellKind::Dirt => {
|
||||
cell.kind = CellKind::Grass {
|
||||
wealth: WealthType::MAX / 2,
|
||||
};
|
||||
chunks.get_mut(parent.get()).unwrap().0 = true;
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
_ => {}
|
||||
},
|
||||
CurrentAction::AddAnimal(ak) => {
|
||||
if cell.kind.can_place_animal(ak) {
|
||||
let v_cell = voronoi.0.cell(cell.voronoi_id);
|
||||
let cell_pos = v_cell.site_position();
|
||||
cmds.spawn((
|
||||
Animal { kind: ak },
|
||||
Transform::from_xyz(cell_pos.x as f32, cell_pos.y as f32, 0.),
|
||||
));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
@ -8,7 +8,7 @@ use bevy::{
|
||||
};
|
||||
use voronoice::Point;
|
||||
|
||||
use super::{CellsEntities, MapMarker, Voronoi};
|
||||
use super::{CellsEntities, MeshMarker, Voronoi};
|
||||
use crate::camera::CameraMarker;
|
||||
|
||||
pub fn picking_backend(
|
||||
|
@ -7,7 +7,7 @@ impl bevy::prelude::Plugin for Plugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.insert_resource(GameTime {
|
||||
current: Duration::ZERO,
|
||||
speed: 24. * 60. * 60. * 10.,
|
||||
speed: 24. * 60. * 60. * 10. * 10.,
|
||||
})
|
||||
.add_systems(PreUpdate, update_time);
|
||||
}
|
||||
|
@ -128,11 +128,11 @@ fn setup(mut world: Commands, asset_server: Res<AssetServer>) {
|
||||
BackgroundColor(TABBAR_COLOR);
|
||||
.observe(move |trigger: Trigger<Pointer<Click>>, mut ca: ResMut<CurrentAction>, mut bg: Query<&mut BackgroundColor>| {
|
||||
if trigger.button == PointerButton::Primary {
|
||||
if *ca == CurrentAction::ChangeCell(CellKind::Forest) {
|
||||
if matches!(*ca, CurrentAction::ChangeCell(CellKind::Forest {..})) {
|
||||
*ca = CurrentAction::None;
|
||||
bg.get_mut(forest).unwrap().0 = TABBAR_COLOR;
|
||||
} else {
|
||||
*ca = CurrentAction::ChangeCell(CellKind::Forest);
|
||||
*ca = CurrentAction::ChangeCell(CellKind::Forest {wealth: 0});
|
||||
bg.get_mut(forest).unwrap().0 = ENABLED_BUTTON_COLOR;
|
||||
}
|
||||
bg.get_mut(grass).unwrap().0 = TABBAR_COLOR;
|
||||
@ -149,11 +149,11 @@ fn setup(mut world: Commands, asset_server: Res<AssetServer>) {
|
||||
BackgroundColor(TABBAR_COLOR);
|
||||
.observe(move |trigger: Trigger<Pointer<Click>>, mut ca: ResMut<CurrentAction>, mut bg: Query<&mut BackgroundColor>| {
|
||||
if trigger.button == PointerButton::Primary {
|
||||
if *ca == CurrentAction::ChangeCell(CellKind::Grass) {
|
||||
if matches!(*ca, CurrentAction::ChangeCell(CellKind::Grass {..})) {
|
||||
*ca = CurrentAction::None;
|
||||
bg.get_mut(grass).unwrap().0 = TABBAR_COLOR;
|
||||
} else {
|
||||
*ca = CurrentAction::ChangeCell(CellKind::Grass);
|
||||
*ca = CurrentAction::ChangeCell(CellKind::Grass {wealth: 0});
|
||||
bg.get_mut(grass).unwrap().0 = ENABLED_BUTTON_COLOR;
|
||||
}
|
||||
bg.get_mut(forest).unwrap().0 = TABBAR_COLOR;
|
||||
|
Loading…
Reference in New Issue
Block a user