expansion + better grass colors
This commit is contained in:
		
							parent
							
								
									533cb2fae4
								
							
						
					
					
						commit
						6d7567d0ae
					
				@ -16,9 +16,8 @@ pub struct Plugin;
 | 
			
		||||
impl bevy::prelude::Plugin for Plugin {
 | 
			
		||||
    fn build(&self, app: &mut App) {
 | 
			
		||||
        app.add_systems(Startup, setup)
 | 
			
		||||
            .insert_resource(Time::<Fixed>::from_seconds(0.25)) // Time for a day
 | 
			
		||||
            .add_systems(PreUpdate, picking_backend.in_set(PickSet::Backend))
 | 
			
		||||
            .add_systems(Update, (update_map_mesh, cells_regeneration))
 | 
			
		||||
            .add_systems(Update, (update_map_mesh, cells_regeneration, expand))
 | 
			
		||||
            .insert_resource(ClearColor(Color::srgb(0., 0., 1.)))
 | 
			
		||||
            .insert_resource(Seed(thread_rng().gen()));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -1,10 +1,11 @@
 | 
			
		||||
use std::time::Duration;
 | 
			
		||||
 | 
			
		||||
use bevy::prelude::*;
 | 
			
		||||
use rand::{seq::IteratorRandom, thread_rng, Rng};
 | 
			
		||||
 | 
			
		||||
use crate::time::GameTime;
 | 
			
		||||
 | 
			
		||||
use super::{MapMarker, MeshNeedsUpdate};
 | 
			
		||||
use super::{CellsEntities, MapMarker, MeshNeedsUpdate, Voronoi};
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
 | 
			
		||||
pub enum CellKind {
 | 
			
		||||
@ -33,15 +34,6 @@ pub struct Cell {
 | 
			
		||||
    pub vertices: Vec<usize>
 | 
			
		||||
}
 | 
			
		||||
impl Cell {
 | 
			
		||||
    // pub fn new(kind: CellKind, voronoi_id: usize, altitude: u8, moisture: u8, vertices: Vec<usize>) -> Self {
 | 
			
		||||
    //     Self {
 | 
			
		||||
    //         kind,
 | 
			
		||||
    //         voronoi_id,
 | 
			
		||||
    //         altitude,
 | 
			
		||||
    //         moisture,
 | 
			
		||||
    //         vertices
 | 
			
		||||
    //     }
 | 
			
		||||
    // }
 | 
			
		||||
    pub fn color(&self, wealth: u8) -> [f32; 4] {
 | 
			
		||||
        match self.kind {
 | 
			
		||||
            CellKind::Sea => [0., 0., 1., 1.],
 | 
			
		||||
@ -49,7 +41,7 @@ impl Cell {
 | 
			
		||||
            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::Stone => [0.5, 0.5, 0.5, 1.],
 | 
			
		||||
            CellKind::Grass => [(136./255.) - (wealth as f32/255.*0.4), (204./255.) - (wealth as f32/255.*0.4), (59./255.) - (wealth as f32/255.*0.4), 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), 1.]
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -83,4 +75,53 @@ pub fn cells_regeneration(
 | 
			
		||||
            map_needs_update.0 = true;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn expand(
 | 
			
		||||
    mut cells: Query<(&mut Cell, Option<&Wealth>)>,
 | 
			
		||||
    map: Query<(&Voronoi, &CellsEntities)>,
 | 
			
		||||
    mut cmds: Commands,
 | 
			
		||||
    t: Res<Time>,
 | 
			
		||||
    gt: Res<GameTime>
 | 
			
		||||
) {
 | 
			
		||||
    let (voronoi, cells_entities) = map.single();
 | 
			
		||||
    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((t.elapsed_secs_f64()*(gt.speed as f64)*wealth/(60.*60.*24.*30.)).min(1.)) { // Let say that grass takes 1 months to expand
 | 
			
		||||
                let target = voronoi.0.cell(cell.voronoi_id).iter_neighbors().choose(&mut random).unwrap();
 | 
			
		||||
                changes.push((target, CellKind::Grass));
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        if cell.kind == CellKind::Forest {
 | 
			
		||||
            if random.gen_bool((t.elapsed_secs_f64()*(gt.speed as f64)*(wealth.unwrap().0 as f64/255.).sqrt()/(60.*60.*24.*365.*5.)).min(1.)) { // Let say that forest takes 5 years to expand
 | 
			
		||||
                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
 | 
			
		||||
            }));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -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. * 365.
 | 
			
		||||
                speed: 24. * 60. * 60. * 10.
 | 
			
		||||
            })
 | 
			
		||||
            .add_systems(PreUpdate, update_time);
 | 
			
		||||
    }
 | 
			
		||||
@ -16,7 +16,7 @@ impl bevy::prelude::Plugin for Plugin {
 | 
			
		||||
#[derive(Resource)]
 | 
			
		||||
pub struct GameTime {
 | 
			
		||||
    pub current: Duration,
 | 
			
		||||
    speed: f32 // = game time / real time
 | 
			
		||||
    pub speed: f32 // = game time / real time
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn update_time(
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user