Skip to content

Commit

Permalink
feat!: use the bundle's Default implementation rather than the fiel…
Browse files Browse the repository at this point in the history
…d's in `LdtkEntity` and `LdtkIntCell` derive macros (#222)

Changes the default behavior to use bundle's struct's `Default` `impl`
instead of each individual's field.

Solves #119.

- [x] I have properly formatted code using `cargo fmt`.
- [x] I have tested the code and documentation i changed by both
crafting and running an example and by running `cargo test`.
- [x] I have ran `cargo clippy` and fixed any issues arising from my
changes.
  • Loading branch information
elenakrittik authored Sep 18, 2023
1 parent 382dea2 commit f003127
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
});
}

#[derive(Bundle, LdtkEntity)]
#[derive(Default, Bundle, LdtkEntity)]
pub struct MyBundle {
a: ComponentA,
b: ComponentB,
Expand Down
2 changes: 1 addition & 1 deletion examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct ComponentA;
#[derive(Default, Component)]
struct ComponentB;

#[derive(Bundle, LdtkEntity)]
#[derive(Default, Bundle, LdtkEntity)]
pub struct MyBundle {
a: ComponentA,
b: ComponentB,
Expand Down
13 changes: 8 additions & 5 deletions macros/src/ldtk_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ static LDTK_ENTITY_ATTRIBUTE_NAME: &str = "ldtk_entity";
static FROM_ENTITY_INSTANCE_ATTRIBUTE_NAME: &str = "from_entity_instance";
static WITH_ATTRIBUTE_NAME: &str = "with";

pub fn expand_ldtk_entity_derive(ast: &syn::DeriveInput) -> proc_macro::TokenStream {
pub fn expand_ldtk_entity_derive(ast: syn::DeriveInput) -> proc_macro::TokenStream {
let struct_name = &ast.ident;

let fields = match &ast.data {
Expand Down Expand Up @@ -96,15 +96,17 @@ pub fn expand_ldtk_entity_derive(ast: &syn::DeriveInput) -> proc_macro::TokenStr
field_constructions.push(expand_with_attribute(attribute, field_name, field_type));
continue;
}

field_constructions.push(quote! {
#field_name: <#field_type as std::default::Default>::default(),
});
}

let generics = &ast.generics;
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

let struct_update = if field_constructions.len() < fields.len() {
quote! { ..<Self as std::default::Default>::default() }
} else {
quote! {}
};

let gen = quote! {
impl #impl_generics bevy_ecs_ldtk::prelude::LdtkEntity for #struct_name #ty_generics #where_clause {
fn bundle_entity(
Expand All @@ -117,6 +119,7 @@ pub fn expand_ldtk_entity_derive(ast: &syn::DeriveInput) -> proc_macro::TokenStr
) -> Self {
Self {
#(#field_constructions)*
#struct_update
}
}
}
Expand Down
13 changes: 8 additions & 5 deletions macros/src/ldtk_int_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ static LDTK_INT_CELL_ATTRIBUTE_NAME: &str = "ldtk_int_cell";
static FROM_INT_GRID_CELL_ATTRIBUTE_NAME: &str = "from_int_grid_cell";
static WITH_ATTRIBUTE_NAME: &str = "with";

pub fn expand_ldtk_int_cell_derive(ast: &syn::DeriveInput) -> proc_macro::TokenStream {
pub fn expand_ldtk_int_cell_derive(ast: syn::DeriveInput) -> proc_macro::TokenStream {
let struct_name = &ast.ident;

let fields = match &ast.data {
Expand Down Expand Up @@ -50,15 +50,17 @@ pub fn expand_ldtk_int_cell_derive(ast: &syn::DeriveInput) -> proc_macro::TokenS
field_constructions.push(expand_with_attribute(attribute, field_name, field_type));
continue;
}

field_constructions.push(quote! {
#field_name: <#field_type as std::default::Default>::default(),
});
}

let generics = &ast.generics;
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

let struct_update = if field_constructions.len() < fields.len() {
quote! { ..<Self as std::default::Default>::default() }
} else {
quote! {}
};

let gen = quote! {
impl #impl_generics bevy_ecs_ldtk::prelude::LdtkIntCell for #struct_name #ty_generics #where_clause {
fn bundle_int_cell(
Expand All @@ -67,6 +69,7 @@ pub fn expand_ldtk_int_cell_derive(ast: &syn::DeriveInput) -> proc_macro::TokenS
) -> Self {
Self {
#(#field_constructions)*
#struct_update
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ mod ldtk_int_cell;
pub fn ldtk_entity_derive(input: TokenStream) -> TokenStream {
let ast = syn::parse(input).unwrap();

ldtk_entity::expand_ldtk_entity_derive(&ast)
ldtk_entity::expand_ldtk_entity_derive(ast)
}

#[proc_macro_derive(LdtkIntCell, attributes(ldtk_int_cell, from_int_grid_cell, with))]
pub fn ldtk_int_cell_derive(input: TokenStream) -> TokenStream {
let ast = syn::parse(input).unwrap();

ldtk_int_cell::expand_ldtk_int_cell_derive(&ast)
ldtk_int_cell::expand_ldtk_int_cell_derive(ast)
}
2 changes: 1 addition & 1 deletion src/app/entity_app_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub trait LdtkEntityAppExt {
/// # struct ComponentB;
/// # #[derive(Component, Default)]
/// # struct ComponentC;
/// #[derive(Bundle, LdtkEntity)]
/// #[derive(Bundle, LdtkEntity, Default)]
/// pub struct MyBundle {
/// a: ComponentA,
/// b: ComponentB,
Expand Down
2 changes: 1 addition & 1 deletion src/app/int_cell_app_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub trait LdtkIntCellAppExt {
/// # struct ComponentB;
/// # #[derive(Component, Default)]
/// # struct ComponentC;
/// #[derive(Bundle, LdtkIntCell)]
/// #[derive(Bundle, LdtkIntCell, Default)]
/// pub struct MyBundle {
/// a: ComponentA,
/// b: ComponentB,
Expand Down
25 changes: 13 additions & 12 deletions src/app/ldtk_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use std::{collections::HashMap, marker::PhantomData};
/// # struct ComponentB;
/// # #[derive(Component, Default)]
/// # struct ComponentC;
/// #[derive(Bundle, LdtkEntity)]
/// #[derive(Bundle, LdtkEntity, Default)]
/// pub struct MyBundle {
/// a: ComponentA,
/// b: ComponentB,
Expand All @@ -60,8 +60,9 @@ use std::{collections::HashMap, marker::PhantomData};
/// Now, when loading your ldtk file, any entities with the entity identifier
/// "my_entity_identifier" will be spawned as `MyBundle`s.
///
/// By default, each component or nested bundle in the bundle will be created using their [Default]
/// implementations.
/// By default, each component or nested bundle in the bundle will be consumed from bundle's
/// [Default] implementation, which means that deriving (or implementing manually) [Default]
/// is required (unless all fields are overriden, see below).
/// However, this behavior can be overridden with some field attribute macros...
///
/// ### `#[sprite_bundle...]`
Expand All @@ -80,14 +81,14 @@ use std::{collections::HashMap, marker::PhantomData};
/// # struct PlayerComponent;
/// # #[derive(Component, Default)]
/// # struct Health;
/// #[derive(Bundle, LdtkEntity)]
/// #[derive(Bundle, LdtkEntity, Default)]
/// pub struct Gem {
/// #[sprite_bundle("textures/gem.png")]
/// sprite_bundle: SpriteBundle,
/// sellable: Sellable,
/// }
///
/// #[derive(Bundle, LdtkEntity)]
/// #[derive(Bundle, LdtkEntity, Default)]
/// pub struct Player {
/// player: PlayerComponent,
/// health: Health,
Expand Down Expand Up @@ -116,14 +117,14 @@ use std::{collections::HashMap, marker::PhantomData};
/// # struct Damage;
/// # #[derive(Component, Default)]
/// # struct BleedDamage;
/// #[derive(Bundle, LdtkEntity)]
/// #[derive(Bundle, LdtkEntity, Default)]
/// pub struct Sword {
/// #[sprite_sheet_bundle("weapons.png", 32.0, 32.0, 4, 5, 5.0, 1.0, 17)]
/// sprite_sheet: SpriteSheetBundle,
/// damage: Damage,
/// }
///
/// #[derive(Bundle, LdtkEntity)]
/// #[derive(Bundle, LdtkEntity, Default)]
/// pub struct Dagger {
/// damage: Damage,
/// bleed_damage: BleedDamage,
Expand All @@ -145,7 +146,7 @@ use std::{collections::HashMap, marker::PhantomData};
/// # struct Player;
/// # #[derive(Component, Default)]
/// # struct BleedDamage;
/// #[derive(Bundle, LdtkEntity)]
/// #[derive(Bundle, LdtkEntity, Default)]
/// pub struct PlayerBundle {
/// player: Player,
/// #[sprite_sheet_bundle]
Expand All @@ -167,7 +168,7 @@ use std::{collections::HashMap, marker::PhantomData};
/// # struct Block;
/// # #[derive(Component, Default)]
/// # struct Movable;
/// #[derive(Bundle, LdtkEntity)]
/// #[derive(Bundle, LdtkEntity, Default)]
/// pub struct BlockBundle {
/// block: Block,
/// movable: Movable,
Expand All @@ -192,14 +193,14 @@ use std::{collections::HashMap, marker::PhantomData};
/// # struct Damage;
/// # #[derive(Component, Default)]
/// # struct BleedDamage;
/// #[derive(Bundle, LdtkEntity)]
/// #[derive(Bundle, LdtkEntity, Default)]
/// pub struct Weapon {
/// damage: Damage,
/// #[sprite_bundle]
/// sprite: SpriteBundle,
/// }
///
/// #[derive(Bundle, LdtkEntity)]
/// #[derive(Bundle, LdtkEntity, Default)]
/// pub struct Dagger {
/// #[ldtk_entity]
/// weapon_bundle: Weapon,
Expand Down Expand Up @@ -234,7 +235,7 @@ use std::{collections::HashMap, marker::PhantomData};
/// }
/// }
///
/// #[derive(Bundle, LdtkEntity)]
/// #[derive(Bundle, LdtkEntity, Default)]
/// pub struct NickelBundle {
/// #[sprite_bundle]
/// sprite: SpriteBundle,
Expand Down
15 changes: 8 additions & 7 deletions src/app/ldtk_int_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use std::{collections::HashMap, marker::PhantomData};
/// # struct ComponentB;
/// # #[derive(Component, Default)]
/// # struct ComponentC;
/// #[derive(Bundle, LdtkIntCell)]
/// #[derive(Bundle, LdtkIntCell, Default)]
/// pub struct MyBundle {
/// a: ComponentA,
/// b: ComponentB,
Expand All @@ -56,8 +56,9 @@ use std::{collections::HashMap, marker::PhantomData};
/// Now, when loading your ldtk file, any IntGrid tiles with the value `1` will be spawned with as
/// tiles with `MyBundle` inserted.
///
/// By default, each component or nested bundle in the bundle will be created using their [Default]
/// implementations.
/// By default, each component or nested bundle in the bundle will be consumed from bundle's
/// [Default] implementation, which means that deriving (or implementing manually) [Default]
/// is required (unless all fields are overriden, see below).
/// However, this behavior can be overriden with some field attribute macros...
///
/// ### `#[ldtk_int_cell]`
Expand All @@ -74,12 +75,12 @@ use std::{collections::HashMap, marker::PhantomData};
/// # struct RigidBody;
/// # #[derive(Component, Default)]
/// # struct Damage;
/// #[derive(Bundle, LdtkIntCell)]
/// #[derive(Bundle, LdtkIntCell, Default)]
/// pub struct Wall {
/// rigid_body: RigidBody,
/// }
///
/// #[derive(Bundle, LdtkIntCell)]
/// #[derive(Bundle, LdtkIntCell, Default)]
/// pub struct DestructibleWall {
/// #[ldtk_int_cell]
/// wall: Wall,
Expand Down Expand Up @@ -115,7 +116,7 @@ use std::{collections::HashMap, marker::PhantomData};
/// }
/// }
///
/// #[derive(Bundle, LdtkIntCell)]
/// #[derive(Bundle, LdtkIntCell, Default)]
/// pub struct Lava {
/// #[from_int_grid_cell]
/// fluid: Fluid,
Expand Down Expand Up @@ -146,7 +147,7 @@ use std::{collections::HashMap, marker::PhantomData};
/// }
/// }
///
/// #[derive(Bundle, LdtkIntCell)]
/// #[derive(Bundle, LdtkIntCell, Default)]
/// pub struct Lava {
/// #[with(initial_fluid)]
/// fluid: Fluid,
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
//! # #[derive(Default, Component)]
//! # struct ComponentB;
//! #
//! #[derive(Bundle, LdtkEntity)]
//! #[derive(Default, Bundle, LdtkEntity)]
//! pub struct MyBundle {
//! a: ComponentA,
//! b: ComponentB,
Expand Down

0 comments on commit f003127

Please sign in to comment.