use std::fmt::Display; use crate::{ DocumentElement, Gamedata, Hand, ItemIndex, ItemLocation, PlayerID, Recipe, RecipeIndex, TileIndex, }; impl Gamedata { pub fn tile_name(&self, index: TileIndex) -> &String { &self.tile_names[index.0] } pub fn is_tile_colliding(&self, index: TileIndex) -> bool { self.tile_collide[index.0] } pub fn is_tile_interactable(&self, index: TileIndex) -> bool { self.tile_interact[index.0] } pub fn item_name(&self, index: ItemIndex) -> &String { &self.item_names[index.0] } pub fn recipe(&self, index: RecipeIndex) -> &Recipe { &self.recipes[index.0] } pub fn get_tile_by_name(&self, name: &str) -> Option { self.tile_names .iter() .position(|t| t == name) .map(TileIndex) } pub fn get_item_by_name(&self, name: &str) -> Option { self.item_names .iter() .position(|t| t == name) .map(ItemIndex) } pub fn recipes(&self) -> impl Iterator { self.recipes .iter() .enumerate() .map(|(i, e)| (RecipeIndex(i), e)) } } impl Recipe { pub fn tile(&self) -> Option { match self { Recipe::Passive { tile, .. } => *tile, Recipe::Active { tile, .. } => *tile, Recipe::Instant { tile, .. } => *tile, } } pub fn speed(&self) -> Option { match self { Recipe::Passive { speed, .. } => Some(*speed), Recipe::Active { speed, .. } => Some(*speed), _ => None, } } pub fn revert_speed(&self) -> Option { match self { Recipe::Passive { revert_speed, .. } => *revert_speed, _ => None, } } pub fn warn(&self) -> bool { match self { Recipe::Passive { warn, .. } => *warn, _ => false, } } pub fn inputs(&self) -> Vec { match self { Recipe::Passive { input, .. } => vec![*input], Recipe::Active { input, .. } => vec![*input], Recipe::Instant { inputs, .. } => inputs.iter().flat_map(|e| e.to_owned()).collect(), } } pub fn outputs(&self) -> Vec { match self { Recipe::Passive { output, .. } => output.iter().copied().collect(), Recipe::Active { outputs, .. } => outputs.iter().flat_map(|e| e.to_owned()).collect(), Recipe::Instant { outputs, .. } => outputs.iter().flat_map(|e| e.to_owned()).collect(), } } pub fn supports_tile(&self, tile: Option) -> bool { if let Some(tile_constraint) = self.tile() { if let Some(tile) = tile { tile == tile_constraint } else { false } } else { true } } } impl Display for ItemLocation { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { ItemLocation::Tile(pos) => write!(f, "tile({pos})"), ItemLocation::Player(PlayerID(id), hand) => write!(f, "player({id}_{hand})"), } } } impl Display for Hand { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(match self { Hand::Left => "left", Hand::Right => "right", }) } } impl Hand { pub fn index(&self) -> usize { match self { Hand::Left => 0, Hand::Right => 1, } } pub fn from_index(i: usize) -> Self { match i { 0 => Hand::Left, 1 => Hand::Right, _ => Hand::Left, } } } impl Default for DocumentElement { fn default() -> Self { Self::Document { es: vec![] } } }