From c9c0942daf6473aca42060feb6b4701b1430c9de Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Wed, 19 May 2021 17:51:38 +0100 Subject: [PATCH] "get by name, tag" functions; update todo --- TODO.md | 25 +++++------------ src/entity.rs | 6 ++--- src/lib.rs | 75 +++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 79 insertions(+), 27 deletions(-) diff --git a/TODO.md b/TODO.md index 1792fb3..b93c6b2 100644 --- a/TODO.md +++ b/TODO.md @@ -2,6 +2,11 @@ ## game data structure +add "from_dir" functions for structs + make that function generic? + +-- + what is the distinction between animate and inanimate objects? idea: @@ -12,21 +17,7 @@ idea: * each position could have (optionally) a tile and (optionally) a thing * things have render priority -### colours - -~convert colours to hex~ -* ~accept abcdef, #abcdef, 0xabcdef (inc. uppercase)~ -* ~export as #abcdef~ - -### palettes - -### images - -* ideally support gif/png... - -### parser - -* ~move tests into their respective modules where appropriate~ +* ideally support gif/png for images (are there other indexed colour formats?) ## players @@ -34,10 +25,6 @@ idea: * re-use player avatar drawing function as generic image drawing function * text (how?) * support older graphics adaptors - -### linux - -* ~get working~ ### windows diff --git a/src/entity.rs b/src/entity.rs index a9a837e..f0e61c1 100644 --- a/src/entity.rs +++ b/src/entity.rs @@ -4,9 +4,9 @@ use serde_derive::{Serialize, Deserialize}; #[derive(Debug, Eq, PartialEq)] pub struct Entity { - name: String, - image: String, - tags: Vec, + pub name: String, + pub image: String, + pub tags: Vec, } impl Entity { diff --git a/src/lib.rs b/src/lib.rs index d979ff2..0d5c444 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -112,8 +112,8 @@ pub struct Game { images: Vec, tiles: Vec, entities: Vec, -// variables: Vec, -// triggers: HashMap, + // variables: Vec, + // triggers: HashMap, music: Vec, } @@ -121,9 +121,7 @@ pub struct Game { pub struct GameParseError; impl Game { - pub fn from(path: String) -> Result { - let path = PathBuf::from(path); - + pub fn from(path: PathBuf) -> Result { let mut images = Vec::new(); let mut tiles = Vec::new(); let mut entities = Vec::new(); @@ -200,4 +198,71 @@ impl Game { Ok(Game { config, images, tiles, palettes, music, entities }) } + + // todo Result<&Image>? + pub fn get_image_by_name(&self, name: String) -> Option<&Image> { + for image in self.images.iter() { + if image.name == name { + return Some(&image); + } + } + + None + } + + pub fn get_entities_by_tag(&self, tag: &String) -> Vec<&Entity> { + let mut entities = Vec::new(); + + for entity in self.entities.iter() { + if entity.tags.contains(tag) { + entities.push(entity); + } + } + + entities + } + + // todo Result<&Entity>? + pub fn get_entity_by_name(&self, name: String) -> Option<&Entity> { + for entity in self.entities.iter() { + if entity.name == name { + return Some(&entity); + } + } + + None + } + + // todo Result<&Music>? + pub fn get_music_by_name(&self, name: String) -> Option<&Music> { + for music in self.music.iter() { + if music.name == name { + return Some(&music); + } + } + + None + } + + // todo Result<&Palette>? + pub fn get_palette_by_name(&self, name: String) -> Option<&Palette> { + for palette in self.palettes.iter() { + if palette.name == name { + return Some(&palette); + } + } + + None + } + + // todo Result<&Tile>? + pub fn get_tile_by_name(&self, name: String) -> Option<&Tile> { + for tile in self.tiles.iter() { + if tile.name == name { + return Some(&tile); + } + } + + None + } }