light friends :)
parent
9a0c360172
commit
03ebb738f3
|
@ -457,6 +457,15 @@ dependencies = [
|
||||||
"glam 0.13.1",
|
"glam 0.13.1",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bevy_mod_raycast"
|
||||||
|
version = "0.2.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2b699d78034aaf5730a4c10a09fd5a2e6886964a42fa7453b9e5f8b926cd846e"
|
||||||
|
dependencies = [
|
||||||
|
"bevy",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bevy_pbr"
|
name = "bevy_pbr"
|
||||||
version = "0.5.0"
|
version = "0.5.0"
|
||||||
|
@ -2124,6 +2133,7 @@ name = "moria"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bevy",
|
"bevy",
|
||||||
|
"bevy_mod_raycast",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
|
@ -7,3 +7,4 @@ edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bevy = "0.5.0"
|
bevy = "0.5.0"
|
||||||
|
bevy_mod_raycast = "0.2.2"
|
||||||
|
|
|
@ -1,8 +1,18 @@
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
use bevy_mod_raycast::{RayCastMethod, RayCastSource};
|
||||||
|
|
||||||
use crate::player::*;
|
use crate::player::*;
|
||||||
|
|
||||||
|
pub struct MyRaycastSet;
|
||||||
|
|
||||||
pub struct Camera;
|
pub struct Camera;
|
||||||
|
pub fn spawn_camera(commands: &mut Commands) {
|
||||||
|
commands
|
||||||
|
.spawn_bundle(PerspectiveCameraBundle::default())
|
||||||
|
.insert(Camera)
|
||||||
|
.insert(RayCastSource::<MyRaycastSet>::new());
|
||||||
|
}
|
||||||
|
|
||||||
pub fn camera_follow_player(
|
pub fn camera_follow_player(
|
||||||
player: Query<(&Transform, &Player), Without<Camera>>,
|
player: Query<(&Transform, &Player), Without<Camera>>,
|
||||||
mut camera: Query<(&mut Transform, &Camera), Without<Player>>,
|
mut camera: Query<(&mut Transform, &Camera), Without<Player>>,
|
||||||
|
@ -18,3 +28,31 @@ pub fn camera_follow_player(
|
||||||
trans.look_at(player_pos, Vec3::Y);
|
trans.look_at(player_pos, Vec3::Y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// update our `RayCastSource` with the current cursor position every frame.
|
||||||
|
pub fn update_raycast_with_cursor(
|
||||||
|
mut cursor: EventReader<CursorMoved>,
|
||||||
|
mut query: Query<&mut RayCastSource<MyRaycastSet>>,
|
||||||
|
) {
|
||||||
|
for mut pick_source in query.iter_mut() {
|
||||||
|
// Grab the most recent cursor event if it exists:
|
||||||
|
if let Some(cursor_latest) = cursor.iter().last() {
|
||||||
|
pick_source.cast_method = RayCastMethod::Screenspace(cursor_latest.position);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct MouseCoords(pub Vec3);
|
||||||
|
|
||||||
|
// Update our `RayCastSource` with the current cursor position every frame.
|
||||||
|
pub fn update_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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
52
src/main.rs
52
src/main.rs
|
@ -1,16 +1,16 @@
|
||||||
use bevy::{input::system::exit_on_esc_system, pbr::AmbientLight, prelude::*};
|
use bevy::{input::system::exit_on_esc_system, pbr::AmbientLight, prelude::*};
|
||||||
|
use bevy_mod_raycast::{build_rays, update_raycast, PluginState, RayCastMesh, RaycastSystem};
|
||||||
|
|
||||||
|
mod camera;
|
||||||
|
mod columns;
|
||||||
|
mod light_balls;
|
||||||
|
mod loading;
|
||||||
|
mod player;
|
||||||
mod rendering;
|
mod rendering;
|
||||||
|
|
||||||
mod light_balls;
|
|
||||||
use light_balls::*;
|
|
||||||
mod player;
|
|
||||||
use player::*;
|
|
||||||
mod camera;
|
|
||||||
use camera::*;
|
use camera::*;
|
||||||
mod columns;
|
use light_balls::*;
|
||||||
|
use player::*;
|
||||||
mod loading;
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
|
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
|
||||||
pub enum AppState {
|
pub enum AppState {
|
||||||
|
@ -24,8 +24,31 @@ fn main() {
|
||||||
.insert_resource(ClearColor(Color::rgb(0.0, 0.0, 0.0)))
|
.insert_resource(ClearColor(Color::rgb(0.0, 0.0, 0.0)))
|
||||||
.add_plugins(rendering::CustomPlugins)
|
.add_plugins(rendering::CustomPlugins)
|
||||||
.init_resource::<LightBallMaterials>()
|
.init_resource::<LightBallMaterials>()
|
||||||
|
.init_resource::<MouseCoords>()
|
||||||
|
.init_resource::<FloatingOrbsInterpolationState>()
|
||||||
.init_resource::<loading::LoadedAssets>()
|
.init_resource::<loading::LoadedAssets>()
|
||||||
.add_state(AppState::Loading)
|
.add_state(AppState::Loading)
|
||||||
|
// raycasting
|
||||||
|
.init_resource::<PluginState<MyRaycastSet>>()
|
||||||
|
.add_system_to_stage(
|
||||||
|
CoreStage::PostUpdate,
|
||||||
|
build_rays::<MyRaycastSet>
|
||||||
|
.system()
|
||||||
|
.label(RaycastSystem::BuildRays),
|
||||||
|
)
|
||||||
|
.add_system_to_stage(
|
||||||
|
CoreStage::PostUpdate,
|
||||||
|
update_raycast::<MyRaycastSet>
|
||||||
|
.system()
|
||||||
|
.label(RaycastSystem::UpdateRaycast)
|
||||||
|
.after(RaycastSystem::BuildRays),
|
||||||
|
)
|
||||||
|
.add_system_to_stage(
|
||||||
|
CoreStage::PreUpdate,
|
||||||
|
update_raycast_with_cursor
|
||||||
|
.system()
|
||||||
|
.before(RaycastSystem::BuildRays),
|
||||||
|
)
|
||||||
// loading
|
// loading
|
||||||
.add_system_set(
|
.add_system_set(
|
||||||
SystemSet::on_update(AppState::Loading).with_system(loading::loading.system()),
|
SystemSet::on_update(AppState::Loading).with_system(loading::loading.system()),
|
||||||
|
@ -34,8 +57,11 @@ fn main() {
|
||||||
.add_system_set(SystemSet::on_enter(AppState::Game).with_system(setup.system()))
|
.add_system_set(SystemSet::on_enter(AppState::Game).with_system(setup.system()))
|
||||||
.add_system_set(
|
.add_system_set(
|
||||||
SystemSet::on_update(AppState::Game)
|
SystemSet::on_update(AppState::Game)
|
||||||
|
.with_system(update_mouse_coords.system())
|
||||||
.with_system(exit_on_esc_system.system())
|
.with_system(exit_on_esc_system.system())
|
||||||
.with_system(light_movement.system())
|
.with_system(light_movement.system())
|
||||||
|
.with_system(move_light_friends.system())
|
||||||
|
.with_system(update_floating_orbs_interpolation.system())
|
||||||
.with_system(move_player.system())
|
.with_system(move_player.system())
|
||||||
.with_system(camera_follow_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()),
|
||||||
|
@ -66,12 +92,14 @@ fn setup(
|
||||||
let floor_material = materials.add(floor_material);
|
let floor_material = materials.add(floor_material);
|
||||||
for i in -10..10 {
|
for i in -10..10 {
|
||||||
for j in -10..10 {
|
for j in -10..10 {
|
||||||
commands.spawn_bundle(PbrBundle {
|
commands
|
||||||
|
.spawn_bundle(PbrBundle {
|
||||||
mesh: meshes.add(Mesh::from(shape::Plane { size: 100.0 })),
|
mesh: meshes.add(Mesh::from(shape::Plane { size: 100.0 })),
|
||||||
material: floor_material.clone(),
|
material: floor_material.clone(),
|
||||||
transform: Transform::from_xyz(100.0 * i as f32, 0.0, 100.0 * j as f32),
|
transform: Transform::from_xyz(100.0 * i as f32, 0.0, 100.0 * j as f32),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
});
|
})
|
||||||
|
.insert(RayCastMesh::<camera::MyRaycastSet>::default());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,7 +124,5 @@ fn setup(
|
||||||
}
|
}
|
||||||
|
|
||||||
// camera
|
// camera
|
||||||
commands
|
spawn_camera(&mut commands);
|
||||||
.spawn_bundle(PerspectiveCameraBundle::default())
|
|
||||||
.insert(Camera);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
use crate::camera::MouseCoords;
|
||||||
|
|
||||||
pub struct Player;
|
pub struct Player;
|
||||||
pub struct PlayerLight {
|
pub struct PlayerLight {
|
||||||
i: u8,
|
i: u8,
|
||||||
|
@ -20,18 +22,23 @@ pub fn spawn_player(
|
||||||
transform: Transform::from_xyz(0.0, 1.0, 0.0),
|
transform: Transform::from_xyz(0.0, 1.0, 0.0),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
})
|
})
|
||||||
.insert(Player)
|
.insert(Player);
|
||||||
.with_children(|parent| {
|
|
||||||
// light
|
// light
|
||||||
let mut light_material: StandardMaterial =
|
let mut light_material: StandardMaterial = Color::rgb(0.737255, 0.560784, 0.560784).into();
|
||||||
Color::rgb(0.737255, 0.560784, 0.560784).into();
|
|
||||||
light_material.metallic = 0.5;
|
light_material.metallic = 0.5;
|
||||||
light_material.reflectance = 0.5;
|
light_material.reflectance = 0.5;
|
||||||
light_material.emissive = Color::rgb(7.52, 5.72, 5.72);
|
light_material.emissive = Color::rgb(7.52, 5.72, 5.72);
|
||||||
let light_material = materials.add(light_material);
|
let light_material = materials.add(light_material);
|
||||||
|
|
||||||
|
commands
|
||||||
|
.spawn()
|
||||||
|
.insert(LightFriends)
|
||||||
|
.insert(Transform::default())
|
||||||
|
.insert(GlobalTransform::default())
|
||||||
|
.with_children(|children| {
|
||||||
for i in 0..6 {
|
for i in 0..6 {
|
||||||
parent
|
children
|
||||||
.spawn_bundle(PbrBundle {
|
.spawn_bundle(PbrBundle {
|
||||||
mesh: meshes.add(Mesh::from(shape::Icosphere {
|
mesh: meshes.add(Mesh::from(shape::Icosphere {
|
||||||
radius: 0.2,
|
radius: 0.2,
|
||||||
|
@ -54,14 +61,56 @@ pub fn spawn_player(
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct LightFriends;
|
||||||
|
pub fn move_light_friends(
|
||||||
|
mut query: Query<(&mut Transform, &LightFriends)>,
|
||||||
|
player: Query<(&Transform, &Player), Without<LightFriends>>,
|
||||||
|
coords: Res<MouseCoords>,
|
||||||
|
interpolation: Res<FloatingOrbsInterpolationState>,
|
||||||
|
time: Res<Time>,
|
||||||
|
) {
|
||||||
|
let player_pos = if let Some(player) = player.iter().next() {
|
||||||
|
player.0.translation
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
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 mut d = trans.translation - center;
|
||||||
|
d.y = 0.0;
|
||||||
|
trans.translation -= d * time.delta_seconds();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn light_movement(mut query: Query<(&mut Transform, &PlayerLight)>, time: Res<Time>) {
|
pub fn light_movement(mut query: Query<(&mut Transform, &PlayerLight)>, time: Res<Time>) {
|
||||||
let t = time.seconds_since_startup();
|
let t = time.seconds_since_startup();
|
||||||
for (mut trans, light) in query.iter_mut() {
|
for (mut trans, light) in query.iter_mut() {
|
||||||
let i = light.i as f64;
|
let i = light.i as f64;
|
||||||
let i2 = i / 2.0;
|
let i2 = i / 2.0;
|
||||||
trans.translation.y = 3.0 * (t * 1.1 * i2 + i).sin() as f32 + 4.0;
|
trans.translation = Vec3::new(
|
||||||
trans.translation.x = 5.0 * i2 as f32 * (t * 0.4 * i2 + i).cos() as f32;
|
5.0 * i2 as f32 * (t * 0.4 * i2 + i).cos() as f32,
|
||||||
trans.translation.z = 5.0 * (t * 0.4 * i + i).sin() as f32;
|
3.0 * (t * 1.1 * i2 + i).sin() as f32 + 4.0,
|
||||||
|
5.0 * (t * 0.4 * i + i).sin() as f32,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct FloatingOrbsInterpolationState(f32);
|
||||||
|
pub fn update_floating_orbs_interpolation(
|
||||||
|
mut state: ResMut<FloatingOrbsInterpolationState>,
|
||||||
|
input: Res<Input<MouseButton>>,
|
||||||
|
time: Res<Time>,
|
||||||
|
) {
|
||||||
|
let ds = time.delta_seconds();
|
||||||
|
if input.pressed(MouseButton::Left) {
|
||||||
|
state.0 = (state.0 + 0.5 * ds).clamp(0.0, 1.0);
|
||||||
|
} else {
|
||||||
|
state.0 = (state.0 - ds).clamp(0.0, 1.0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,9 +121,6 @@ pub fn move_player(
|
||||||
) {
|
) {
|
||||||
let ds = time.delta_seconds() * 5.0;
|
let ds = time.delta_seconds() * 5.0;
|
||||||
|
|
||||||
// TODO rotate according to camera position
|
|
||||||
// but make it snap to coord system
|
|
||||||
|
|
||||||
for (mut transform, _) in query.iter_mut() {
|
for (mut transform, _) in query.iter_mut() {
|
||||||
if input.pressed(KeyCode::W) {
|
if input.pressed(KeyCode::W) {
|
||||||
transform.translation += Vec3::X * ds;
|
transform.translation += Vec3::X * ds;
|
||||||
|
|
Loading…
Reference in New Issue