From 22147c818aafa46a0f4b8001a136e32bc96a208e Mon Sep 17 00:00:00 2001 From: tuxmain Date: Wed, 24 Aug 2022 12:21:08 +0200 Subject: [PATCH] Platform material & filter bundle --- src/filters.rs | 20 +++++++++++++++++ src/game.rs | 12 ++++++---- src/levels.rs | 3 +++ src/levels/level0.rs | 17 +++++++++++--- src/levels/level1.rs | 53 ++++++++++++++++++++++++++++++++++++++------ src/main.rs | 17 +++++++------- 6 files changed, 99 insertions(+), 23 deletions(-) create mode 100644 src/filters.rs diff --git a/src/filters.rs b/src/filters.rs new file mode 100644 index 0000000..a4439c4 --- /dev/null +++ b/src/filters.rs @@ -0,0 +1,20 @@ +use bevy::prelude::*; +use bevy_rapier2d::prelude::*; + +#[derive(Component, Default)] +pub struct FilterColor(pub Color); + +#[derive(Component)] +pub enum PassThroughFilter { + Absorbing, +} + +#[derive(Bundle)] +pub struct AbsorbingFilter { + pub color: FilterColor, + #[bundle] + pub mesh: ColorMesh2dBundle, + pub collider: Collider, + pub sensor: Sensor, + pub filter_type: PassThroughFilter, +} diff --git a/src/game.rs b/src/game.rs index ef051cb..0b0d139 100644 --- a/src/game.rs +++ b/src/game.rs @@ -1,6 +1,8 @@ #![allow(clippy::precedence)] #![allow(clippy::too_many_arguments)] +pub use crate::filters::*; + use crate::AppState; use bevy::{ @@ -322,14 +324,16 @@ fn move_camera( .iter() .find(|(character_id, _transform)| *character_id == selected_character_id) { - let (camera, mut camera_transform) = - camera_query.single_mut(); + let (camera, mut camera_transform) = camera_query.single_mut(); let size: Vec2 = camera.logical_viewport_size().unwrap(); let half_height: f32 = size.y * 0.5; - camera_transform.translation = camera_transform.translation.lerp(transform.translation, time.delta_seconds() * FOLLOW_SPEED); + camera_transform.translation = camera_transform + .translation + .lerp(transform.translation, time.delta_seconds() * FOLLOW_SPEED); // prevent camera from going too low - camera_transform.translation.y = camera_transform.translation.y.max(half_height-MARGIN); + camera_transform.translation.y = + camera_transform.translation.y.max(half_height - MARGIN); camera_transform.translation.z = 999.0; } } diff --git a/src/levels.rs b/src/levels.rs index b34c23f..76c35ea 100644 --- a/src/levels.rs +++ b/src/levels.rs @@ -39,6 +39,7 @@ pub fn despawn_level(mut commands: Commands, level_query: Query, + mut meshes: ResMut>, mut materials: ResMut>, current_level: Res, mut level_query: Query<(&mut SelectedCharacterId, &mut CharacterIdList)>, @@ -54,6 +55,7 @@ pub fn post_setup_level( match level_id.0 { 0 => level0::setup( &mut commands, + &mut meshes, &character_meshes, &mut materials, &mut selected_character_id, @@ -62,6 +64,7 @@ pub fn post_setup_level( ), 1 => level1::setup( &mut commands, + &mut meshes, &character_meshes, &mut materials, &mut selected_character_id, diff --git a/src/levels/level0.rs b/src/levels/level0.rs index e95849d..92e9618 100644 --- a/src/levels/level0.rs +++ b/src/levels/level0.rs @@ -1,10 +1,11 @@ use crate::game::*; -use bevy::prelude::*; +use bevy::prelude::{shape::Quad, *}; use bevy_rapier2d::prelude::*; pub fn setup( commands: &mut Commands, + meshes: &mut ResMut>, character_meshes: &Res, materials: &mut ResMut>, selected_character_id: &mut Mut, @@ -12,8 +13,18 @@ pub fn setup( audio: &Res>, ) { commands - .spawn_bundle(TransformBundle::from(Transform::from_xyz(0.0, -256.0, 0.0))) - .insert(Collider::cuboid(400., 10.)) + .spawn_bundle(ColorMesh2dBundle { + mesh: meshes + .add(Mesh::from(Quad { + size: Vec2 { x: 800.0, y: 16.0 }, + flip: false, + })) + .into(), + material: materials.add(ColorMaterial::from(Color::GRAY)), + transform: Transform::from_xyz(0.0, -256.0, 0.0), + ..default() + }) + .insert(Collider::cuboid(400., 8.)) .insert(Level); spawn_character( diff --git a/src/levels/level1.rs b/src/levels/level1.rs index 4c29b4c..5e86ff0 100644 --- a/src/levels/level1.rs +++ b/src/levels/level1.rs @@ -1,10 +1,11 @@ use crate::game::*; -use bevy::prelude::*; +use bevy::prelude::{shape::Quad, *}; use bevy_rapier2d::prelude::*; pub fn setup( commands: &mut Commands, + meshes: &mut ResMut>, character_meshes: &Res, materials: &mut ResMut>, selected_character_id: &mut Mut, @@ -12,14 +13,32 @@ pub fn setup( audio: &Res>, ) { commands - .spawn_bundle(TransformBundle::from(Transform::from_xyz(0.0, -256.0, 0.0))) - .insert(Collider::cuboid(400., 10.)) + .spawn_bundle(ColorMesh2dBundle { + mesh: meshes + .add(Mesh::from(Quad { + size: Vec2 { x: 800.0, y: 16.0 }, + flip: false, + })) + .into(), + material: materials.add(ColorMaterial::from(Color::GRAY)), + transform: Transform::from_xyz(0.0, -256.0, 0.0), + ..default() + }) + .insert(Collider::cuboid(400., 8.)) .insert(Level); commands - .spawn_bundle(TransformBundle::from(Transform::from_xyz( - 256.0, -128.0, 0.0, - ))) - .insert(Collider::cuboid(200., 10.)) + .spawn_bundle(ColorMesh2dBundle { + mesh: meshes + .add(Mesh::from(Quad { + size: Vec2 { x: 400.0, y: 16.0 }, + flip: false, + })) + .into(), + material: materials.add(ColorMaterial::from(Color::GRAY)), + transform: Transform::from_xyz(256.0, -128.0, 0.0), + ..default() + }) + .insert(Collider::cuboid(200., 8.)) .insert(Level); spawn_character( @@ -52,4 +71,24 @@ pub fn setup( Transform::from_xyz(0., -128., 0.), Color::GREEN, ); + + commands + .spawn_bundle(AbsorbingFilter { + color: FilterColor(Color::RED), + 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_type: PassThroughFilter::Absorbing, + }) + .insert(Level); } diff --git a/src/main.rs b/src/main.rs index 7a19dab..b7fbbc2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,12 @@ #[cfg(not(target_arch = "wasm32"))] mod audio; +mod filters; mod game; mod levels; mod menu; mod particle_effect; -use bevy::{core_pipeline::clear_color::ClearColorConfig, prelude::*}; +use bevy::prelude::*; use bevy_rapier2d::prelude::*; #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] @@ -23,15 +24,18 @@ fn main() { std::thread::spawn(move || audio::setup(audio_event_receiver)); App::new() + .insert_resource(Msaa { samples: 4 }) .insert_resource(audio_event_sender) .add_state(AppState::Menu) + .insert_resource(ClearColor(Color::BLACK)) .add_plugins(DefaultPlugins) .add_plugin(RapierPhysicsPlugin::::pixels_per_meter(64.0)) - .add_plugin(RapierDebugRenderPlugin::default()) + //.add_plugin(RapierDebugRenderPlugin::default()) .add_plugin(menu::MenuPlugin) .add_plugin(game::GamePlugin) .add_plugin(particle_effect::ParticleEffectPlugin) - .add_plugin(bevy_inspector_egui::WorldInspectorPlugin::new()) + //.add_plugin(bevy_inspector_egui::WorldInspectorPlugin::new()) + .add_system(bevy::window::close_on_esc) .add_startup_system(setup) .run(); } @@ -43,12 +47,7 @@ fn setup(mut commands: Commands, asset_server: Res) { let font: Handle = asset_server.load("UacariLegacy-Thin.ttf"); commands.insert_resource(font); - commands.spawn_bundle(Camera2dBundle { - camera_2d: Camera2d { - clear_color: ClearColorConfig::Custom(Color::BLACK), - }, - ..Default::default() - }); + commands.spawn_bundle(Camera2dBundle::default()); commands.insert_resource(AmbientLight { color: Color::WHITE, brightness: 0.6,