From 56fea25a879411ee7b056e2017368e45649ce736 Mon Sep 17 00:00:00 2001 From: tuxmain Date: Thu, 25 Aug 2022 09:55:39 +0200 Subject: [PATCH] Filter spawn functions --- src/filters.rs | 53 +++++++++++++++++++++++++++++++++++++++++++- src/levels/level1.rs | 46 ++++++++++++-------------------------- 2 files changed, 66 insertions(+), 33 deletions(-) diff --git a/src/filters.rs b/src/filters.rs index e34b458..9a18af2 100644 --- a/src/filters.rs +++ b/src/filters.rs @@ -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>, + materials: &mut ResMut>, + + 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, + + 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); +} diff --git a/src/levels/level1.rs b/src/levels/level1.rs index f2d7a73..8bcf4bb 100644 --- a/src/levels/level1.rs +++ b/src/levels/level1.rs @@ -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., + ); }