Latest commit 85dac0c
标签
标签可以标记应用程序中的各种事物,例如 [System]、[Run Criteria]、[Stage]、[Ambiguity Set]。
标签常用于[系统排序]和添加[阶段]。
Bevy 巧用 Rust 类型系统实现了可用字符串或自定义类型来制作标签,甚至可以混合起来使用。
todo:未测试,大意是字符串很快,但不方便编译器报错。 Using strings for labels is quick and easy for prototyping. However, they are easy to mistype and are unstructured. The compiler cannot validate them for you, to catch mistakes.
todo:未测试,意思是建议用 enum 来定义标签。 You can also use custom types (usually enums) to define your labels. This allows the compiler to check them, and helps you stay organized in larger projects.
根据需要来派生相应的特性:StageLabel、SystemLabel、RunCriteriaLabel、AmbiguitySetLabel。
任何类型的值都可以作为标签,只要这个类型具有以下的 Rust 特型:
Clone + PartialEq + Eq + Hash + Debug(和隐含的 + Send + Sync + 'static)。
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(SystemLabel)] enum MySystems { InputSet, Movement, } #[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(SystemLabel)] enum MyStages { Prepare, Cleanup, } #[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(SystemLabel)] strut DebugStage; fn main() { App::new() .add_plugins(DefaultPlugins) // 添加游戏系统 .add_system_set( SystemSet::new() .label(MySystems::InputSet) .with_system(keyboard_input) .with_system(gamepad_input) ) .add_system(player_movement.label(MySystems::Movement)) // 临时调试系统,仅用一个字符串标签 .add_system(debug_movement.label("temp-debug")) // 添加自定义阶段:注意,Bevy 的 CoreStage 也是枚举! .add_stage_before(CoreStage::Update, MyStages::prepare, SystemStage::parallel()) .add_stage_after(CoreStage::Update, MyStages::Cleanup, SystemStage::parallel()) .add_stage_after(CoreStage::Update, DebugStage, SystemStage::parallel()) // 这里只需使用一个字符串 .add_stage_before(CoreStage::PostUpdate, "temp-debug-hack", SystemStage::parallel()) .run(); }
对于快速的游戏原型开发,只需用字符串作为标签就可以了。但是,把标签定义为自定义类型,Rust 编译器可以检查它们,IDE 也能够可以自动完成。这是推荐的方式,因为自定义类型的标签可以防止错误,在大型项目中保持组织性。