treevolution/sim_mod/
cell_types.rs

1use macroquad::color::*;
2use crate::constants::simulation::LEAF_ABSORB_RATE;
3use crate::sim_mod::cell_types::CellType::{Leaf, Empty, Trunk, Seed, Dead};
4use crate::traits::color_convert::ColorConvert;
5
6
7const DEAD_LEAF_COLOR: Color = Color {
8    r: 0.729,
9    g: 0.557,
10    b: 0.137,
11    a: 1.,
12};
13const DEAD_CELL_COLOR: Color = Color {
14    r: 0.498,
15    g: 0.439,
16    b: 0.325,
17    a: 1.,
18};
19#[derive(Copy, Clone)]
20pub enum CellType {
21    Empty,
22    Leaf { sun_absorbed: f32},
23    Trunk {root_connection: f32},
24    Dead,
25    Seed
26}
27
28impl CellType {
29    pub fn new_leaf() -> CellType {
30        Leaf {
31            sun_absorbed: 1.,
32        }
33    }
34    // creates a trunk which has full connection to root
35    pub fn new_root() -> CellType {
36        Trunk {
37            root_connection: 1.
38        }
39    }
40
41    pub fn get_root_con(&self) -> f32 {
42        if let Trunk { root_connection, .. } = self {
43            *root_connection
44        } else {
45            panic!("Tried to get root con from a non Trunk CellType");
46        }
47    }
48}
49
50impl ColorConvert for CellType {
51    fn get_color(&self) -> Color {
52        match self {
53            Empty => {WHITE}
54            Leaf { mut sun_absorbed } => {
55                sun_absorbed /= LEAF_ABSORB_RATE;
56                Color::new(
57                    LIME.r * sun_absorbed + DEAD_LEAF_COLOR.r * (1.-sun_absorbed),
58                    LIME.g * sun_absorbed + DEAD_LEAF_COLOR.g * (1.-sun_absorbed),
59                    LIME.b * sun_absorbed + DEAD_LEAF_COLOR.b * (1.-sun_absorbed),
60                    1.)
61            }
62            Trunk { .. } => {BROWN}
63            Seed => {YELLOW}
64            Dead => {GRAY}
65        }
66    }
67}