outfly/src/main.rs

212 lines
7.3 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 cmd;
mod hud;
mod load;
mod menu;
mod var;
mod visual;
mod world;
#[allow(dead_code)]
mod nature;
pub mod prelude {
pub use crate::var::{FONT, Settings};
pub use crate::load::load_asset;
pub fn bool2vis(boolean: bool) -> bevy::prelude::Visibility {
if boolean {
bevy::prelude::Visibility::Inherited
} else {
bevy::prelude::Visibility::Hidden
}
}
}
use bevy::window::{Window, WindowMode, PrimaryWindow, CursorGrabMode};
use bevy::diagnostic::FrameTimeDiagnosticsPlugin;
use bevy::prelude::*;
use bevy::pbr::ExtendedMaterial;
use std::env;
const HELP: &str = "./outfly [options]
Options:
--version, -v show this program's version number and exit
--help, -h show this help message and exit
--gl use GL rendering instead of Vulkan
--fs-legacy use 'legacy' (non-borderless) fullscreen mode
--fs-sized use 'sized' (non-borderless) fullscreen mode
--windowed start in non-fullscreen mode
Note: borderless fullscreen is the default, but it crashes on some systems.";
fn main() {
let prefs = var::load_prefs();
let mut opt = CommandLineOptions {
window_mode_fullscreen: prefs.get_fullscreen_mode(),
window_mode_initial: prefs.get_window_mode(),
use_gl: prefs.render_mode_is_gl(),
};
let args: Vec<String> = env::args().collect();
if args.len() > 1 {
for arg in &args[1..] {
if arg == "--help" || arg == "-h" {
println!("{}", HELP);
return;
}
else if arg == "--version" || arg == "-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;
}
else if arg == "--gl" {
opt.use_gl = true;
}
else if arg == "--windowed" {
opt.window_mode_initial = WindowMode::Windowed;
}
else if arg == "--fs-legacy" {
let mode = WindowMode::Fullscreen;
if opt.window_mode_initial == opt.window_mode_fullscreen {
opt.window_mode_initial = mode;
}
opt.window_mode_fullscreen = mode;
}
else if arg == "--fs-sized" {
let mode = WindowMode::SizedFullscreen;
if opt.window_mode_initial == opt.window_mode_fullscreen {
opt.window_mode_initial = mode;
}
opt.window_mode_fullscreen = mode;
}
else {
println!("{}", HELP);
println!("\nERROR: no such option: `{}`", arg);
return;
}
}
}
if opt.use_gl {
env::set_var("WGPU_BACKEND", "gl");
}
let mut app = App::new();
app.insert_resource(opt);
#[cfg(feature = "embed_assets")]
app.add_plugins(bevy_embedded_assets::EmbeddedAssetPlugin {
mode: bevy_embedded_assets::PluginMode::ReplaceDefault
});
app.add_plugins(OutFlyPlugin);
app.insert_resource(prefs);
app.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,
cmd::CmdPlugin,
menu::MenuPlugin,
visual::VisualPlugin,
hud::HudPlugin,
load::LoadPlugin,
world::WorldPlugin,
));
}
}
#[derive(Resource, Default)]
pub struct CommandLineOptions {
window_mode_fullscreen: WindowMode,
window_mode_initial: WindowMode,
use_gl: bool,
}
fn setup(
mut windows: Query<&mut Window, With<PrimaryWindow>>,
opt: Res<CommandLineOptions>,
) {
for mut window in &mut windows {
window.cursor.grab_mode = CursorGrabMode::Locked;
window.cursor.visible = false;
window.mode = opt.window_mode_initial;
window.title = "OutFly".to_string();
}
}
fn handle_input(
keyboard_input: Res<ButtonInput<KeyCode>>,
settings: Res<var::Settings>,
opt: Res<CommandLineOptions>,
mut app_exit_events: ResMut<Events<bevy::app::AppExit>>,
mut windows: Query<&mut Window, With<PrimaryWindow>>,
mut ew_sfx: EventWriter<audio::PlaySfxEvent>,
) {
if keyboard_input.pressed(settings.key_exit) {
app_exit_events.send(bevy::app::AppExit);
}
if keyboard_input.just_pressed(settings.key_fullscreen) {
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Click));
for mut window in &mut windows {
window.mode = if window.mode == WindowMode::Windowed {
opt.window_mode_fullscreen
} else {
WindowMode::Windowed
}
}
}
}
fn debug(
settings: Res<var::Settings>,
keyboard_input: Res<ButtonInput<KeyCode>>,
mut commands: Commands,
mut extended_materials: ResMut<Assets<ExtendedMaterial<StandardMaterial, load::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(load::AsteroidSurface::material());
entity.insert(material);
}
}
}