moria/src/main.rs

106 lines
3.1 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-01 10:50:56 +00:00
mod rendering;
2021-09-30 20:31:47 +00:00
mod light_balls;
use light_balls::*;
mod player;
use player::*;
2021-10-01 12:30:23 +00:00
mod camera;
use camera::*;
2021-09-30 20:31:47 +00:00
mod columns;
2021-10-01 17:56:26 +00:00
mod loading;
#[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-01 17:56:26 +00:00
.init_resource::<loading::LoadedAssets>()
.add_state(AppState::Loading)
// 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(exit_on_esc_system.system())
.with_system(light_movement.system())
.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 {
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()
});
}
}
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
commands
.spawn_bundle(PerspectiveCameraBundle {
2021-10-01 17:56:26 +00:00
transform: Transform::from_xyz(-40.0, 110.0, 40.0).looking_at(Vec3::ZERO, Vec3::Y),
2021-09-30 20:31:47 +00:00
..Default::default()
})
.insert(Camera);
}