From c9ecaffd6b3538e987a9752191bf39b8411a5a64 Mon Sep 17 00:00:00 2001 From: annieversary Date: Wed, 6 Oct 2021 19:28:36 +0200 Subject: [PATCH] add illumination and debug mode --- Cargo.lock | 10 +++++++++ Cargo.toml | 1 + src/debug.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++ src/illumination.rs | 3 +++ src/light_balls.rs | 17 ++++++++++++++-- src/main.rs | 14 ++++++++++++- src/player.rs | 5 +++-- 7 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 src/debug.rs create mode 100644 src/illumination.rs diff --git a/Cargo.lock b/Cargo.lock index 4e240cd..e2c75ca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -484,6 +484,15 @@ dependencies = [ "bevy_window", ] +[[package]] +name = "bevy_prototype_debug_lines" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70e5d04fa850cbfdc7b2525bb0d41ed3b19b0da7c013a09ce9ce85949f5b3102" +dependencies = [ + "bevy", +] + [[package]] name = "bevy_reflect" version = "0.5.0" @@ -2134,6 +2143,7 @@ version = "0.1.0" dependencies = [ "bevy", "bevy_mod_raycast", + "bevy_prototype_debug_lines", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index bc8dd57..fbb146d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,4 @@ edition = "2018" [dependencies] bevy = "0.5.0" bevy_mod_raycast = "0.2.2" +bevy_prototype_debug_lines = "0.3.3" diff --git a/src/debug.rs b/src/debug.rs new file mode 100644 index 0000000..9409e1a --- /dev/null +++ b/src/debug.rs @@ -0,0 +1,49 @@ +use std::f32::consts::TAU; + +use bevy::{ecs::schedule::ShouldRun, prelude::*}; +use bevy_prototype_debug_lines::*; + +use crate::illumination::Illumination; + +#[derive(Default)] +pub struct Debug { + on: bool, +} + +pub fn debug_run_criteria(debug: Res) -> ShouldRun { + if debug.on { + ShouldRun::Yes + } else { + ShouldRun::No + } +} + +pub fn debug_draw_illumination( + query: Query<(&GlobalTransform, &Illumination)>, + mut lines: ResMut, +) { + for (trans, illumination) in query.iter() { + draw_sphere(&mut lines, trans.translation, illumination.radius); + } +} + +fn draw_sphere(lines: &mut DebugLines, c: Vec3, r: f32) { + const THICKNESS: f32 = 0.1; + const MAX_LINES: usize = 10; + + for i in 0..MAX_LINES { + let angle1 = (i as f32 / MAX_LINES as f32) * TAU; + let angle2 = ((i + 1) as f32 / MAX_LINES as f32) * TAU; + + let start = Vec3::new(r * f32::cos(angle1), 1.0, r * f32::sin(angle1)) + c; + let end = Vec3::new(r * f32::cos(angle2), 1.0, r * f32::sin(angle2)) + c; + + lines.line(start, end, THICKNESS); + } +} + +pub fn toggle_debug(mut debug: ResMut, input: Res>) { + if input.pressed(KeyCode::J) && input.pressed(KeyCode::K) && input.just_pressed(KeyCode::L) { + debug.on = !debug.on; + } +} diff --git a/src/illumination.rs b/src/illumination.rs new file mode 100644 index 0000000..ccd7150 --- /dev/null +++ b/src/illumination.rs @@ -0,0 +1,3 @@ +pub struct Illumination { + pub radius: f32, +} diff --git a/src/light_balls.rs b/src/light_balls.rs index 5ff6885..0cc32b2 100644 --- a/src/light_balls.rs +++ b/src/light_balls.rs @@ -1,6 +1,6 @@ use bevy::prelude::*; -use crate::player::Player; +use crate::{illumination::Illumination, player::Player}; const RANGE: f32 = 25.0; @@ -14,6 +14,7 @@ pub fn light_up_ball_when_close_to_player( &LightBall, &mut Handle, Option<&mut Light>, + Option<&mut Illumination>, )>, materials: Res, ) { @@ -23,7 +24,7 @@ pub fn light_up_ball_when_close_to_player( return; }; - for (entity, trans, _, mut material, light) in thingies.iter_mut() { + for (entity, trans, _, mut material, light, illumination) in thingies.iter_mut() { let dis = trans.translation.distance(player_pos); if dis < RANGE { *material = materials.lit.clone(); @@ -39,6 +40,15 @@ pub fn light_up_ball_when_close_to_player( ..Default::default() }); } + + // same with illumination + if let Some(mut l) = illumination { + l.radius = 15.0 * ((RANGE - dis) / RANGE).sqrt(); + } else { + commands.entity(entity).insert(Illumination { + radius: 15.0 * ((RANGE - dis) / RANGE).sqrt().sqrt(), + }); + } } else { *material = materials.unlit.clone(); @@ -46,6 +56,9 @@ pub fn light_up_ball_when_close_to_player( if light.is_some() { commands.entity(entity).remove::(); } + if illumination.is_some() { + commands.entity(entity).remove::(); + } } } } diff --git a/src/main.rs b/src/main.rs index bb48564..03e0fcb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,18 @@ use bevy::{input::system::exit_on_esc_system, pbr::AmbientLight, prelude::*}; use bevy_mod_raycast::{build_rays, update_raycast, PluginState, RayCastMesh, RaycastSystem}; +use bevy_prototype_debug_lines::*; mod camera; mod columns; +mod debug; +mod illumination; mod light_balls; mod loading; mod player; mod rendering; use camera::*; +use debug::*; use light_balls::*; use player::*; @@ -23,6 +27,8 @@ fn main() { .insert_resource(Msaa { samples: 4 }) .insert_resource(ClearColor(Color::rgb(0.0, 0.0, 0.0))) .add_plugins(rendering::CustomPlugins) + .add_plugin(DebugLinesPlugin) + .init_resource::() .init_resource::() .init_resource::() .init_resource::() @@ -64,7 +70,13 @@ fn main() { .with_system(update_floating_orbs_interpolation.system()) .with_system(move_player.system()) .with_system(camera_follow_player.system()) - .with_system(light_up_ball_when_close_to_player.system()), + .with_system(light_up_ball_when_close_to_player.system()) + .with_system(toggle_debug.system()), + ) + .add_system_set( + SystemSet::on_update(AppState::Game) + .with_run_criteria(debug_run_criteria.system()) + .with_system(debug_draw_illumination.system()), ) .run(); } diff --git a/src/player.rs b/src/player.rs index 875cce4..8fc5362 100644 --- a/src/player.rs +++ b/src/player.rs @@ -1,6 +1,6 @@ use bevy::prelude::*; -use crate::camera::MouseCoords; +use crate::{camera::MouseCoords, illumination::Illumination}; pub struct Player; pub struct PlayerLight { @@ -59,7 +59,8 @@ pub fn spawn_player( }, ..Default::default() }) - .insert(PlayerLight { i: i as u8 + 1 }); + .insert(PlayerLight { i: i as u8 + 1 }) + .insert(Illumination { radius: 13.0 }); } }); }