use clap::ValueEnum;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::fmt::{Display, Formatter};
#[derive(Debug, Copy, Clone)]
pub struct SpeedConfig {
pub ms_value: u64,
pub name: &'static str,
pub score_modifier: u16,
pub symbol: &'static str,
}
const SLOW_CONFIG: SpeedConfig = SpeedConfig {
ms_value: 150,
name: "Slow",
score_modifier: 1,
symbol: "🐢",
};
const NORMAL_CONFIG: SpeedConfig = SpeedConfig {
ms_value: 125,
name: "Normal",
score_modifier: 2,
symbol: "🐍",
};
const FAST_CONFIG: SpeedConfig = SpeedConfig {
ms_value: 100,
name: "Fast",
score_modifier: 3,
symbol: "🐉",
};
const CRAZY_CONFIG: SpeedConfig = SpeedConfig {
ms_value: 80,
name: "Crazy",
score_modifier: 4,
symbol: "🦖",
};
#[derive(Debug, Copy, Clone, Serialize, Deserialize, ValueEnum, Default)]
pub enum Speed {
Slow,
#[default]
Normal,
Fast,
Crazy,
}
impl Display for Speed {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}{}", self.name(), self.symbol())
}
}
impl Speed {
#[must_use]
pub fn config(&self) -> &'static SpeedConfig {
match self {
Speed::Slow => &SLOW_CONFIG,
Speed::Normal => &NORMAL_CONFIG,
Speed::Fast => &FAST_CONFIG,
Speed::Crazy => &CRAZY_CONFIG,
}
}
#[must_use]
pub fn ms_value(&self) -> u64 {
self.config().ms_value
}
#[must_use]
pub fn name(&self) -> &'static str {
self.config().name
}
#[must_use]
pub fn symbol(&self) -> &'static str {
self.config().symbol
}
#[must_use]
pub fn score_modifier(&self) -> u16 {
self.config().score_modifier
}
}