Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Deref for Tile<'tileset> #191

Merged
merged 2 commits into from
Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn main() {
)
.unwrap();
println!("{:?}", map);
println!("{:?}", map.tilesets()[0].get_tile(0).unwrap().probability());
println!("{:?}", map.tilesets()[0].get_tile(0).unwrap().probability);
}

```
Expand Down
2 changes: 1 addition & 1 deletion src/layers/tile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl LayerTileData {

/// Get the layer tile's local id within its parent tileset.
#[inline]
pub fn id(&self) -> u32 {
pub fn id(&self) -> TileId {
self.id
}

Expand Down
54 changes: 20 additions & 34 deletions src/tile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@ use crate::{
/// A tile ID, local to a tileset.
pub type TileId = u32;

/// Raw data belonging to a tile.
#[derive(Debug, PartialEq, Clone, Default)]
pub(crate) struct TileData {
image: Option<Image>,
properties: Properties,
collision: Option<ObjectLayerData>,
animation: Option<Vec<Frame>>,
tile_type: Option<String>,
probability: f32,
pub struct TileData {
/// The image of the tile. Only set when the tile is part of an "image collection" tileset.
pub image: Option<Image>,
/// The custom properties of this tile.
pub properties: Properties,
/// The collision shapes of this tile.
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.
Expand All @@ -41,35 +48,14 @@ impl<'tileset> Tile<'tileset> {
pub fn tileset(&self) -> &'tileset Tileset {
self.tileset
}
}

/// Get a reference to the tile's image.
pub fn image(&self) -> Option<&Image> {
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()
}
impl<'tileset> std::ops::Deref for Tile<'tileset> {
type Target = TileData;

/// Get the tile's probability.
pub fn probability(&self) -> f32 {
self.data.probability
#[inline]
fn deref(&self) -> &'tileset Self::Target {
self.data
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/tileset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::error::{Error, Result};
use crate::image::Image;
use crate::properties::{parse_properties, Properties};
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.
///
Expand Down Expand Up @@ -53,7 +53,7 @@ pub struct Tileset {
pub image: Option<Image>,

/// 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.
pub properties: Properties,
Expand Down Expand Up @@ -124,13 +124,13 @@ impl Tileset {

/// Gets the tile with the specified ID from the tileset.
#[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))
}

/// Iterates through the tiles from this tileset.
#[inline]
pub fn tiles(&self) -> impl ExactSizeIterator<Item = (u32, Tile)> {
pub fn tiles(&self) -> impl ExactSizeIterator<Item = (TileId, Tile)> {
self.tiles
.iter()
.map(move |(id, data)| (*id, Tile::new(self, data)))
Expand Down
2 changes: 1 addition & 1 deletion tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ fn test_tile_property() {
let prop_value: String = if let Some(&PropertyValue::StringValue(ref v)) = r.tilesets()[0]
.get_tile(1)
.unwrap()
.properties()
.properties
.get("a tile property")
{
v.clone()
Expand Down