add pillars and stuff

main
annieversary 2021-10-16 22:31:32 +02:00
parent c9ecaffd6b
commit 2e8245eb92
4 changed files with 164 additions and 12 deletions

View File

@ -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<MyRaycastSet>>,
mut coords: ResMut<MouseCoords>,
) {
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<MouseCoords>,
) {
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;
}
}
}

View File

@ -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::<Debug>()
.init_resource::<LightBallMaterials>()
.init_resource::<PillarMaterials>()
.init_resource::<MouseCoords>()
.init_resource::<FloatingOrbsInterpolationState>()
.init_resource::<PillarActivationProgress>()
.init_resource::<loading::LoadedAssets>()
.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<Assets<StandardMaterial>>,
mut ambient: ResMut<AmbientLight>,
light_ball_materials: Res<LightBallMaterials>,
pillar_materials: Res<PillarMaterials>,
) {
// 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);

117
src/pillar.rs Normal file
View File

@ -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<PillarActivationProgress>,
input: Res<Input<MouseButton>>,
) {
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<PillarActivationProgress>,
mut query: Query<(&mut Handle<StandardMaterial>, &UnlitPillar)>,
materials: Res<PillarMaterials>,
) {
if let Some(p) = progress.0 {
if p.1 >= 1.0 {
progress.0 = None;
// activate pillar
commands
.entity(p.0)
.remove::<UnlitPillar>()
.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<StandardMaterial>,
lit: Handle<StandardMaterial>,
mesh: Handle<Mesh>,
}
impl FromWorld for PillarMaterials {
fn from_world(world: &mut World) -> Self {
let world = world.cell();
let mut materials = world
.get_resource_mut::<Assets<StandardMaterial>>()
.unwrap();
let mut meshes = world.get_resource_mut::<Assets<Mesh>>().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 }
}
}

View File

@ -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<Time>,
interpolation: Res<FloatingOrbsInterpolationState>,
pillar_progress: Res<PillarActivationProgress>,
) {
let t = time.seconds_since_startup();
// make the friends go further when the button is pressed
let width = 5.0 + interpolation.0 * 7.0;
// light pillar activation progress
let p = pillar_progress.0.map(|(_, p)| p).unwrap_or(0.0);
// make the friends go further when the button is pressed, but close in when activating a pillar
let width = (5.0 + interpolation.0 * 7.0) * (1.0 - p);
let p = p as f64 / 3.0;
for (mut trans, light) in query.iter_mut() {
let i = light.i as f64;
let i2 = i / 2.0;
trans.translation = Vec3::new(
width * i2 as f32 * (t * 0.4 * i2 + i).cos() as f32,
3.0 * (t * 1.1 * i2 + i).sin() as f32 + 4.0,
width * (t * 0.4 * i + i).sin() as f32,
width * i2 as f32 * (t * (0.4 + p) * i2 + i).cos() as f32,
3.0 * (t * (1.1 + p) * i2 + i).sin() as f32 + 4.0,
width * (t * (0.4 + p) * i + i).sin() as f32,
);
}
}