Closed marcoseiza closed 1 year ago
Please add some documentation w/ an example to src/app/ldtk_entity.rs
Ready for another review!
Checked with cargo test
and cargo build
, all pass!
okay, third times the charm 😅
Thanks @Trouv for reviewing!!
Summary
Adds a new attribute to the
LdtkEntity
derive macro namedwith
. 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
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 theidentifier
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 thiswith(function)
attribute.ToDo