Filter spawn functions

This commit is contained in:
Pascal Engélibert 2022-08-25 09:55:39 +02:00
parent 5ac4b4711c
commit 56fea25a87
Signed by: tuxmain
GPG key ID: 3504BC6D362F7DCA
2 changed files with 66 additions and 33 deletions

View file

@ -1,4 +1,6 @@
use bevy::prelude::*; use crate::game::Level;
use bevy::prelude::{shape::Quad, *};
use bevy_rapier2d::prelude::*; use bevy_rapier2d::prelude::*;
#[derive(Component)] #[derive(Component)]
@ -36,6 +38,32 @@ pub struct AbsorbingFilter {
pub filter: PassThroughFilter, pub filter: PassThroughFilter,
} }
pub fn spawn_absorbing_filter(
commands: &mut Commands,
meshes: &mut ResMut<Assets<Mesh>>,
materials: &mut ResMut<Assets<ColorMaterial>>,
transform: Transform,
size: Vec2,
color: Color,
) {
let mut material_color = color;
material_color.set_a(0.5);
commands
.spawn_bundle(AbsorbingFilter {
mesh: ColorMesh2dBundle {
mesh: meshes.add(Mesh::from(Quad { size, flip: false })).into(),
material: materials.add(material_color.into()),
transform,
..Default::default()
},
collider: Collider::cuboid(size.x / 2., size.y / 2.),
sensor: Sensor,
filter: PassThroughFilter::Absorbing(color),
})
.insert(Level);
}
#[derive(Bundle)] #[derive(Bundle)]
pub struct RotatingFilter { pub struct RotatingFilter {
#[bundle] #[bundle]
@ -46,3 +74,26 @@ pub struct RotatingFilter {
pub velocity: Velocity, pub velocity: Velocity,
pub rigid_body: RigidBody, pub rigid_body: RigidBody,
} }
pub fn spawn_rotating_filter(
commands: &mut Commands,
asset_server: &Res<AssetServer>,
transform: Transform,
angle: f32,
) {
commands
.spawn_bundle(RotatingFilter {
sprite: SpriteBundle {
texture: asset_server.get_handle("bevy.png"),
transform,
..Default::default()
},
collider: Collider::ball(32.),
sensor: Sensor,
filter: PassThroughFilter::Rotating(angle),
velocity: Velocity::angular(1.),
rigid_body: RigidBody::KinematicVelocityBased,
})
.insert(Level);
}

View file

@ -52,37 +52,19 @@ pub fn setup(
], ],
); );
commands spawn_absorbing_filter(
.spawn_bundle(AbsorbingFilter { commands,
mesh: ColorMesh2dBundle { meshes,
mesh: meshes materials,
.add(Mesh::from(Quad { Transform::from_xyz(0., 0., 2.),
size: Vec2 { x: 128.0, y: 16.0 }, Vec2 { x: 128.0, y: 16.0 },
flip: false, Color::RED,
})) );
.into(),
material: materials.add(Color::rgba(1., 0., 0., 0.5).into()),
transform: Transform::from_xyz(0., 0., 2.),
..Default::default()
},
collider: Collider::cuboid(64., 8.),
sensor: Sensor,
filter: PassThroughFilter::Absorbing(Color::RED),
})
.insert(Level);
commands spawn_rotating_filter(
.spawn_bundle(RotatingFilter { commands,
sprite: SpriteBundle { asset_server,
texture: asset_server.get_handle("bevy.png"), Transform::from_xyz(256., -224., 2.),
transform: Transform::from_xyz(256., -224., 2.), 45.,
..Default::default() );
},
collider: Collider::ball(32.),
sensor: Sensor,
filter: PassThroughFilter::Rotating(45.),
velocity: Velocity::angular(1.),
rigid_body: RigidBody::KinematicVelocityBased,
})
.insert(Level);
} }