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

Allow opting out of SpriteSheetBundle grid construction #161

Merged
merged 5 commits into from
Feb 9, 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
213 changes: 146 additions & 67 deletions assets/Typical_2D_platformer_example.ldtk

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions examples/platformer/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,13 @@ pub struct ChestBundle {
pub collider_bundle: ColliderBundle,
}

#[derive(Clone, Default, Bundle, LdtkEntity)]
pub struct PumpkinsBundle {
#[sprite_sheet_bundle(no_grid)]
#[bundle]
pub sprite_sheet_bundle: SpriteSheetBundle,
}

#[derive(Clone, Default, Component)]
pub struct GroundDetection {
pub on_ground: bool,
Expand Down
1 change: 1 addition & 0 deletions examples/platformer/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ fn main() {
.register_ldtk_entity::<components::PlayerBundle>("Player")
.register_ldtk_entity::<components::MobBundle>("Mob")
.register_ldtk_entity::<components::ChestBundle>("Chest")
.register_ldtk_entity::<components::PumpkinsBundle>("Pumpkins")
.run();
}
16 changes: 14 additions & 2 deletions macros/src/ldtk_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,24 @@ fn expand_sprite_sheet_bundle_attribute(
},
}
},
syn::Meta::List(syn::MetaList { nested, .. }) if nested.len() == 1 => {
let mut nested_iter = nested.iter();

match nested_iter.next() {
Some(syn::NestedMeta::Meta(syn::Meta::Path(path))) if path.is_ident("no_grid") => {},
_ => panic!("Argument of #[sprite_sheet_bundle(...)] should be no_grid")
};

quote! {
#field_name: bevy_ecs_ldtk::utils::sprite_sheet_bundle_from_entity_info(entity_instance, tileset, tileset_definition, texture_atlases, false),
}
},
syn::Meta::Path(_) => {
quote! {
#field_name: bevy_ecs_ldtk::utils::sprite_sheet_bundle_from_entity_info(entity_instance, tileset, tileset_definition, texture_atlases),
#field_name: bevy_ecs_ldtk::utils::sprite_sheet_bundle_from_entity_info(entity_instance, tileset, tileset_definition, texture_atlases, true),
}
},
_ => panic!("#[sprite_sheet_bundle...] attribute should take the form #[sprite_sheet_bundle(\"asset/path.png\", tile_width, tile_height, columns, rows, padding, offset, index)] or #[sprite_sheet_bundle]"),
_ => panic!("#[sprite_sheet_bundle...] attribute should take the form #[sprite_sheet_bundle(\"asset/path.png\", tile_width, tile_height, columns, rows, padding, offset, index)], #[sprite_sheet_bundle(no_grid)] or #[sprite_sheet_bundle]"),
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/app/ldtk_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ use std::{collections::HashMap, marker::PhantomData};
/// [Bundle]: bevy::prelude::Bundle
/// [App]: bevy::prelude::App
/// [Component]: bevy::prelude::Component
/// [SpriteBundle]: bevy::prelude::SpriteBundle
/// [SpriteSheetBundle]: bevy::prelude::SpriteSheetBundle
/// [TextureAtlas]: bevy::prelude::TextureAtlas
///
/// Provides a constructor which can be used for spawning entities from an LDtk file.
///
Expand Down Expand Up @@ -104,6 +107,10 @@ use std::{collections::HashMap, marker::PhantomData};
/// Similar to using [TextureAtlas::from_grid()].
/// - `#[sprite_sheet_bundle]` will create the field using information from the LDtk Editor visual,
/// if it has one.
/// - `#[sprite_sheet_bundle(no_grid)]` will create the field using information from the LDtk
/// Editor visual, if it has one, but without using a grid. Instead a single texture will be used.
/// This may be useful if the LDtk entity's visual uses a rectangle of tiles from its tileset,
/// but will prevent using the generated [TextureAtlas] for animation purposes.
/// ```
/// # use bevy::prelude::*;
/// # use bevy_ecs_ldtk::prelude::*;
Expand Down Expand Up @@ -346,6 +353,7 @@ impl LdtkEntity for SpriteSheetBundle {
tileset,
tileset_definition,
texture_atlases,
true,
)
}
}
Expand Down
51 changes: 38 additions & 13 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,22 +314,47 @@ pub fn sprite_sheet_bundle_from_entity_info(
tileset: Option<&Handle<Image>>,
tileset_definition: Option<&TilesetDefinition>,
texture_atlases: &mut Assets<TextureAtlas>,
grid: bool,
) -> SpriteSheetBundle {
match (tileset, &entity_instance.tile, tileset_definition) {
(Some(tileset), Some(tile), Some(tileset_definition)) => SpriteSheetBundle {
texture_atlas: texture_atlases.add(TextureAtlas::from_grid(
tileset.clone(),
Vec2::new(tile.w as f32, tile.h as f32),
tileset_definition.c_wid as usize,
tileset_definition.c_hei as usize,
Some(Vec2::splat(tileset_definition.spacing as f32)),
Some(Vec2::splat(tileset_definition.padding as f32)),
)),
sprite: TextureAtlasSprite {
index: (tile.y / (tile.h + tileset_definition.spacing)) as usize
* tileset_definition.c_wid as usize
+ (tile.x / (tile.w + tileset_definition.spacing)) as usize,
..Default::default()
texture_atlas: if grid {
texture_atlases.add(TextureAtlas::from_grid(
tileset.clone(),
Vec2::new(tile.w as f32, tile.h as f32),
tileset_definition.c_wid as usize,
tileset_definition.c_hei as usize,
Some(Vec2::splat(tileset_definition.spacing as f32)),
Some(Vec2::splat(tileset_definition.padding as f32)),
))
} else {
let mut texture_atlas = TextureAtlas::new_empty(
tileset.clone(),
Vec2::new(
tileset_definition.px_wid as f32,
tileset_definition.px_hei as f32,
),
);
texture_atlas.add_texture(Rect::new(
tile.x as f32,
tile.y as f32,
(tile.x + tile.w) as f32,
(tile.y + tile.h) as f32,
));
texture_atlases.add(texture_atlas)
},
sprite: if grid {
TextureAtlasSprite {
index: (tile.y / (tile.h + tileset_definition.spacing)) as usize
* tileset_definition.c_wid as usize
+ (tile.x / (tile.w + tileset_definition.spacing)) as usize,
..Default::default()
}
} else {
TextureAtlasSprite {
index: 0,
..Default::default()
}
},
..Default::default()
},
Expand Down