From 4ed5e0bce888727a537b1286a30fd1d743ad6d62 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Wed, 24 Jun 2020 15:14:41 +0100 Subject: [PATCH] check if avatar exists --- src/game.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/game.rs b/src/game.rs index 087c1cb..b89b94f 100644 --- a/src/game.rs +++ b/src/game.rs @@ -82,6 +82,7 @@ impl Version { #[derive(Debug)] pub enum NotFound { + Avatar, Room, Sprite, Tile, @@ -114,7 +115,7 @@ pub struct GameHasNoAvatar; impl Game { #[inline] - pub fn from(string: String) -> Result { + pub fn from(string: String) -> Result { let line_endings_crlf = string.contains("\r\n"); let mut string = string; if line_endings_crlf { @@ -168,6 +169,7 @@ impl Game { let mut tiles: Vec = Vec::new(); let mut sprites: Vec = Vec::new(); let mut items: Vec = Vec::new(); + let mut avatar_exists = false; for segment in segments { if segment.starts_with("# BITSY VERSION") { @@ -203,7 +205,11 @@ impl Game { } else if segment.starts_with("SPR ") { let sprite = Sprite::from(segment); if sprite.is_ok() { - sprites.push(sprite.unwrap()); + let sprite = sprite.unwrap(); + if ! avatar_exists && sprite.id == "A".to_string() { + avatar_exists = true; + } + sprites.push(sprite); } } else if segment.starts_with("ITM ") { items.push(Item::from(segment)); @@ -221,8 +227,10 @@ impl Game { } } - // todo check if SPR A (avatar) exists - + if ! avatar_exists { + return Err(NotFound::Avatar); + } + Ok( Game { name,