๐Ÿ“ฆ djberube / xaxocolony

๐Ÿ“„ seasons.rs ยท 50 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50use crate::prelude::*;

// Create Plugin
pub struct SeasonsPlugin;

impl Plugin for SeasonsPlugin {
    fn build(&self, app: &mut App) {
        app
        .add_systems(
            Update,
            seasons_system
            .run_if(bevy::time::common_conditions::on_timer(bevy::utils::Duration::from_secs_f32(2.0)))
            .run_if(in_state(GameState::InGame))
        )
        ;
    }
}

pub fn seasons_system(
    mut commands: Commands,
    mut plants: Query<(Entity, &mut Plant, &mut Transform, Option<&Foragable>, Option<&Choppable>)>,
) {
    for (entity, mut plant, mut transform, foragable, choppable) in plants.iter_mut() {
        if plant.growth < 1.0 {
            let rand = rand::thread_rng().gen_range(0..2);
            let base_growth_speed = plant.plant_type.growth_speed();
            plant.growth += match rand { 0 => 3.0 * base_growth_speed, 1 => base_growth_speed, _ => 0.0 };
            transform.scale = Vec3::new(plant.growth, plant.growth, 1.0);
            if plant.growth >= 0.5 {
                // Is plant one that is typically edible?
                if plant.plant_type.is_forageable().0.is_some() && foragable.is_none() {
                    commands.entity(entity).insert(Foragable);
                }
                if plant.plant_type.is_choppable().0.is_some() && choppable.is_none() {
                    commands.entity(entity).insert(Choppable);
                }
            }
        } else {
            plant.growth += 0.01;
            if plant.growth > 1.01 {
                let mut rng = rand::thread_rng();
                let death = rng.gen_range(0..100);
                if death < 2 {
                    //commands.entity(entity).despawn();
                    plant.growth = 0.01;
                }
            }
        }
    }
}