use anyhow::Result; use assetpath::AssetPath; use engine::prelude::*; use serde::{Deserialize, Serialize}; use std::{fmt, slice::Iter}; use crate::config::items::ItemSettings; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] pub enum ItemSlots { Helmet, ChestPlate, Belt, Gloves, Boots, Ring, Amulet, MainHand, OffHand, } impl ItemSlots { const COUNT: u32 = 9; pub fn iter() -> Iter<'static, ItemSlots> { use ItemSlots::*; static SLOTS: [ItemSlots; ItemSlots::COUNT as usize] = [ Helmet, ChestPlate, Belt, Gloves, Boots, Ring, Amulet, MainHand, OffHand, ]; SLOTS.iter() } pub fn random() -> Self { let n = Random::range(0, Self::COUNT); n.into() } pub fn get_path<'a>(&self, item_settings: &'a ItemSettings) -> &'a AssetPath { use ItemSlots::*; match self { Helmet => &item_settings.icon_paths.helmet, ChestPlate => &item_settings.icon_paths.chest, Belt => &item_settings.icon_paths.belt, Gloves => &item_settings.icon_paths.gloves, Boots => &item_settings.icon_paths.boots, Ring => &item_settings.icon_paths.ring, Amulet => &item_settings.icon_paths.amulet, MainHand => &item_settings.icon_paths.main_hand, OffHand => &item_settings.icon_paths.off_hand, } } } impl From for ItemSlots { fn from(n: u32) -> Self { use ItemSlots::*; match n { 0 => Helmet, 1 => ChestPlate, 2 => Belt, 3 => Gloves, 4 => Boots, 5 => Ring, 6 => Amulet, 7 => MainHand, 8 => OffHand, _ => panic!("can only convert number below COUNT"), } } } impl std::str::FromStr for ItemSlots { type Err = anyhow::Error; fn from_str(s: &str) -> Result { match s { "Helmet" => Ok(Self::Helmet), "Chest Plate" => Ok(Self::ChestPlate), "Belt" => Ok(Self::Belt), "Gloves" => Ok(Self::Gloves), "Boots" => Ok(Self::Boots), "Ring" => Ok(Self::Ring), "Amulet" => Ok(Self::Amulet), "Main Hand" => Ok(Self::MainHand), "Off Hand" => Ok(Self::OffHand), _ => { return Err(anyhow::Error::msg(format!( "Failed parsing ItemSlots from {}", s ))); } } } } impl fmt::Display for ItemSlots { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { use ItemSlots::*; match self { Helmet => write!(f, "Helmet"), ChestPlate => write!(f, "Chest Plate"), Belt => write!(f, "Belt"), Gloves => write!(f, "Gloves"), Boots => write!(f, "Boots"), Ring => write!(f, "Ring"), Amulet => write!(f, "Amulet"), MainHand => write!(f, "Main Hand"), OffHand => write!(f, "Off Hand"), } } }