From 75e0869c9a31c572431b51b0f31e909f2ed56264 Mon Sep 17 00:00:00 2001 From: perlindgren <per.lindgren@ltu.se> Date: Wed, 26 Jan 2022 21:32:20 +0100 Subject: [PATCH] Add parallax support (#101) --- CHANGELOG.md | 2 +- assets/tiled_parallax.tmx | 46 +++++++++++++++++++++++++++++++++++++++ src/layers.rs | 8 ++++++- tests/lib.rs | 25 +++++++++++++++++++++ 4 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 assets/tiled_parallax.tmx diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b57687..83a04f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - `Tileset::source` for obtaining where the tileset actually came from. - `Tileset::columns`. -- `Layer::id`, `Layer::width` and `Layer::height`. +- `Layer::id`, `Layer::width`, `Layer::height`, `Layer::parallax_x` and `Layer::parallax_y`. - Support for 'object'-type properties. - Support for multiline string properties. - Documentation for map members. diff --git a/assets/tiled_parallax.tmx b/assets/tiled_parallax.tmx new file mode 100644 index 0000000..c0d811d --- /dev/null +++ b/assets/tiled_parallax.tmx @@ -0,0 +1,46 @@ +<?xml version="1.0" encoding="UTF-8"?> +<map version="1.5" tiledversion="1.7.1" orientation="orthogonal" renderorder="right-down" width="10" height="10" tilewidth="32" tileheight="32" infinite="0" nextlayerid="4" nextobjectid="1"> + <tileset firstgid="1" source="tilesheet.tsx"/> + <layer id="3" name="Background" width="10" height="10" parallaxx="0.5" parallaxy="0.75"> + <data encoding="csv"> +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0, +48,49,50,48,49,50,48,49,50,0, +62,63,64,62,63,64,62,63,64,0, +76,77,78,76,77,78,76,77,78,0, +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0 +</data> + </layer> + <layer id="2" name="Middle" width="10" height="10"> + <data encoding="csv"> +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0, +0,0,6,7,8,0,0,0,0,0, +0,0,20,21,22,0,6,7,8,0, +0,0,34,35,36,0,20,21,22,0, +0,0,0,0,0,0,34,35,36,0, +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0 +</data> + </layer> + <layer id="1" name="Foreground" width="10" height="10" parallaxx="2" parallaxy="2"> + <data encoding="csv"> +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,3,0, +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0 +</data> + </layer> +</map> \ No newline at end of file diff --git a/src/layers.rs b/src/layers.rs index faa5f60..b5c0b65 100644 --- a/src/layers.rs +++ b/src/layers.rs @@ -65,6 +65,8 @@ pub struct Layer { pub visible: bool, pub offset_x: f32, pub offset_y: f32, + pub parallax_x: f32, + pub parallax_y: f32, pub opacity: f32, pub properties: Properties, pub layer_type: LayerType, @@ -78,13 +80,15 @@ impl Layer { infinite: bool, path_relative_to: Option<&Path>, ) -> Result<Self, TiledError> { - let ((opacity, visible, offset_x, offset_y, name, id), ()) = get_attrs!( + let ((opacity, visible, offset_x, offset_y, parallax_x, parallax_y, name, id), ()) = get_attrs!( attrs, optionals: [ ("opacity", opacity, |v:String| v.parse().ok()), ("visible", visible, |v:String| v.parse().ok().map(|x:i32| x == 1)), ("offsetx", offset_x, |v:String| v.parse().ok()), ("offsety", offset_y, |v:String| v.parse().ok()), + ("parallaxx", parallax_x, |v:String| v.parse().ok()), + ("parallaxy", parallax_y, |v:String| v.parse().ok()), ("name", name, |v| Some(v)), ("id", id, |v:String| v.parse().ok()), ], @@ -113,6 +117,8 @@ impl Layer { visible: visible.unwrap_or(true), offset_x: offset_x.unwrap_or(0.0), offset_y: offset_y.unwrap_or(0.0), + parallax_x: parallax_x.unwrap_or(1.0), + parallax_y: parallax_y.unwrap_or(1.0), opacity: opacity.unwrap_or(1.0), name: name.unwrap_or_default(), id: id.unwrap_or(0), diff --git a/tests/lib.rs b/tests/lib.rs index 46bb347..4954b84 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -217,6 +217,31 @@ fn test_ldk_export() { } } +#[test] +fn test_parallax_layers() { + let r = Map::parse_file("assets/tiled_parallax.tmx").unwrap(); + for (i, layer) in r.layers.iter().enumerate() { + match i { + 0 => { + assert_eq!(layer.name, "Background"); + assert_eq!(layer.parallax_x, 0.5); + assert_eq!(layer.parallax_y, 0.75); + } + 1 => { + assert_eq!(layer.name, "Middle"); + assert_eq!(layer.parallax_x, 1.0); + assert_eq!(layer.parallax_y, 1.0); + } + 2 => { + assert_eq!(layer.name, "Foreground"); + assert_eq!(layer.parallax_x, 2.0); + assert_eq!(layer.parallax_y, 2.0); + } + _ => panic!("unexpected layer"), + } + } +} + #[test] fn test_object_property() { let r = parse_map_without_source(&Path::new("assets/tiled_object_property.tmx")).unwrap(); -- GitLab