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::*;
#[derive(Component)]
@ -36,6 +38,32 @@ pub struct AbsorbingFilter {
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)]
pub struct RotatingFilter {
#[bundle]
@ -46,3 +74,26 @@ pub struct RotatingFilter {
pub velocity: Velocity,
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_bundle(AbsorbingFilter {
mesh: ColorMesh2dBundle {
mesh: meshes
.add(Mesh::from(Quad {
size: Vec2 { x: 128.0, y: 16.0 },
flip: false,
}))
.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);
spawn_absorbing_filter(
commands,
meshes,
materials,
Transform::from_xyz(0., 0., 2.),
Vec2 { x: 128.0, y: 16.0 },
Color::RED,
);
commands
.spawn_bundle(RotatingFilter {
sprite: SpriteBundle {
texture: asset_server.get_handle("bevy.png"),
transform: Transform::from_xyz(256., -224., 2.),
..Default::default()
},
collider: Collider::ball(32.),
sensor: Sensor,
filter: PassThroughFilter::Rotating(45.),
velocity: Velocity::angular(1.),
rigid_body: RigidBody::KinematicVelocityBased,
})
.insert(Level);
spawn_rotating_filter(
commands,
asset_server,
Transform::from_xyz(256., -224., 2.),
45.,
);
}