This commit is contained in:
Mike 2025-07-17 13:33:59 -07:00 committed by GitHub
commit 733ca10534
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -10,6 +10,7 @@
//! or <https://doc.rust-lang.org/rust-by-example/generics.html>
use bevy::prelude::*;
use system_param_in_associated_type::*;
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash, States)]
enum AppState {
@ -40,6 +41,7 @@ fn main() {
(
print_text_system,
transition_to_in_game_system.run_if(in_state(AppState::MainMenu)),
system::<ItemA>,
),
)
// Cleanup systems.
@ -61,6 +63,8 @@ fn setup_system(mut commands: Commands) {
TextToPrint("I will always print".to_string()),
LevelUnload,
));
commands.insert_resource(ResourceC { data: 3 });
}
fn print_text_system(time: Res<Time>, mut query: Query<(&mut PrinterTick, &TextToPrint)>) {
@ -87,3 +91,137 @@ fn cleanup_system<T: Component>(mut commands: Commands, query: Query<Entity, Wit
commands.entity(e).despawn();
}
}
// For a more advanced usage you may want have a group of system params to implement a trait.
// Note that this example is a little contrived in the interest of keeping things simple. The
// purpose here is to demontrate how to get the traits and lifetimes to work properly.
mod system_with_generic_system_param {
use super::*;
use bevy::ecs::system::SystemParam;
struct DamagePlugin;
impl Plugin for DamagePlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, setup_damage).add_systems(
Update,
(
apply_damage::<PlayerDamageParams>,
apply_damage::<EnemyDamageParams>,
),
);
}
}
#[derive(Component)]
struct Player;
#[derive(Component)]
struct Enemy;
#[derive(Component)]
struct Health(f32);
#[derive(Resource)]
struct EnemySettings {
/// damage done by player to enemy
take_damage: f32,
/// damage done by enemy to player
do_damage: f32,
}
pub trait GetDamage {
fn apply_damage(&mut self) {}
}
#[derive(SystemParam)]
struct PlayerDamageParams<'w, 's> {
player: Query<'w, 's, &'static mut Health, With<Player>>,
enemy_settings: Res<'w, EnemySettings>,
}
impl<'w, 's> GetDamage for PlayerDamageParams<'w, 's> {
fn apply_damage(&mut self) {
let mut player_health = self.player.single_mut();
player_health.0 += self.enemy_settings.do_damage;
}
}
#[derive(SystemParam)]
struct EnemyDamageParams<'w, 's> {
enemies: Query<'w, 's, &'static mut Health, With<Enemy>>,
enemy_settings: Res<'w, EnemySettings>,
}
impl<'w, 's> GetDamage for EnemyDamageParams<'w, 's> {
fn apply_damage(&mut self) {
for mut enemy_health in self.enemies.iter_mut() {
enemy_health.0 -= self.enemy_settings.take_damage;
}
}
}
// Note that the param passed into a system is `SystemParam::Item` and not just `SystemParam`.
fn apply_damage<S: SystemParam>(mut param: S::Item<'_, '_>)
where
for<'w, 's> S::Item<'w, 's>: GetDamage,
{
param.apply_damage();
}
fn setup_damage(mut commands: Commands) {
commands.insert_resource(EnemySettings {
do_damage: 1.0,
take_damage: 2.0,
});
}
}
// You may want to be have the SystemParam be specified in an associated type.
mod system_param_in_associated_type {
use super::*;
use bevy::ecs::system::{lifetimeless::SRes, StaticSystemParam, SystemParam, SystemParamItem};
#[derive(Resource)]
pub struct ResourceA {
pub data: u32,
}
#[derive(Resource)]
pub struct ResourceC {
pub data: u32,
}
pub trait MyTrait {
type Param: SystemParam + 'static;
fn do_something(&self, param: &mut SystemParamItem<Self::Param>) -> u32;
}
#[derive(Resource)]
pub struct ItemA;
impl MyTrait for ItemA {
// specifies that data needed by do_something
type Param = SRes<ResourceC>;
fn do_something(&self, param: &mut SystemParamItem<Self::Param>) -> u32 {
param.data
}
}
#[derive(Resource)]
pub struct ItemB;
impl MyTrait for ItemB {
// we can specify any system param here, including a combination of other system params
type Param = (SRes<ResourceA>, SRes<ResourceC>);
fn do_something(&self, param: &mut SystemParamItem<Self::Param>) -> u32 {
// todo: Make this more intelligible
param.0.data + param.1.data
}
}
pub fn system<S: MyTrait + Resource>(
mut param: StaticSystemParam<<S as MyTrait>::Param>,
item: ResMut<S>,
) {
item.do_something(&mut param);
}
}