moria/src/main.rs

129 lines
4.0 KiB
Rust

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;
use camera::*;
use light_balls::*;
use player::*;
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub enum AppState {
Loading,
Game,
}
fn main() {
App::build()
.insert_resource(Msaa { samples: 4 })
.insert_resource(ClearColor(Color::rgb(0.0, 0.0, 0.0)))
.add_plugins(rendering::CustomPlugins)
.init_resource::<LightBallMaterials>()
.init_resource::<MouseCoords>()
.init_resource::<FloatingOrbsInterpolationState>()
.init_resource::<loading::LoadedAssets>()
.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
.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)
.with_system(update_mouse_coords.system())
.with_system(exit_on_esc_system.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(camera_follow_player.system())
.with_system(light_up_ball_when_close_to_player.system()),
)
.run();
}
fn setup(
mut commands: Commands,
assets: Res<loading::LoadedAssets>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut ambient: ResMut<AmbientLight>,
light_ball_materials: Res<LightBallMaterials>,
) {
// set ambient light to very low
ambient.color = Color::rgb(0.3, 0.3, 0.3);
ambient.brightness = 0.01;
// floor
let mut floor_material = StandardMaterial {
base_color_texture: Some(assets.floor.clone()),
base_color: Color::rgb(0.3, 0.3, 0.3),
..Default::default()
};
floor_material.metallic = 0.0;
floor_material.reflectance = 0.0;
let floor_material = materials.add(floor_material);
for i in -10..10 {
for j in -10..10 {
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());
}
}
// columns
// columns::spawn_columns(&mut commands, &mut meshes, &mut materials);
// player
spawn_player(&mut commands, &mut meshes, &mut materials);
// light balls
for i in 1..10 {
spawn_light_ball(
&mut commands,
&light_ball_materials,
Vec3::new(i as f32 * 30.0, 2.0, 10.0),
);
spawn_light_ball(
&mut commands,
&light_ball_materials,
Vec3::new(i as f32 * 30.0, 2.0, -10.0),
);
}
// camera
spawn_camera(&mut commands);
}