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

With attribute #128

Merged
merged 7 commits into from
Nov 13, 2022
Merged

With attribute #128

merged 7 commits into from
Nov 13, 2022

Conversation

marcoseiza
Copy link
Contributor

@marcoseiza marcoseiza commented Nov 8, 2022

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
#[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

  • Ensure type safety of function return; the error messages aren't very good if the function doesn't return the correct type

Copy link
Owner

@Trouv Trouv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution. This is a great idea, and in hindsight it seems so obvious. Def wanna get this in.

macros/src/ldtk_entity.rs Outdated Show resolved Hide resolved
@Trouv
Copy link
Owner

Trouv commented Nov 12, 2022

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

@marcoseiza
Copy link
Contributor Author

Ready for another review!

src/app/ldtk_entity.rs Outdated Show resolved Hide resolved
@marcoseiza
Copy link
Contributor Author

Checked with cargo test and cargo build, all pass!

src/app/ldtk_entity.rs Outdated Show resolved Hide resolved
@marcoseiza
Copy link
Contributor Author

okay, third times the charm 😅

@Trouv Trouv merged commit 18e84be into Trouv:main Nov 13, 2022
@marcoseiza
Copy link
Contributor Author

Thanks @Trouv for reviewing!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants