8个版本 (破坏性)

0.6.0 2024年7月4日
0.5.0 2024年5月26日
0.4.0 2024年5月11日
0.3.0 2024年4月17日
0.0.2 2023年11月14日

#599 in 游戏开发

Download history 199/week @ 2024-05-08 9/week @ 2024-05-15 154/week @ 2024-05-22 18/week @ 2024-05-29 8/week @ 2024-06-05 4/week @ 2024-06-12 139/week @ 2024-07-03 4/week @ 2024-07-10 13/week @ 2024-07-24 73/week @ 2024-07-31

每月下载量:86

MIT/Apache

13KB
135

bevy_bundlication

基于包模式的bevy网络复制。使用类似包的API为bevy_replicon提供复制组规则。

目标

  • 简化复制组的定义
  • 简化带宽优化

入门指南

bevy_bundlication使用与bevy中的Bundle类似的模式。任何匹配包的内容都将进行网络化。每个字段需要实现NetworkedComponent,这可以通过手动实现或在具有ComponentSerialzeDeserialize的类型上实现 blanket impl 来完成。对于 blanket impl 会导致冲突的类型,可以使用#[bundlication)]属性,其中Wrapper是实现了NetworkedWrapper<YourType>的类型。

可以使用replicate_group::<Bundle>()将包注册到bevy_replicon。

use bevy::prelude::*;
use bevy_replicon::prelude::*;

#[derive(NetworkedBundle)]
pub struct PlayerPositionBundle {
    // The content of this field doesn't get sent, and it will be received as the default value,
    // it therefor requires neither Serialize/Deserialize nor NetworkedComponent
    #[bundlication(no_send)]
    pub player: Player,
    // This component is sent and spawned as is
    pub speed: Speed,
    // We replicate Transform, but it is serialized/deserialized using the logic of JustTranslation
    #[bundlication(as = JustTranslation)]
    pub translation: Transform,
    // If we also use this as a bundle and have fields we don't want replicon to consider, we can
    // add the skip attribute
    #[bundlication(skip)]
    pub skipped: GlobalTransform,
}

pub struct MovementPlugin;

impl Plugin for MovementPlugin {
    fn build(&self, app: &mut App) {
        // To replicate the bundle, we register our bundle to bevy_replicon
        app.replicate_group::<PlayerPositionBundle>();
    }
}

use bevy_bundlication::prelude::*;
use serde::{Serialize, Deserialize};

// We need Default on Player because we use the no_send attribute
#[derive(Component, Default)]
pub struct Player(u128);

// Speed derives all required traits for the NetworkedBundle blanket impl
#[derive(Component, Serialize, Deserialize)]
pub struct Speed(f32);

// We define a type to network a type we don't own in a different way than its default behavior.
// This can also be used to network components without Serialize/Deserialize
// In this case we only network the translation part of Transform
#[derive(Serialize, Deserialize)]
pub struct JustTranslation(Vec3);

impl NetworkedWrapper<Transform> for JustTranslation {
    fn write_data(from: &Transform, w: impl std::io::Write, _: &SerializeCtx) -> BincodeResult<()> {
        serialize(w, &from.translation)?;
        Ok(())
    }

    fn read_new(r: impl std::io::Read, _: &mut DeserializeCtx) -> BincodeResult<Transform> {
        let translation: Vec3 = deserialize(r)?;
        Ok(Transform::from_translation(translation))
    }
}

许可证

此存储库中的所有代码均可在以下任一许可证下使用:

您可任选其一。

依赖项

~39–76MB
~1.5M SLoC