diff --git a/src/game.rs b/src/game.rs index 8c9ef82..84fb606 100644 --- a/src/game.rs +++ b/src/game.rs @@ -680,6 +680,10 @@ impl Game { new_unique_id(self.variable_ids()) } + pub fn get_palette(&self, id: &str) -> Option<&Palette> { + self.palettes.iter().find(|palette| palette.id == id) + } + /// todo refactor? pub fn get_tile_id(&self, matching_tile: &Tile) -> Option { for tile in &self.tiles { @@ -858,7 +862,7 @@ impl Game { #[cfg(test)] mod test { - use crate::{TextDirection, Font, Version, Game, Tile, Image}; + use crate::{TextDirection, Font, Version, Game, Tile, Image, Palette, Colour}; #[test] fn game_from_string() { @@ -1086,4 +1090,22 @@ mod test { assert_eq!(Game::from("".to_string() ).unwrap_err(), crate::error::NotFound::Anything); assert_eq!(Game::from(" \n \r\n".to_string()).unwrap_err(), crate::error::NotFound::Anything); } + + #[test] + fn get_palette() { + let mut game = crate::mock::game_default(); + let new_palette = Palette { + id: "1".to_string(), + name: Some("sadness".to_string()), + colours: vec![ + Colour { red: 133, green: 131, blue: 111 }, + Colour { red: 105, green: 93, blue: 104 }, + Colour { red: 62, green: 74, blue: 76 }, + ] + }; + game.add_palette(new_palette.clone()); + assert_eq!(game.get_palette("0").unwrap(), &crate::mock::game_default().palettes[0]); + assert_eq!(game.get_palette("1").unwrap(), &new_palette); + assert_eq!(game.get_palette("2"), None); + } }