outfly/src/main.rs
2024-04-23 17:33:36 +02:00

130 lines
4.7 KiB
Rust

// ▄████████▄ + ███ + ▄█████████ ███ +
// ███▀ ▀███ + + ███ ███▀ + ███ + +
// ███ + ███ ███ ███ █████████ ███ ███ ███ ███
// ███ +███ ███ ███ ███ ███▐██████ ███ ███ ███
// ███ + ███ ███+ ███ +███ ███ + ███ ███ + ███
// ███▄ ▄███ ███▄ ███ ███ + ███ + ███ ███▄ ███
// ▀████████▀ + ▀███████ ███▄ ███▄ ▀████ ▀███████
// + + + ███
// + ▀████████████████████████████████████████████████████▀
//
// This module initializes the game, handles command-line arguments,
// and manages window-related key bindings.
mod actor;
mod audio;
mod camera;
mod chat;
mod commands;
mod effects;
mod hud;
mod shading;
mod skeleton;
mod var;
mod world;
#[allow(dead_code)]
mod nature;
use bevy::window::{Window, WindowMode, PrimaryWindow, CursorGrabMode};
use bevy::diagnostic::FrameTimeDiagnosticsPlugin;
use bevy::prelude::*;
use bevy::pbr::ExtendedMaterial;
use std::env;
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() > 1 {
if args[1] == "--version" || args[1] == "-v" {
let version = option_env!("CARGO_PKG_VERSION").unwrap();
let name = option_env!("CARGO_PKG_NAME").unwrap();
let homepage = option_env!("CARGO_PKG_HOMEPAGE").unwrap();
println!("{name} {version}");
println!("License: GNU GPL version 3: https://gnu.org/licenses/gpl.html");
println!("{homepage}");
return;
}
}
let mut app = App::new();
#[cfg(feature = "embed_assets")]
app.add_plugins(bevy_embedded_assets::EmbeddedAssetPlugin {
mode: bevy_embedded_assets::PluginMode::ReplaceDefault
});
app.add_plugins(OutFlyPlugin).run();
}
pub struct OutFlyPlugin;
impl Plugin for OutFlyPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, setup);
app.add_systems(Update, handle_input);
app.add_systems(Update, debug);
app.insert_resource(var::Settings::default());
app.insert_resource(var::GameVars::default());
app.add_plugins((
DefaultPlugins,//.set(ImagePlugin::default_nearest()),
FrameTimeDiagnosticsPlugin,
actor::ActorPlugin,
audio::AudioPlugin,
camera::CameraPlugin,
chat::ChatPlugin,
commands::CommandsPlugin,
effects::EffectsPlugin,
hud::HudPlugin,
shading::ShadingPlugin,
skeleton::SkeletonPlugin,
world::WorldPlugin,
));
}
}
fn setup(
mut windows: Query<&mut Window, With<PrimaryWindow>>,
) {
for mut window in &mut windows {
window.cursor.grab_mode = CursorGrabMode::Locked;
window.cursor.visible = false;
window.mode = WindowMode::BorderlessFullscreen;
window.title = "OutFly".to_string();
}
}
fn handle_input(
keyboard_input: Res<ButtonInput<KeyCode>>,
settings: Res<var::Settings>,
mut app_exit_events: ResMut<Events<bevy::app::AppExit>>,
mut windows: Query<&mut Window, With<PrimaryWindow>>,
) {
if keyboard_input.pressed(settings.key_exit) {
app_exit_events.send(bevy::app::AppExit);
}
if keyboard_input.just_pressed(settings.key_fullscreen) {
for mut window in &mut windows {
window.mode = if window.mode == WindowMode::Windowed {
WindowMode::BorderlessFullscreen
} else {
WindowMode::Windowed
}
}
}
}
fn debug(
settings: Res<var::Settings>,
keyboard_input: Res<ButtonInput<KeyCode>>,
mut commands: Commands,
mut extended_materials: ResMut<Assets<ExtendedMaterial<StandardMaterial, shading::AsteroidSurface>>>,
materials: Query<(Entity, Option<&Name>, &Handle<Mesh>)>,
) {
if settings.dev_mode && keyboard_input.pressed(KeyCode::KeyP) {
for (entity, _name, mesh) in &materials {
dbg!(mesh);
let mut entity = commands.entity(entity);
entity.remove::<Handle<StandardMaterial>>();
let material = extended_materials.add(shading::AsteroidSurface::material());
entity.insert(material);
}
}
}