You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
martian-chess/src/main.rs

136 lines
3.6 KiB
Rust

#![feature(iter_array_chunks)] // used in ray.rs
#![feature(iter_intersperse)] // used in debug.rs
mod audio;
mod credits;
mod debug;
mod display2d;
mod display3d;
mod game;
mod hit;
mod loading;
mod menu;
mod prelude;
mod tweak;
mod ui;
use std::time::Duration;
use bevy::input::ButtonState;
use crate::prelude::*;
fn main() {
if std::env::var_os("CARGO_MANIFEST_DIR").is_none() {
std::env::set_var("CARGO_MANIFEST_DIR", ".");
}
let mut app = App::new();
app.add_state::<GameState>();
app.add_state::<DisplayState>();
app.add_systems(
Update,
(
debug_state::<DisplayState>.run_if(resource_changed::<State<DisplayState>>()),
debug_state::<GameState>.run_if(resource_changed::<State<GameState>>()),
),
);
app.add_systems(
Update,
(
// TODO: Run this other times too so we ensure a camera is always active
toggle_display_camera.run_if(state_changed::<DisplayState>()),
toggle_display_camera.run_if(state_changed::<GameState>()),
),
);
app.add_plugins(
DefaultPlugins
.set(ImagePlugin::default_nearest())
.set(WindowPlugin {
primary_window: Some(Window {
title: "Martian Chess".into(),
resolution: (640., 480.).into(),
..default()
}),
..default()
})
.set(AssetPlugin {
watch_for_changes: ChangeWatcher::with_delay(Duration::from_millis(200)),
..default()
}),
);
app.add_plugins(credits::CreditsPlugin);
app.add_plugins(debug::DebugPlugin);
app.add_plugins(display2d::Display2dPlugin);
app.add_plugins(display3d::Display3dPlugin);
app.add_plugins(game::GamePlugin);
app.add_plugins(loading::LoadingPlugin);
app.add_plugins(menu::MenuPlugin);
app.add_plugins(audio::AudioPlugin);
app.add_plugins(ui::UiPlugin);
app.add_plugins(tweak::TweakPlugin);
app.run();
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default, States, Component)]
pub enum GameState {
#[default]
Loading,
Menu,
Credits,
Play,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default, States, Component)]
pub(crate) enum DisplayState {
#[default]
Display2d,
Display3d,
}
/// System for printing the current state
///
/// Only runs when state is modified.
fn debug_state<S: States>(state: Res<State<S>>) {
info!("State change {:?}", *state);
}
fn toggle_display_camera(
state: Res<State<DisplayState>>,
mut cameras: Query<(&mut Camera, &DisplayState)>,
) {
cameras.iter_mut().for_each(|(mut camera, display_state)| {
camera.is_active = display_state == state.get();
});
}
fn activate<Marker: Component>(
mut entities: Query<&mut Visibility, (With<Marker>, Without<game::Captured>)>,
) {
entities.iter_mut().for_each(|mut visibility| {
*visibility = Visibility::Visible;
});
}
fn deactivate<Marker: Component>(
mut entities: Query<&mut Visibility, (With<Marker>, Without<game::Captured>)>,
) {
entities.iter_mut().for_each(|mut visibility| {
*visibility = Visibility::Hidden;
});
}
pub(crate) fn any_component_changed<C: Component>(q: Query<Entity, Changed<C>>) -> bool {
!q.is_empty()
}
pub(crate) fn any_component_added<C: Component>(q: Query<Entity, Added<C>>) -> bool {
!q.is_empty()
}
pub(crate) fn _any_component_added_or_changed<C: Component>(
q: Query<Entity, Or<(Added<C>, Changed<C>)>>,
) -> bool {
!q.is_empty()
}