|
|
|
|
@ -12,9 +12,10 @@ use fighter::*;
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
|
App::new()
|
|
|
|
|
.add_plugins((DefaultPlugins, BlocksPlugin, FighterPlugin, DebugPlugin))
|
|
|
|
|
.add_plugins(DefaultPlugins)
|
|
|
|
|
.add_plugins(FeathersPlugins)
|
|
|
|
|
.add_plugins((BlocksPlugin, FighterPlugin, DebugPlugin))
|
|
|
|
|
.init_state::<Loading>()
|
|
|
|
|
.init_state::<Debugger>()
|
|
|
|
|
.init_state::<GameState>()
|
|
|
|
|
.init_resource::<AllAssets>()
|
|
|
|
|
.init_resource::<SetupChecklist>()
|
|
|
|
|
@ -22,28 +23,47 @@ fn main() {
|
|
|
|
|
.add_systems(
|
|
|
|
|
Update,
|
|
|
|
|
loading_check
|
|
|
|
|
.run_if(in_state(Loading::Active))
|
|
|
|
|
.run_if(in_state(Loading(true)))
|
|
|
|
|
.run_if(resource_changed::<AllAssets>),
|
|
|
|
|
)
|
|
|
|
|
// Wait for pending assets to be loaded
|
|
|
|
|
.add_systems(Update, loading_wait.run_if(in_state(Loading::Active)))
|
|
|
|
|
.add_systems(Update, loading_wait.run_if(in_state(Loading(true))))
|
|
|
|
|
// Once done loading, move to the setup state
|
|
|
|
|
.add_systems(OnEnter(Loading::Idle), setup_game)
|
|
|
|
|
.add_systems(OnEnter(Loading(false)), setup_game)
|
|
|
|
|
// Check if the game is ready to progress
|
|
|
|
|
.add_systems(Update, setup_wait.run_if(in_state(GameState::Setup)))
|
|
|
|
|
// State toggles
|
|
|
|
|
.add_systems(Update, (
|
|
|
|
|
(
|
|
|
|
|
toggle_state_visibility::<Loading>,
|
|
|
|
|
sync_state_to_ui::<Loading>,
|
|
|
|
|
).run_if(state_changed::<Loading>),
|
|
|
|
|
(
|
|
|
|
|
toggle_state_visibility::<GameState>,
|
|
|
|
|
sync_state_to_ui::<GameState>,
|
|
|
|
|
).run_if(state_changed::<GameState>)
|
|
|
|
|
))
|
|
|
|
|
.run();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Reports if the game is loading assets
|
|
|
|
|
#[derive(States, Default, Clone, Eq, Debug, PartialEq, Hash)]
|
|
|
|
|
enum Loading {
|
|
|
|
|
#[default]
|
|
|
|
|
Active,
|
|
|
|
|
Idle,
|
|
|
|
|
#[derive(States, Clone, Eq, Debug, PartialEq, Hash, Component)]
|
|
|
|
|
struct Loading(bool);
|
|
|
|
|
|
|
|
|
|
impl Default for Loading {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Loading(true)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl fmt::Display for Loading {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
write!(f, "Loading {}", self.0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Tracks what state the main game loop is in
|
|
|
|
|
#[derive(States, Default, Clone, Eq, Debug, PartialEq, Hash)]
|
|
|
|
|
#[derive(States, Default, Clone, Eq, Debug, PartialEq, Hash, Component)]
|
|
|
|
|
enum GameState {
|
|
|
|
|
#[default]
|
|
|
|
|
Boot,
|
|
|
|
|
@ -51,6 +71,16 @@ enum GameState {
|
|
|
|
|
Run,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl fmt::Display for GameState {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
match self {
|
|
|
|
|
GameState::Boot => write!(f, "Game State: Boot"),
|
|
|
|
|
GameState::Setup => write!(f, "GameState: Setup"),
|
|
|
|
|
GameState::Run => write!(f, "GameState: Run"),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A list of all assets so we don't lose them
|
|
|
|
|
#[derive(Default, Resource, Debug)]
|
|
|
|
|
struct AllAssets {
|
|
|
|
|
@ -73,23 +103,23 @@ impl SetupChecklist {
|
|
|
|
|
fn loading_check(mut next: ResMut<NextState<Loading>>, all_assets: Res<AllAssets>) {
|
|
|
|
|
debug_assert!(all_assets.is_changed());
|
|
|
|
|
|
|
|
|
|
next.set(Loading::Active);
|
|
|
|
|
next.set(Loading(true));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Waits in Loading::Active until all assets are loaded then move to Loading::Idle
|
|
|
|
|
/// Waits in Loading::Active until all assets are loaded then move to Loading(false)
|
|
|
|
|
fn loading_wait(
|
|
|
|
|
curr: Res<State<Loading>>,
|
|
|
|
|
mut next: ResMut<NextState<Loading>>,
|
|
|
|
|
server: Res<AssetServer>,
|
|
|
|
|
all_assets: Res<AllAssets>,
|
|
|
|
|
) {
|
|
|
|
|
debug_assert!(*curr.get() == Loading::Active);
|
|
|
|
|
debug_assert!(*curr.get() == Loading(true));
|
|
|
|
|
if all_assets
|
|
|
|
|
.handles
|
|
|
|
|
.iter()
|
|
|
|
|
.all(|h| matches!(server.get_load_state(h.id()), Some(LoadState::Loaded)))
|
|
|
|
|
{
|
|
|
|
|
next.set(Loading::Idle);
|
|
|
|
|
next.set(Loading(false));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|