Skip to content
Snippets Groups Projects
Unverified Commit 3614aad0 authored by Thorbjørn Lindeijer's avatar Thorbjørn Lindeijer Committed by GitHub
Browse files

Implement Deref for Tile<'tileset> (#191)

Also use the TileId alias in a few more places.
parent 377d748b
No related branches found
No related tags found
No related merge requests found
...@@ -27,7 +27,7 @@ fn main() { ...@@ -27,7 +27,7 @@ fn main() {
) )
.unwrap(); .unwrap();
println!("{:?}", map); println!("{:?}", map);
println!("{:?}", map.tilesets()[0].get_tile(0).unwrap().probability()); println!("{:?}", map.tilesets()[0].get_tile(0).unwrap().probability);
} }
``` ```
......
...@@ -46,7 +46,7 @@ impl LayerTileData { ...@@ -46,7 +46,7 @@ impl LayerTileData {
/// Get the layer tile's local id within its parent tileset. /// Get the layer tile's local id within its parent tileset.
#[inline] #[inline]
pub fn id(&self) -> u32 { pub fn id(&self) -> TileId {
self.id self.id
} }
......
...@@ -15,14 +15,21 @@ use crate::{ ...@@ -15,14 +15,21 @@ use crate::{
/// A tile ID, local to a tileset. /// A tile ID, local to a tileset.
pub type TileId = u32; pub type TileId = u32;
/// Raw data belonging to a tile.
#[derive(Debug, PartialEq, Clone, Default)] #[derive(Debug, PartialEq, Clone, Default)]
pub(crate) struct TileData { pub struct TileData {
image: Option<Image>, /// The image of the tile. Only set when the tile is part of an "image collection" tileset.
properties: Properties, pub image: Option<Image>,
collision: Option<ObjectLayerData>, /// The custom properties of this tile.
animation: Option<Vec<Frame>>, pub properties: Properties,
tile_type: Option<String>, /// The collision shapes of this tile.
probability: f32, pub collision: Option<ObjectLayerData>,
/// The animation frames of this tile.
pub animation: Option<Vec<Frame>>,
/// The type of this tile.
pub tile_type: Option<String>,
/// The probability of this tile.
pub probability: f32,
} }
/// Points to a tile belonging to a tileset. /// Points to a tile belonging to a tileset.
...@@ -41,35 +48,14 @@ impl<'tileset> Tile<'tileset> { ...@@ -41,35 +48,14 @@ impl<'tileset> Tile<'tileset> {
pub fn tileset(&self) -> &'tileset Tileset { pub fn tileset(&self) -> &'tileset Tileset {
self.tileset self.tileset
} }
}
/// Get a reference to the tile's image. impl<'tileset> std::ops::Deref for Tile<'tileset> {
pub fn image(&self) -> Option<&Image> { type Target = TileData;
self.data.image.as_ref()
}
/// Get a reference to the tile's properties.
pub fn properties(&self) -> &Properties {
&self.data.properties
}
/// Get a reference to the tile's collision.
pub fn collision(&self) -> Option<&ObjectLayerData> {
self.data.collision.as_ref()
}
/// Get a reference to the tile's animation frames.
pub fn animation(&self) -> Option<&[Frame]> {
self.data.animation.as_ref().map(Vec::as_slice)
}
/// Get a reference to the tile's type.
pub fn tile_type(&self) -> Option<&str> {
self.data.tile_type.as_deref()
}
/// Get the tile's probability. #[inline]
pub fn probability(&self) -> f32 { fn deref(&self) -> &'tileset Self::Target {
self.data.probability self.data
} }
} }
......
...@@ -10,7 +10,7 @@ use crate::error::{Error, Result}; ...@@ -10,7 +10,7 @@ use crate::error::{Error, Result};
use crate::image::Image; use crate::image::Image;
use crate::properties::{parse_properties, Properties}; use crate::properties::{parse_properties, Properties};
use crate::tile::TileData; use crate::tile::TileData;
use crate::{util::*, Gid, Tile}; use crate::{util::*, Gid, Tile, TileId};
/// A collection of tiles for usage in maps and template objects. /// A collection of tiles for usage in maps and template objects.
/// ///
...@@ -53,7 +53,7 @@ pub struct Tileset { ...@@ -53,7 +53,7 @@ pub struct Tileset {
pub image: Option<Image>, pub image: Option<Image>,
/// All the tiles present in this tileset, indexed by their local IDs. /// All the tiles present in this tileset, indexed by their local IDs.
tiles: HashMap<u32, TileData>, tiles: HashMap<TileId, TileData>,
/// The custom properties of the tileset. /// The custom properties of the tileset.
pub properties: Properties, pub properties: Properties,
...@@ -124,13 +124,13 @@ impl Tileset { ...@@ -124,13 +124,13 @@ impl Tileset {
/// Gets the tile with the specified ID from the tileset. /// Gets the tile with the specified ID from the tileset.
#[inline] #[inline]
pub fn get_tile(&self, id: u32) -> Option<Tile> { pub fn get_tile(&self, id: TileId) -> Option<Tile> {
self.tiles.get(&id).map(|data| Tile::new(self, data)) self.tiles.get(&id).map(|data| Tile::new(self, data))
} }
/// Iterates through the tiles from this tileset. /// Iterates through the tiles from this tileset.
#[inline] #[inline]
pub fn tiles(&self) -> impl ExactSizeIterator<Item = (u32, Tile)> { pub fn tiles(&self) -> impl ExactSizeIterator<Item = (TileId, Tile)> {
self.tiles self.tiles
.iter() .iter()
.map(move |(id, data)| (*id, Tile::new(self, data))) .map(move |(id, data)| (*id, Tile::new(self, data)))
......
...@@ -192,7 +192,7 @@ fn test_tile_property() { ...@@ -192,7 +192,7 @@ fn test_tile_property() {
let prop_value: String = if let Some(&PropertyValue::StringValue(ref v)) = r.tilesets()[0] let prop_value: String = if let Some(&PropertyValue::StringValue(ref v)) = r.tilesets()[0]
.get_tile(1) .get_tile(1)
.unwrap() .unwrap()
.properties() .properties
.get("a tile property") .get("a tile property")
{ {
v.clone() v.clone()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment