load state actually blocks now

selection-refactor
Elijah Voigt 2 years ago
parent b8138c0129
commit 4dbf524e3f

7
Cargo.lock generated

@ -1904,6 +1904,7 @@ dependencies = [
"bytemuck", "bytemuck",
"byteorder", "byteorder",
"color_quant", "color_quant",
"jpeg-decoder",
"num-rational", "num-rational",
"num-traits", "num-traits",
"png", "png",
@ -2026,6 +2027,12 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "jpeg-decoder"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc0000e42512c92e31c2252315bda326620a4e034105e900c98ec492fa077b3e"
[[package]] [[package]]
name = "js-sys" name = "js-sys"
version = "0.3.64" version = "0.3.64"

@ -6,4 +6,4 @@ build = "build.rs"
[dependencies] [dependencies]
bevy_fmod = { version = "0.3", features = ["live-update"] } bevy_fmod = { version = "0.3", features = ["live-update"] }
bevy = "0.11" bevy = { version = "0.11", features = ["jpeg", "hdr"] }

@ -0,0 +1,35 @@
//! This example demonstrates Bevy's immediate mode drawing API intended for visual debugging.
use bevy::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(Update, system)
.run();
}
fn setup(mut commands: Commands) {
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(0., 1.5, 6.).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
// light
commands.spawn(PointLightBundle {
point_light: PointLight {
intensity: 1500.0,
shadows_enabled: true,
..default()
},
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..default()
});
}
fn system(mut gizmos: Gizmos) {
gizmos.cuboid(
Transform::from_translation(Vec3::Y * 0.5).with_scale(Vec3::splat(1.)),
Color::BLACK,
);
}

@ -79,7 +79,7 @@ fn toggle_debug_mode(
} }
fn toggle_debug_ui( fn toggle_debug_ui(
mut visibility: Query<&mut Visibility, (With<DebugRoot>, Without<Children>)>, mut visibility: Query<&mut Visibility, With<DebugRoot>>,
enabled: Option<Res<DebugEnabled>>, enabled: Option<Res<DebugEnabled>>,
) { ) {
visibility.iter_mut().for_each(|mut vis| { visibility.iter_mut().for_each(|mut vis| {
@ -94,7 +94,12 @@ fn init_debug_ui(mut commands: Commands) {
commands commands
.spawn(( .spawn((
NodeBundle { NodeBundle {
style: Style { ..default() }, style: Style {
padding: UiRect::all(Val::Px(10.0)),
..default()
},
background_color: Color::BLACK.with_a(0.6).into(),
visibility: Visibility::Hidden,
..default() ..default()
}, },
DebugRoot, DebugRoot,

@ -23,15 +23,12 @@ pub(crate) struct Display2dPlugin;
impl Plugin for Display2dPlugin { impl Plugin for Display2dPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems( app.add_systems(Startup, (initialize_camera, set_background))
Startup, .add_systems(OnEnter(GameState::Loading), load_spritesheet)
(initialize_camera, load_spritesheet, set_background) .add_systems(OnExit(GameState::Loading), initialize_board)
.run_if(in_state(GameState::Loading)),
)
.add_systems( .add_systems(
Update, Update,
( (
initialize_board.run_if(resource_added::<SpriteSheet>()),
active_tile.run_if(in_state(GameState::Display2d)), active_tile.run_if(in_state(GameState::Display2d)),
menu::exit_to_menu.run_if(in_state(GameState::Display2d)), menu::exit_to_menu.run_if(in_state(GameState::Display2d)),
select_piece select_piece
@ -100,7 +97,7 @@ fn load_spritesheet(
mut commands: Commands, mut commands: Commands,
) { ) {
let atlas = TextureAtlas::from_grid( let atlas = TextureAtlas::from_grid(
server.load("sprites.png"), server.load("images/sprites.png"),
Vec2::new(TILE_SIZE, TILE_SIZE), Vec2::new(TILE_SIZE, TILE_SIZE),
5, 5,
1, 1,
@ -120,7 +117,7 @@ fn set_background(
commands.spawn(( commands.spawn((
BackgroundImage, BackgroundImage,
SpriteBundle { SpriteBundle {
texture: server.load("mars-daybreak.png"), texture: server.load("images/mars-daybreak.png"),
sprite: Sprite { sprite: Sprite {
custom_size: Some(Vec2 { custom_size: Some(Vec2 {
x: window.single().width(), x: window.single().width(),
@ -128,6 +125,10 @@ fn set_background(
}), }),
..default() ..default()
}, },
transform: Transform {
translation: Vec3::NEG_Z,
..default()
},
..default() ..default()
}, },
)); ));

@ -4,68 +4,144 @@ pub(crate) struct Display3dPlugin;
impl Plugin for Display3dPlugin { impl Plugin for Display3dPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems( app.add_systems(Startup, initialize_camera)
Startup, .add_systems(OnEnter(GameState::Loading), load_models)
(initialize_camera, load_models).run_if(in_state(GameState::Loading)), .add_systems(OnExit(GameState::Loading), initialize_board)
)
.add_systems( .add_systems(
Update, Update,
(
initialize_board.run_if(resource_added::<ModelMap>()),
menu::exit_to_menu.run_if(in_state(GameState::Display3d)), menu::exit_to_menu.run_if(in_state(GameState::Display3d)),
),
) )
.add_systems(OnEnter(GameState::Display3d), activate); .add_systems(Update, gizmo_system.run_if(in_state(GameState::Display3d)))
.add_systems(OnEnter(GameState::Display3d), activate)
.add_systems(OnExit(GameState::Display3d), deactivate);
} }
} }
#[derive(Debug, Component)] #[derive(Debug, Component)]
struct Board3d; struct Board3d;
#[derive(Debug, Resource)]
struct ModelMap;
fn initialize_camera(mut commands: Commands) { fn initialize_camera(mut commands: Commands) {
commands.spawn(( commands.spawn((
Camera3dBundle { Camera3dBundle {
camera: Camera { camera: Camera {
is_active: false, is_active: false,
hdr: true,
..default() ..default()
}, },
transform: Transform::from_xyz(0., 1.5, 6.).looking_at(Vec3::ZERO, Vec3::Y),
..default() ..default()
}, },
UiCameraConfig { show_ui: true }, UiCameraConfig { show_ui: true },
)); ));
// light
commands.spawn(PointLightBundle {
point_light: PointLight {
intensity: 1500.0,
shadows_enabled: true,
..default()
},
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..default()
});
} }
fn load_models(server: Res<AssetServer>, mut commands: Commands) { #[derive(Debug, Resource)]
warn!("TODO: Load models"); struct ModelsFile {
handle: Handle<Gltf>,
}
commands.insert_resource(ModelMap); /// Load 3d models
/// This is kind of pulling double duty.
/// Both loads the GLTF file _and_ populates the ModelMap once that is loaded.
fn load_models(server: Res<AssetServer>, mut commands: Commands) {
commands.insert_resource(ModelsFile {
handle: server.load("models/Martian Chess.glb"),
});
} }
fn initialize_board(mut commands: Commands, model_map: Option<Res<ModelMap>>) { /// Initialize the 3d board
if let Some(models) = model_map { fn initialize_board(
warn!("TODO: Intialize 3D Board!"); mut commands: Commands,
model_file: Option<Res<ModelsFile>>,
gltfs: Res<Assets<Gltf>>,
) {
info!("Initializing board");
if let Some(mf) = model_file {
let gltf = gltfs.get(&mf.handle).expect("Load GLTF content");
commands.spawn(( info!("Initializing root");
commands
.spawn((
SpatialBundle { SpatialBundle {
visibility: Visibility::Hidden, visibility: Visibility::Hidden,
..default() ..default()
}, },
Board3d, Board3d,
))
.with_children(|parent| {
info!("Initializing 3D lights!");
parent.spawn((
Board3d,
PointLightBundle {
point_light: PointLight {
intensity: 1500.0,
shadows_enabled: true,
..default()
},
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..default()
},
));
info!("Intializeing 3D Board!");
parent.spawn((
Board3d,
SceneBundle {
scene: gltf
.named_scenes
.get("Gameboard")
.expect("Game board model")
.clone(),
..default()
},
)); ));
});
} }
} }
/// Make this the active state
fn activate( fn activate(
mut cameras: Query<&mut Camera, With<Camera3d>>, mut cameras: Query<&mut Camera, With<Camera3d>>,
mut boards: Query<&mut Visibility, With<Board3d>>, mut boards: Query<&mut Visibility, With<Board3d>>,
) { ) {
cameras.iter_mut().for_each(|mut camera| { cameras.iter_mut().for_each(|mut camera| {
info!("Activating 3d camera");
camera.is_active = true; camera.is_active = true;
}); });
boards.iter_mut().for_each(|mut visibility| { boards.iter_mut().for_each(|mut visibility| {
info!("Making entities visible");
*visibility = Visibility::Visible; *visibility = Visibility::Visible;
}); });
} }
/// Make this the non-active state
fn deactivate(
mut cameras: Query<&mut Camera, With<Camera3d>>,
mut boards: Query<&mut Visibility, With<Board3d>>,
) {
cameras.iter_mut().for_each(|mut camera| {
info!("Deactivating 3d camera");
camera.is_active = false;
});
boards.iter_mut().for_each(|mut visibility| {
info!("Making entities visible");
*visibility = Visibility::Hidden;
});
}
fn gizmo_system(mut gizmos: Gizmos) {
gizmos.cuboid(
Transform::from_translation(Vec3::Y * 0.5).with_scale(Vec3::splat(1.)),
Color::WHITE,
);
}

@ -114,7 +114,6 @@ impl Board {
impl std::fmt::Display for Board { impl std::fmt::Display for Board {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let _ = write!(f, "\n");
self.inner.iter().rev().for_each(|row| { self.inner.iter().rev().for_each(|row| {
let _ = write!(f, "+--+--+--+--+--+--+--+--+\n"); let _ = write!(f, "+--+--+--+--+--+--+--+--+\n");
let _ = write!(f, "|"); let _ = write!(f, "|");
@ -126,7 +125,7 @@ impl std::fmt::Display for Board {
}); });
let _ = write!(f, "\n"); let _ = write!(f, "\n");
}); });
let _ = write!(f, "+--+--+--+--+--+--+--+--+\n"); let _ = write!(f, "+--+--+--+--+--+--+--+--+");
Ok(()) Ok(())
} }
} }

@ -9,7 +9,8 @@ mod prelude;
use std::time::Duration; use std::time::Duration;
use bevy::{ use bevy::{
asset::ChangeWatcher, asset::{ChangeWatcher, HandleId},
gltf::{GltfMesh, GltfNode},
input::{keyboard::KeyboardInput, ButtonState}, input::{keyboard::KeyboardInput, ButtonState},
}; };
@ -65,13 +66,35 @@ pub enum GameState {
fn loading( fn loading(
server: Res<AssetServer>, server: Res<AssetServer>,
sprites: Res<Assets<Image>>, sprites: Res<Assets<Image>>,
gltfs: Res<Assets<Gltf>>,
mut next_state: ResMut<NextState<GameState>>, mut next_state: ResMut<NextState<GameState>>,
) { ) {
let items = { sprites.ids() }; let s_ids = sprites
let states = server.get_group_load_state(items); .ids()
match states { .filter(|&id| matches!(id, HandleId::AssetPathId(_)))
LoadState::Loaded | LoadState::NotLoaded => next_state.set(GameState::Menu), .collect::<Vec<HandleId>>();
_ => (),
let g_ids = gltfs
.ids()
.filter(|&id| matches!(id, HandleId::AssetPathId(_)))
.collect::<Vec<HandleId>>();
info!(
"Sprite len: {:?} | GLTF len: {:?}",
s_ids.len(),
g_ids.len()
);
if s_ids.len() > 0 && g_ids.len() > 0 {
let s_ready = s_ids
.iter()
.all(|&id| server.get_load_state(id) == LoadState::Loaded);
let g_ready = g_ids
.iter()
.all(|&id| server.get_load_state(id) == LoadState::Loaded);
if s_ready && g_ready {
next_state.set(GameState::Menu)
}
} }
} }

@ -156,7 +156,7 @@ fn handle_menu_start(
events events
.iter() .iter()
.filter(|&interaction| *interaction == Interaction::Pressed) .filter(|&interaction| *interaction == Interaction::Pressed)
.for_each(|_| next_state.set(GameState::Display2d)) .for_each(|_| next_state.set(GameState::Display3d))
} }
fn handle_menu_quit( fn handle_menu_quit(

Loading…
Cancel
Save