From 8e5d55c2a5d46edd0b4a082b7f0b1e3cfb77d362 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Sun, 12 Apr 2020 15:40:40 +0100 Subject: [PATCH] implement scene transitions --- Cargo.toml | 2 +- README.md | 5 ++- src/exit.rs | 95 ++++++++++++++++++++++++++++++++++++++++++++++++----- src/lib.rs | 2 +- src/mock.rs | 2 +- 5 files changed, 91 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2d04ec9..d34ba93 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bitsy-parser" -version = "0.65.1" +version = "0.65.2" authors = ["Max Bradbury "] edition = "2018" description = "A parser for Bitsy game data" diff --git a/README.md b/README.md index 0033c43..8ecd9da 100644 --- a/README.md +++ b/README.md @@ -6,10 +6,9 @@ the version number follows Bitsy itself, so version 0.65.* targets Bitsy 6.5. ## todo -### handle fancy exits +### failing tests -* bidirectional? -* fancy animations +test_room_from_string shows an unexpected ordering for the items in the output of room::to_string ### tidy up diff --git a/src/exit.rs b/src/exit.rs index ce477d6..9de25a9 100644 --- a/src/exit.rs +++ b/src/exit.rs @@ -1,26 +1,79 @@ use crate::Position; +use std::iter::Enumerate; + +#[derive(Debug, Eq, PartialEq)] +pub(crate) enum Transition { + None, + FadeToWhite, + FadeToBlack, + Wave, + Tunnel, + SlideUp, + SlideDown, + SlideLeft, + SlideRight, +} + +impl From<&str> for Transition { + fn from(str: &str) -> Transition { + match str { + "fade_w" => Transition::FadeToWhite, + "fade_b" => Transition::FadeToBlack, + "wave" => Transition::Wave, + "tunnel" => Transition::Tunnel, + "slide_u" => Transition::SlideUp, + "slide_d" => Transition::SlideDown, + "slide_l" => Transition::SlideLeft, + "slide_r" => Transition::SlideRight, + _ => Transition::None, + } + } +} + +impl ToString for Transition { + fn to_string(&self) -> String { + match &self { + Transition::FadeToWhite => " FX fade_w", + Transition::FadeToBlack => " FX fade_b", + Transition::Wave => " FX wave", + Transition::Tunnel => " FX tunnel", + Transition::SlideUp => " FX slide_u", + Transition::SlideDown => " FX slide_d", + Transition::SlideLeft => " FX slide_l", + Transition::SlideRight => " FX slide_r", + Transition::None => "", + }.to_string() + } +} #[derive(Debug, Eq, PartialEq)] pub struct Exit { /// destination pub(crate) room: String, /// id pub(crate) position: Position, + pub(crate) effect: Transition, } impl From for Exit { fn from(string: String) -> Exit { - // e.g. "4 3,3" - let room_position: Vec<&str> = string.split(' ').collect(); - let room = room_position[0].to_string(); - let position = Position::from(room_position[1].to_string()); + // e.g. "EXT 6,4 0 10,12 FX fade_w" + let room_position_effect: Vec<&str> = string.split_whitespace().collect(); + let room = room_position_effect[0].to_string(); + let position = Position::from(room_position_effect[1].to_string()); - Exit { room, position } + let effect = if room_position_effect.len() == 4 { + Transition::from(room_position_effect[3]) + } else { + Transition::None + }; + + Exit { room, position, effect } } } impl ToString for Exit { fn to_string(&self) -> String { - format!("{} {}", self.room, self.position.to_string()) + format!("{} {}{}", self.room, self.position.to_string(), self.effect.to_string()) } } @@ -28,14 +81,38 @@ impl ToString for Exit { fn test_exit_from_string() { assert_eq!( Exit::from("a 12,13".to_string()), - Exit { room: "a".to_string(), position: Position { x: 12, y: 13 } } + Exit { room: "a".to_string(), position: Position { x: 12, y: 13 }, effect: Transition::None } + ); +} + +#[test] +fn test_exit_from_string_with_fx() { + assert_eq!( + Exit::from("a 12,13 FX slide_u".to_string()), + Exit { room: "a".to_string(), position: Position { x: 12, y: 13 }, effect: Transition::SlideUp } ); } #[test] fn test_exit_to_string() { assert_eq!( - Exit { room: "8".to_string(), position: Position { x: 5, y: 6 } }.to_string(), + Exit { + room: "8".to_string(), + position: Position { x: 5, y: 6 }, + effect: Transition::None + }.to_string(), "8 5,6".to_string() ); -} \ No newline at end of file +} + +#[test] +fn test_exit_to_string_with_fx() { + assert_eq!( + Exit { + room: "8".to_string(), + position: Position { x: 5, y: 6 }, + effect: Transition::FadeToWhite + }.to_string(), + "8 5,6 FX fade_w".to_string() + ); +} diff --git a/src/lib.rs b/src/lib.rs index 47672b1..9d8e143 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,7 +18,7 @@ use avatar::Avatar; use colour::Colour; use dialogue::Dialogue; use ending::Ending; -use exit::Exit; +use exit::{Transition, Exit}; use game::Game; use palette::Palette; use image::Image; diff --git a/src/mock.rs b/src/mock.rs index e158b0c..f082eab 100644 --- a/src/mock.rs +++ b/src/mock.rs @@ -149,7 +149,7 @@ pub fn room() -> Room { exits: vec![ ExitInstance { position: Position { x: 3, y: 3}, - exit: Exit { room: "3".to_string(), position: Position { x: 10, y: 6}} + exit: Exit { room: "3".to_string(), position: Position { x: 10, y: 6}, effect: Transition::None} }, ], endings: vec![