moria/src/main.rs

129 lines
4.0 KiB
Rust
Raw Normal View History

2021-09-30 20:31:47 +00:00
use bevy::{input::system::exit_on_esc_system, pbr::AmbientLight, prelude::*};
2021-10-02 21:48:07 +00:00
use bevy_mod_raycast::{build_rays, update_raycast, PluginState, RayCastMesh, RaycastSystem};
2021-09-30 20:31:47 +00:00
2021-10-02 21:48:07 +00:00
mod camera;
mod columns;
mod light_balls;
mod loading;
mod player;
2021-10-01 10:50:56 +00:00
mod rendering;
2021-10-02 21:48:07 +00:00
use camera::*;
2021-09-30 20:31:47 +00:00
use light_balls::*;
use player::*;
2021-10-01 17:56:26 +00:00
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub enum AppState {
Loading,
Game,
}
2021-09-30 20:31:47 +00:00
fn main() {
App::build()
.insert_resource(Msaa { samples: 4 })
2021-10-01 17:56:26 +00:00
.insert_resource(ClearColor(Color::rgb(0.0, 0.0, 0.0)))
2021-10-01 10:50:56 +00:00
.add_plugins(rendering::CustomPlugins)
2021-09-30 20:31:47 +00:00
.init_resource::<LightBallMaterials>()
2021-10-02 21:48:07 +00:00
.init_resource::<MouseCoords>()
.init_resource::<FloatingOrbsInterpolationState>()
2021-10-01 17:56:26 +00:00
.init_resource::<loading::LoadedAssets>()
.add_state(AppState::Loading)
2021-10-02 21:48:07 +00:00
// 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),
)
2021-10-01 17:56:26 +00:00
// loading
.add_system_set(
SystemSet::on_update(AppState::Loading).with_system(loading::loading.system()),
)
// game
.add_system_set(SystemSet::on_enter(AppState::Game).with_system(setup.system()))
.add_system_set(
SystemSet::on_update(AppState::Game)
2021-10-02 21:48:07 +00:00
.with_system(update_mouse_coords.system())
2021-10-01 17:56:26 +00:00
.with_system(exit_on_esc_system.system())
.with_system(light_movement.system())
2021-10-02 21:48:07 +00:00
.with_system(move_light_friends.system())
.with_system(update_floating_orbs_interpolation.system())
2021-10-01 17:56:26 +00:00
.with_system(move_player.system())
.with_system(camera_follow_player.system())
.with_system(light_up_ball_when_close_to_player.system()),
)
2021-09-30 20:31:47 +00:00
.run();
}
fn setup(
mut commands: Commands,
2021-10-01 17:56:26 +00:00
assets: Res<loading::LoadedAssets>,
2021-09-30 20:31:47 +00:00
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut ambient: ResMut<AmbientLight>,
light_ball_materials: Res<LightBallMaterials>,
) {
2021-10-01 12:30:23 +00:00
// set ambient light to very low
2021-09-30 20:31:47 +00:00
ambient.color = Color::rgb(0.3, 0.3, 0.3);
2021-10-01 17:56:26 +00:00
ambient.brightness = 0.01;
2021-09-30 20:31:47 +00:00
// floor
2021-10-01 17:56:26 +00:00
let mut floor_material = StandardMaterial {
base_color_texture: Some(assets.floor.clone()),
base_color: Color::rgb(0.3, 0.3, 0.3),
..Default::default()
};
2021-09-30 20:31:47 +00:00
floor_material.metallic = 0.0;
floor_material.reflectance = 0.0;
let floor_material = materials.add(floor_material);
2021-10-01 17:56:26 +00:00
for i in -10..10 {
for j in -10..10 {
2021-10-02 21:48:07 +00:00
commands
.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 100.0 })),
material: floor_material.clone(),
transform: Transform::from_xyz(100.0 * i as f32, 0.0, 100.0 * j as f32),
..Default::default()
})
.insert(RayCastMesh::<camera::MyRaycastSet>::default());
2021-10-01 17:56:26 +00:00
}
}
2021-09-30 20:31:47 +00:00
// columns
2021-10-01 17:56:26 +00:00
// columns::spawn_columns(&mut commands, &mut meshes, &mut materials);
2021-09-30 20:31:47 +00:00
// player
spawn_player(&mut commands, &mut meshes, &mut materials);
// light balls
for i in 1..10 {
spawn_light_ball(
&mut commands,
&light_ball_materials,
2021-10-01 17:56:26 +00:00
Vec3::new(i as f32 * 30.0, 2.0, 10.0),
2021-09-30 20:31:47 +00:00
);
spawn_light_ball(
&mut commands,
&light_ball_materials,
2021-10-01 17:56:26 +00:00
Vec3::new(i as f32 * 30.0, 2.0, -10.0),
2021-09-30 20:31:47 +00:00
);
}
// camera
2021-10-02 21:48:07 +00:00
spawn_camera(&mut commands);
2021-09-30 20:31:47 +00:00
}