Trouv / bevy_ecs_ldtk

ECS-friendly ldtk plugin for bevy, leveraging bevy_ecs_tilemap
Other
664 stars 75 forks source link

With attribute #128

Closed marcoseiza closed 1 year ago

marcoseiza commented 1 year ago

Summary

Adds a new attribute to the LdtkEntity derive macro named with. It allows for bundles to implement custom initializers using a method. This macro takes in a scoped function with signature (e: EntityInstance) -> T where T is the field type.

Example

pub struct Bundle {
   #[with(foo_initializer)]
    foo: i32;
}

fn foo_initializer(_e: EntityInstance) -> i32 {
    4
}
Longer Example ```rs #[derive(Clone, Default, Bundle)] pub struct ColliderBundle { pub collider: Collider, pub rigid_body: RigidBody, pub damping: Damping, ... } #[derive(Bundle, Default, LdtkEntity)] pub struct PlayerBundle { player: Player, #[with(player_collider)] #[bundle] collider: ColliderBundle, } fn player_collider(_: EntityInstance) -> ColliderBundle { ColliderBundle { collider: Collider::capsule(Vec2::new(0., -4.), Vec2::new(0., -12.), 5.), rigid_body: RigidBody::Dynamic, rotation_constraints: LockedAxes::ROTATION_LOCKED, damping: Damping { linear_damping: 10.0, ..Default::default() }, ..Default::default() } } ```

Issue

The same functionality can be done using from_entity_instance by filtering the identifier to the correct type. However, this could cause function bloat when many entities with different identifiers implement the same bundle. You can have cleaner and safer code separation with this with(function) attribute.

ToDo

Trouv commented 1 year ago

Please add some documentation w/ an example to src/app/ldtk_entity.rs

marcoseiza commented 1 year ago

Ready for another review!

marcoseiza commented 1 year ago

Checked with cargo test and cargo build, all pass!

marcoseiza commented 1 year ago

okay, third times the charm 😅

marcoseiza commented 1 year ago

Thanks @Trouv for reviewing!!