Skip to content

Commit

Permalink
Add parallax support (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
perlindgren authored Jan 26, 2022
1 parent 968073b commit 75e0869
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
46 changes: 46 additions & 0 deletions assets/tiled_parallax.tmx
Original file line number Diff line number Diff line change
@@ -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>
8 changes: 7 additions & 1 deletion src/layers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()),
],
Expand Down Expand Up @@ -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),
Expand Down
25 changes: 25 additions & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 75e0869

Please sign in to comment.