diff --git a/src/camera.rs b/src/camera.rs index d0746f0..d167c3d 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -1,7 +1,7 @@ use bevy::prelude::*; use bevy_mod_raycast::{RayCastMethod, RayCastSource}; -use crate::player::*; +use crate::{pillar::UnlitPillar, player::*}; pub struct MyRaycastSet; @@ -43,16 +43,36 @@ pub fn update_raycast_with_cursor( } #[derive(Default)] -pub struct MouseCoords(pub Vec3); +pub struct MouseCoords { + pub raw: Vec3, + pub processed: Vec3, +} // Update our `RayCastSource` with the current cursor position every frame. -pub fn update_mouse_coords( +pub fn update_raw_mouse_coords( query: Query<&RayCastSource>, mut coords: ResMut, ) { for pick_source in query.iter() { if let Some((_, intersection)) = pick_source.intersect_top() { - coords.0 = intersection.position(); + coords.raw = intersection.position(); + } + } +} + +/// checks for pillars and stuff +pub fn update_processed_mouse_coords( + query: Query<(&Transform, &UnlitPillar)>, + mut coords: ResMut, +) { + let mut dis = 20.0; + coords.processed = coords.raw; + + for (trans, _) in query.iter() { + let d = trans.translation.distance(coords.raw); + if d < dis { + coords.processed = trans.translation; + dis = d; } } } diff --git a/src/main.rs b/src/main.rs index 03e0fcb..7911bcf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,12 +8,14 @@ mod debug; mod illumination; mod light_balls; mod loading; +mod pillar; mod player; mod rendering; use camera::*; use debug::*; use light_balls::*; +use pillar::*; use player::*; #[derive(Clone, PartialEq, Eq, Hash, Debug)] @@ -30,8 +32,10 @@ fn main() { .add_plugin(DebugLinesPlugin) .init_resource::() .init_resource::() + .init_resource::() .init_resource::() .init_resource::() + .init_resource::() .init_resource::() .add_state(AppState::Loading) // raycasting @@ -63,7 +67,8 @@ fn main() { .add_system_set(SystemSet::on_enter(AppState::Game).with_system(setup.system())) .add_system_set( SystemSet::on_update(AppState::Game) - .with_system(update_mouse_coords.system()) + .with_system(update_raw_mouse_coords.system()) + .with_system(update_processed_mouse_coords.system()) .with_system(exit_on_esc_system.system()) .with_system(light_movement.system()) .with_system(move_light_friends.system()) @@ -71,6 +76,8 @@ fn main() { .with_system(move_player.system()) .with_system(camera_follow_player.system()) .with_system(light_up_ball_when_close_to_player.system()) + .with_system(increase_progress_when_activating_pillar.system()) + .with_system(activate_pillar_when_progress_is_1.system()) .with_system(toggle_debug.system()), ) .add_system_set( @@ -88,6 +95,7 @@ fn setup( mut materials: ResMut>, mut ambient: ResMut, light_ball_materials: Res, + pillar_materials: Res, ) { // set ambient light to very low ambient.color = Color::rgb(0.3, 0.3, 0.3); @@ -118,6 +126,8 @@ fn setup( // columns // columns::spawn_columns(&mut commands, &mut meshes, &mut materials); + spawn_pillar(&mut commands, &pillar_materials, Vec3::new(0.0, 1.0, 30.0)); + // player spawn_player(&mut commands, &mut meshes, &mut materials); diff --git a/src/pillar.rs b/src/pillar.rs new file mode 100644 index 0000000..ede200f --- /dev/null +++ b/src/pillar.rs @@ -0,0 +1,117 @@ +use bevy::prelude::*; + +use crate::player::LightFriends; + +const SIZE: f32 = 10.0; +const HEIGHT: f32 = 20.0; + +pub struct UnlitPillar; +pub struct LitPillar; + +pub fn spawn_pillar(commands: &mut Commands, materials: &PillarMaterials, pos: Vec3) { + let mut column_material: StandardMaterial = Color::rgb(0.7, 0.7, 0.7).into(); + column_material.metallic = 0.0; + column_material.reflectance = 0.0; + commands + .spawn_bundle(PbrBundle { + mesh: materials.mesh.clone(), + material: materials.unlit.clone(), + transform: Transform::from_translation(pos), + ..Default::default() + }) + .insert(UnlitPillar); +} + +#[derive(Default)] +pub struct PillarActivationProgress(pub Option<(Entity, f32)>); + +pub fn increase_progress_when_activating_pillar( + pillars: Query<(Entity, &Transform, &UnlitPillar)>, + friends: Query<(&Transform, &LightFriends)>, + mut progress: ResMut, + input: Res>, +) { + if !input.pressed(MouseButton::Left) { + return; + } + + let (friends_trans, _) = if let Some(f) = friends.iter().next() { + f + } else { + return; + }; + + for (entity, trans, _) in pillars.iter() { + let d = trans.translation.distance(friends_trans.translation); + if d < 5.0 { + // set as the one on progress and increase + if let Some(p) = &mut progress.0 { + if p.0 == entity { + p.1 = (p.1 + 0.008).clamp(0.0, 1.0); + } else { + *p = (entity, 0.0); + } + } else { + progress.0 = Some((entity, 0.0)); + } + } + } +} + +pub fn activate_pillar_when_progress_is_1( + mut commands: Commands, + mut progress: ResMut, + mut query: Query<(&mut Handle, &UnlitPillar)>, + materials: Res, +) { + if let Some(p) = progress.0 { + if p.1 >= 1.0 { + progress.0 = None; + + // activate pillar + commands + .entity(p.0) + .remove::() + .insert(LitPillar) + .insert(Light { + color: Color::rgb(15.0, 15.0, 15.0), + intensity: 300.0, + ..Default::default() + }); + + if let Ok((mut handle, _)) = query.get_mut(p.0) { + *handle = materials.lit.clone(); + } + } + } +} + +pub struct PillarMaterials { + unlit: Handle, + lit: Handle, + mesh: Handle, +} +impl FromWorld for PillarMaterials { + fn from_world(world: &mut World) -> Self { + let world = world.cell(); + let mut materials = world + .get_resource_mut::>() + .unwrap(); + let mut meshes = world.get_resource_mut::>().unwrap(); + + let mut unlit: StandardMaterial = Color::rgb(0.9, 0.9, 0.9).into(); + unlit.metallic = 0.0; + unlit.reflectance = 0.0; + let unlit = materials.add(unlit); + + let mut lit: StandardMaterial = Color::rgb(15.0, 15.0, 15.0).into(); + lit.metallic = 0.5; + lit.reflectance = 0.5; + lit.emissive = Color::rgb(15.0, 15.0, 15.0); + let lit = materials.add(lit); + + let mesh = meshes.add(Mesh::from(shape::Box::new(SIZE, HEIGHT, SIZE))); + + Self { lit, unlit, mesh } + } +} diff --git a/src/player.rs b/src/player.rs index 8fc5362..a784a73 100644 --- a/src/player.rs +++ b/src/player.rs @@ -1,6 +1,6 @@ use bevy::prelude::*; -use crate::{camera::MouseCoords, illumination::Illumination}; +use crate::{camera::MouseCoords, illumination::Illumination, pillar::PillarActivationProgress}; pub struct Player; pub struct PlayerLight { @@ -82,7 +82,7 @@ pub fn move_light_friends( for (mut trans, _light) in query.iter_mut() { // interpolate between player pos and mouse click pos let i = interpolation.0; - let center = i * coords.0 + (1.0 - i) * player_pos; + let center = i * coords.processed + (1.0 - i) * player_pos; let mut d = trans.translation - center; d.y = 0.0; @@ -94,18 +94,23 @@ pub fn light_movement( mut query: Query<(&mut Transform, &PlayerLight)>, time: Res