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

Add support for TSX tilesets #429

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ bevy = { version = "0.10", default-features = false, features = [
"bevy_asset",
"bevy_sprite",
] }

futures-lite = "1.13.0"
log = "0.4"
regex = "1.5.4"

Expand Down
36 changes: 22 additions & 14 deletions examples/helpers/tiled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
// * 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::Cursor;
use std::io::{Cursor, Error, ErrorKind, Read};
use std::path::Path;
use std::sync::Arc;

use bevy::{
asset::{AssetLoader, AssetPath, LoadedAsset},
asset::{AssetIo, AssetLoader, AssetPath, LoadedAsset},
log,
prelude::{
AddAsset, Added, AssetEvent, Assets, Bundle, Commands, Component, DespawnRecursiveExt,
Expand Down Expand Up @@ -67,25 +67,33 @@ pub struct TiledMapBundle {
pub global_transform: GlobalTransform,
}

struct BytesResourceReader {
struct BytesResourceReader<'a> {
bytes: Arc<[u8]>,
asset_io: &'a dyn AssetIo,
}

impl BytesResourceReader {
fn new(bytes: &[u8]) -> Self {
impl<'a> BytesResourceReader<'a> {
pub fn new(bytes: &'a [u8], asset_io: &'a dyn AssetIo) -> Self {
Self {
bytes: Arc::from(bytes),
asset_io,
}
}
}

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()))
impl<'a> tiled::ResourceReader for BytesResourceReader<'a> {
type Resource = Box<dyn Read + 'a>;
type Error = Error;

fn read_from(&mut self, path: &Path) -> std::result::Result<Self::Resource, Self::Error> {
if let Some(extension) = path.extension() {
if extension == "tsx" {
let future = self.asset_io.load_path(path);
let data = futures_lite::future::block_on(future)
.map_err(|err| Error::new(ErrorKind::NotFound, err))?;
return Ok(Box::new(Cursor::new(data)));
}
}
Ok(Box::new(Cursor::new(self.bytes.clone())))
}
}

Expand All @@ -107,7 +115,7 @@ impl AssetLoader for TiledLoader {

let mut loader = tiled::Loader::with_cache_and_reader(
tiled::DefaultResourceCache::new(),
BytesResourceReader::new(bytes),
BytesResourceReader::new(bytes, load_context.asset_io()),
);
let map = loader
.load_tmx_map(load_context.path())
Expand Down