#bevy #gamedev #query #single #component #entity #player

bevy_single

该包允许从bevy查询中更方便地访问单个实体。

1个不稳定版本

0.1.0 2024年7月25日

#541 in 游戏开发

Download history 65/week @ 2024-07-19 56/week @ 2024-07-26 5/week @ 2024-08-02

每月 126 次下载

MIT/Apache

18KB
71

从查询中获取单个实体

示例

在使用Single之前

use bevy::prelude::*;

#[derive(Component)]
struct Player;

pub fn snap_camera(
    players: Query<&Transform, With<Player>>,
    mut cameras: Query<&mut Transform, (With<Camera>, Without<Player>)>,
) {
    let player = players.single();
    let mut camera = cameras.single_mut();

    camera.translation = player.translation.xy().extend(100.0);
}

使用Single之后

use bevy::prelude::*;
use bevy_single::prelude::*;

#[derive(Component)]
struct Player;

pub fn snap_camera(
    player: Single<&Transform, With<Player>>,
    mut camera: Single<&mut Transform, (With<Camera>, Without<Player>)>,
) {
    camera.translation = player.translation.xy().extend(100.0);
}

或者像这样

use bevy::prelude::*;
use bevy_single::prelude::*;

#[derive(Component)]
struct Player;

pub fn snap_camera(
    Single(player): Single<&Transform, With<Player>>,
    Single(mut camera): Single<&mut Transform, (With<Camera>, Without<Player>)>,
) {
    camera.translation = player.translation.xy().extend(100.0);
}

具有多个组件的示例

在使用Single之前

use bevy::prelude::*;
use bevy_rapier2d::prelude::*;

#[derive(Component)]
struct Player;

pub fn move_with_wasd(
    mut players: Query<(&mut KinematicCharacterController, &Player)>,
    input: Res<ButtonInput<KeyCode>>,
    time: Res<Time>,
) {
    let (mut controller, player) = players.single_mut();

    const WASD_DISPATCH: [(KeyCode, Vec2); 4] = [
        (KeyCode::KeyW, Vec2::Y),
        (KeyCode::KeyA, Vec2::NEG_X),
        (KeyCode::KeyS, Vec2::NEG_Y),
        (KeyCode::KeyD, Vec2::X),
    ];

    let mut translation = None;

    for (key, vector) in WASD_DISPATCH {
        if input.pressed(key) {
            translation = Some(translation.unwrap_or(Vec2::ZERO) + vector);
        }
    }

    controller.translation =
        translation.map(|t| t.normalize() * player.speed * time.delta_seconds());
}

使用Single之后

use bevy::prelude::*;
use bevy_rapier2d::prelude::*;
use bevy_single::prelude::*;

#[derive(Component)]
struct Player;

pub fn move_with_wasd(
    Single((
        mut controller,
        player
    )): Single<(&mut KinematicCharacterController, &Player)>,
    input: Res<ButtonInput<KeyCode>>,
    time: Res<Time>,
) {
    const WASD_DISPATCH: [(KeyCode, Vec2); 4] = [
        (KeyCode::KeyW, Vec2::Y),
        (KeyCode::KeyA, Vec2::NEG_X),
        (KeyCode::KeyS, Vec2::NEG_Y),
        (KeyCode::KeyD, Vec2::X),
    ];

    let mut translation = None;

    for (key, vector) in WASD_DISPATCH {
        if input.pressed(key) {
            translation = Some(translation.unwrap_or(Vec2::ZERO) + vector);
        }
    }

    controller.translation =
        translation.map(|t| t.normalize() * player.speed * time.delta_seconds());
}

带有SystemParam的示例

在使用Single之前

use bevy::prelude::*;

#[derive(Component)]
struct SpritesheetAnimation;

pub fn example(
    mut param_set: ParamSet<(
        Query<Entity>,
        Query<&mut SpritesheetAnimation>
    )>,
) {
    let entities = param_set.p0();
    let mut animation = param_set.p1().single_mut();
    // ...
}

使用Single之后

use bevy::prelude::*;
use bevy_single::prelude::*;

#[derive(Component)]
struct SpritesheetAnimation;

pub fn example(
    mut param_set: ParamSet<(
        Query<Entity>,
        Single<&mut SpritesheetAnimation>
    )>,
) {
    let entities = param_set.p0();
    let Single(mut animation) = param_set.p1();
    // ...
}

依赖关系

~11MB
~193K SLoC