check if avatar exists

This commit is contained in:
Max Bradbury 2020-06-24 15:14:41 +01:00
parent 04c0ebca69
commit 4ed5e0bce8
1 changed files with 12 additions and 4 deletions

View File

@ -82,6 +82,7 @@ impl Version {
#[derive(Debug)] #[derive(Debug)]
pub enum NotFound { pub enum NotFound {
Avatar,
Room, Room,
Sprite, Sprite,
Tile, Tile,
@ -114,7 +115,7 @@ pub struct GameHasNoAvatar;
impl Game { impl Game {
#[inline] #[inline]
pub fn from(string: String) -> Result<Game, GameHasNoAvatar> { pub fn from(string: String) -> Result<Game, NotFound> {
let line_endings_crlf = string.contains("\r\n"); let line_endings_crlf = string.contains("\r\n");
let mut string = string; let mut string = string;
if line_endings_crlf { if line_endings_crlf {
@ -168,6 +169,7 @@ impl Game {
let mut tiles: Vec<Tile> = Vec::new(); let mut tiles: Vec<Tile> = Vec::new();
let mut sprites: Vec<Sprite> = Vec::new(); let mut sprites: Vec<Sprite> = Vec::new();
let mut items: Vec<Item> = Vec::new(); let mut items: Vec<Item> = Vec::new();
let mut avatar_exists = false;
for segment in segments { for segment in segments {
if segment.starts_with("# BITSY VERSION") { if segment.starts_with("# BITSY VERSION") {
@ -203,7 +205,11 @@ impl Game {
} else if segment.starts_with("SPR ") { } else if segment.starts_with("SPR ") {
let sprite = Sprite::from(segment); let sprite = Sprite::from(segment);
if sprite.is_ok() { 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 ") { } else if segment.starts_with("ITM ") {
items.push(Item::from(segment)); 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( Ok(
Game { Game {
name, name,