#how can i use multiple instances of a component for the same entity

1 messages · Page 1 of 1 (latest)

echo jay
#
pub fn spawn_player (
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<PlayerShader>>,
    asset_server: Res<AssetServer>
) {
    let quad1 = MaterialMesh2dBundle {
        mesh: meshes.add(Mesh::from(shape::Quad {
            size: Vec2::splat(PlayerConfig::default().size),
            flip: false
        })).into(),
        material: materials.add(PlayerShader::default()),
        ..Default::default()
    };

    let quad2 = MaterialMesh2dBundle {
        mesh: meshes.add(Mesh::from(shape::Quad {
            size: Vec2::splat(PlayerConfig::default().size),
            flip: false
        })).into(),
        material: materials.add(PlayerShader::default()),
        ..Default::default()
    };

    commands.spawn((
        Player { config_handle: asset_server.load("data/player.toml") },
        (quad1, PlayerBody), // don't know what to do here?
        (quad2, PlayerBow),
    ));
}
pub fn player_config (
    // this doesn't work
    mesh_handle: Query<&Mesh2dHandle, With<PlayerBow>>,
    mut meshes: ResMut<Assets<Mesh>>,
) {}
#

PlayerBody and PlayerBow are just marker components

willow gate
#

This is an example of where you probably want to use entity hierarchies

#

For example, the bow should probably be a child of the body

echo jay
#

thanks

willow gate
#

This has a few nice benefits:

  1. It conceptually makes more sense to separate these components since they the player is not both a body and a bow (a player has a body and has a bow)
  2. Child entities are transformed along with their parents. This makes things a lot easier when you want to add swinging and other transformations
  3. The bow entity can have its own set of components which allow you to control it with more granularity