use crate::components::CellState;
#[cfg(feature = "auto-coloring")]
use bevy::prelude::Color;
use bevy::prelude::Component;
use std::fmt::Debug;
#[derive(Debug, Copy, Clone, PartialEq, Component, Default)]
#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
pub enum RainbowCellState {
#[default]
Dead,
Alive(f32),
}
impl CellState for RainbowCellState {
fn new_cell_state<'a>(&self, neighbor_cells: impl Iterator<Item = &'a Self>) -> Self {
let alive_cells: Vec<f32> = neighbor_cells
.filter_map(|c| match c {
Self::Dead => None,
Self::Alive(s) => Some(*s),
})
.collect();
let alive_cells_count = alive_cells.len();
match (self, alive_cells_count) {
(Self::Alive(_), 2 | 3) => *self,
(Self::Dead, 3) => {
let val: f32 = alive_cells.into_iter().sum::<f32>() / 3.;
Self::Alive(val)
}
_ => Self::Dead,
}
}
#[cfg(feature = "auto-coloring")]
fn color(&self) -> Option<Color> {
match self {
Self::Dead => None,
Self::Alive(v) => Some(Color::srgb(*v, *v, *v)),
}
}
}
impl RainbowCellState {
#[must_use]
#[inline]
pub const fn is_alive(&self) -> bool {
matches!(self, Self::Alive(_))
}
}