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

Update Tiled crate and related example to 0.11.0 #425

Merged
merged 1 commit into from
May 13, 2023
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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ ldtk_rust = { version = "0.6" }
rand = "0.8"
env_logger = "0.9"
serde_json = { version = "1.0" }
tiled = { version = "0.10.2", default-features = false }
tiled = { version = "0.11.0", default-features = false }

[dev-dependencies.bevy]
version = "0.10"
Expand Down
35 changes: 31 additions & 4 deletions examples/helpers/tiled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
// * When the 'atlas' feature is enabled tilesets using a collection of images will be skipped.
// * Only finite tile layers are loaded. Infinite tile layers and object layers will be skipped.

use std::io::BufReader;
use std::io::Cursor;
use std::path::Path;
use std::sync::Arc;

use bevy::{
asset::{AssetLoader, AssetPath, LoadedAsset},
Expand Down Expand Up @@ -65,6 +67,28 @@ pub struct TiledMapBundle {
pub global_transform: GlobalTransform,
}

struct BytesResourceReader {
bytes: Arc<[u8]>,
}

impl BytesResourceReader {
fn new(bytes: &[u8]) -> Self {
Self {
bytes: Arc::from(bytes),
}
}
}

impl tiled::ResourceReader for BytesResourceReader {
type Resource = Cursor<Arc<[u8]>>;
type Error = std::io::Error;

fn read_from(&mut self, _path: &Path) -> std::result::Result<Self::Resource, Self::Error> {
// In this case, the path is ignored because the byte data is already provided.
Ok(Cursor::new(self.bytes.clone()))
}
}

pub struct TiledLoader;

impl AssetLoader for TiledLoader {
Expand All @@ -81,9 +105,12 @@ impl AssetLoader for TiledLoader {
.parent()
.expect("The asset load context was empty.");

let mut loader = tiled::Loader::new();
let mut loader = tiled::Loader::with_cache_and_reader(
tiled::DefaultResourceCache::new(),
BytesResourceReader::new(bytes),
);
let map = loader
.load_tmx_map_from(BufReader::new(bytes), load_context.path())
.load_tmx_map(load_context.path())
.map_err(|e| anyhow::anyhow!("Could not load TMX map: {e}"))?;

let mut dependencies = Vec::new();
Expand Down Expand Up @@ -232,7 +259,7 @@ pub fn process_loaded_maps(
let offset_x = layer.offset_x;
let offset_y = layer.offset_y;

let tiled::LayerType::TileLayer(tile_layer) = layer.layer_type() else {
let tiled::LayerType::Tiles(tile_layer) = layer.layer_type() else {
log::info!(
"Skipping layer {} because only tile layers are supported.",
layer.id()
Expand Down