add illumination and debug mode

This commit is contained in:
annieversary 2021-10-06 19:28:36 +02:00
parent 0474c8c036
commit c9ecaffd6b
7 changed files with 94 additions and 5 deletions

10
Cargo.lock generated
View file

@ -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]]

View file

@ -8,3 +8,4 @@ edition = "2018"
[dependencies]
bevy = "0.5.0"
bevy_mod_raycast = "0.2.2"
bevy_prototype_debug_lines = "0.3.3"

49
src/debug.rs Normal file
View file

@ -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<Debug>) -> ShouldRun {
if debug.on {
ShouldRun::Yes
} else {
ShouldRun::No
}
}
pub fn debug_draw_illumination(
query: Query<(&GlobalTransform, &Illumination)>,
mut lines: ResMut<DebugLines>,
) {
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<Debug>, input: Res<Input<KeyCode>>) {
if input.pressed(KeyCode::J) && input.pressed(KeyCode::K) && input.just_pressed(KeyCode::L) {
debug.on = !debug.on;
}
}

3
src/illumination.rs Normal file
View file

@ -0,0 +1,3 @@
pub struct Illumination {
pub radius: f32,
}

View file

@ -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<StandardMaterial>,
Option<&mut Light>,
Option<&mut Illumination>,
)>,
materials: Res<LightBallMaterials>,
) {
@ -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::<Light>();
}
if illumination.is_some() {
commands.entity(entity).remove::<Illumination>();
}
}
}
}

View file

@ -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::<Debug>()
.init_resource::<LightBallMaterials>()
.init_resource::<MouseCoords>()
.init_resource::<FloatingOrbsInterpolationState>()
@ -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();
}

View file

@ -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 });
}
});
}