TnuaScheme

Derive Macro TnuaScheme 

Source
#[derive(TnuaScheme)]
{
    // Attributes available to this derive:
    #[scheme]
}
Expand description

Make an enum a control scheme for a Tnua character controller.

This implements the TnuaScheme trait for the enum, and also generates the following structs required for implementing it (replace {name} with the name of the control scheme enum):

  • {name}Config - a struct with the configuration of the basis and all the actions.
  • {name}ActionDiscriminant - an enum mirroring the control scheme, except all the variants are units.
  • {name}ActionState - an enum mirroring the control scheme, except instead of just the input types each variant contains a TnuaActionState which holds the input, configuration and memory of the action.

The enum itself must have a #[scheme(basis = ...)] attribute that specifies the basis of the control scheme (typically TnuaBuiltinWalk). The following additional parameters are allowed on that scheme attribute on the enum:

  • #[scheme(serde)] - derive Serialize and Deserialize on the generated action state enum.
    • This is mostly useful with (and will probably fail without) the serialize feature enabled on the bevy-tnua crate.
    • The control scheme enum itself will not get these derives automatically - that derive will need to be added manually.
    • With these, and with the serialize feature enabled, the TnuaController and TnuaGhostOverwrites of the control scheme will also be serializable and deserializable - allowing networking libraries to synchronize them between machines.
    • Even without this setting and without the serialize feature on the bevy-tnua crate, the generated configuration struct and the action discriminant enum will still get these derives.

Each variant must be a tuple variant, where the first element of the tuple is the action, followed by zero or more payloads.

Payloads are ignored by Tnua itself - they are for the user systems to keep track of data related to the actions - except when they are annotated by #[scheme(modify_basis_config)]. Such payloads will modify the configuration when the action they are part of is in effect.

Example:

#[derive(TnuaScheme)]
#[scheme(basis = TnuaBuiltinWalk)]
pub enum DemoControlScheme {
    Jump(TnuaBuiltinJump),
    Crouch(
        TnuaBuiltinCrouch,
        // While this action is in effect, `SlowDownWhileCrouching` will change the
        // `TnuaBuiltinWalkConfig` to reduce character speed.
        #[scheme(modify_basis_config)] SlowDownWhileCrouching,
    ),
    WallSlide(
        TnuaBuiltinWallSlide,
        // This payload has is ignored by Tnua, but user code can use it to tell which wall
        // the character is sliding on.
        Entity,
    ),
}