Loading screen. Ironing out bugs with color change?

selection-refactor
Elijah Voigt 2 years ago
parent 6dac7f5e4d
commit f87bfb5a6c

@ -22,14 +22,8 @@ impl Plugin for CreditsPlugin {
menu::exit_to_menu.run_if(in_state(GameState::Credits)), menu::exit_to_menu.run_if(in_state(GameState::Credits)),
), ),
) )
.add_systems( .add_systems(OnEnter(GameState::Credits), activate::<Credits>)
OnEnter(GameState::Credits), .add_systems(OnExit(GameState::Credits), deactivate::<Credits>);
activate::<Credits, CreditsCamera>,
)
.add_systems(
OnExit(GameState::Credits),
deactivate::<Credits, CreditsCamera>,
);
} }
} }
@ -77,12 +71,9 @@ fn parse_credits(bytes: &[u8]) -> Result<CreditsText, Utf8Error> {
#[derive(Debug, Component)] #[derive(Debug, Component)]
struct Credits; struct Credits;
#[derive(Debug, Component)]
struct CreditsCamera;
fn init_credits_ui(mut commands: Commands, server: Res<AssetServer>) { fn init_credits_ui(mut commands: Commands, server: Res<AssetServer>) {
commands.spawn(( commands.spawn((
CreditsCamera, Credits,
Camera2dBundle { Camera2dBundle {
camera: Camera { camera: Camera {
is_active: false, is_active: false,

@ -44,19 +44,15 @@ impl Plugin for Display2dPlugin {
.run_if(in_state(GameState::Display2d)) .run_if(in_state(GameState::Display2d))
.run_if(any_component_removed::<game::Selected>()), .run_if(any_component_removed::<game::Selected>()),
update_background.run_if(on_event::<WindowResized>()), update_background.run_if(on_event::<WindowResized>()),
// PERF: Only need to run when piece2d or tile2d added set_transform
set_sprite.after(game::set_side), .after(game::update_board::<Piece2d>)
set_transform.after(game::update_board::<Piece2d>), .run_if(any_component_changed::<BoardIndex>),
set_piece_sprite.run_if(any_component_changed::<Side>),
set_tile_sprite.run_if(any_component_added::<Tile>),
), ),
) )
.add_systems( .add_systems(OnEnter(GameState::Display2d), activate::<Display2d>)
OnEnter(GameState::Display2d), .add_systems(OnExit(GameState::Display2d), deactivate::<Display2d>);
activate::<Board2d, Display2dCamera>,
)
.add_systems(
OnExit(GameState::Display2d),
deactivate::<Board2d, Display2dCamera>,
);
} }
} }
@ -66,9 +62,9 @@ struct SpriteSheet {
handle: Handle<TextureAtlas>, handle: Handle<TextureAtlas>,
} }
/// Marker component for the 2d board entity /// Marker component for the 2d entitys
#[derive(Debug, Component)] #[derive(Debug, Component)]
struct Board2d; struct Display2d;
/// Marker for 2d piece entities /// Marker for 2d piece entities
#[derive(Debug, Component)] #[derive(Debug, Component)]
@ -77,16 +73,13 @@ pub(crate) struct Piece2d;
#[derive(Debug, Component)] #[derive(Debug, Component)]
struct Tile2d; struct Tile2d;
#[derive(Debug, Component)]
struct Display2dCamera;
#[derive(Debug, Component)] #[derive(Debug, Component)]
struct BackgroundImage; struct BackgroundImage;
/// STARTUP: Initialize 2d gameplay Camera /// STARTUP: Initialize 2d gameplay Camera
fn initialize_camera(mut commands: Commands) { fn initialize_camera(mut commands: Commands) {
commands.spawn(( commands.spawn((
Display2dCamera, Display2d,
Camera2dBundle { Camera2dBundle {
camera: Camera { camera: Camera {
is_active: false, is_active: false,
@ -163,11 +156,11 @@ fn initialize_board(board: Option<Res<Board>>, mut commands: Commands) {
if let Some(board) = board { if let Some(board) = board {
commands commands
.spawn(( .spawn((
Display2d,
SpatialBundle { SpatialBundle {
visibility: Visibility::Hidden, visibility: Visibility::Hidden,
..default() ..default()
}, },
Board2d,
)) ))
.with_children(|parent| { .with_children(|parent| {
// Spawn tiles // Spawn tiles
@ -198,41 +191,54 @@ fn initialize_board(board: Option<Res<Board>>, mut commands: Commands) {
} }
} }
fn set_sprite( fn set_piece_sprite(
mut events: Query< mut events: Query<
( (
&mut TextureAtlasSprite, &mut TextureAtlasSprite,
&mut Handle<TextureAtlas>, &mut Handle<TextureAtlas>,
Option<&Piece>, &Piece,
Option<&Side>, &Side,
Option<&Tile>,
), ),
Or<(Added<Piece>, Added<Tile2d>, Changed<Side>)>, Or<(Added<Piece>, Changed<Side>)>,
>, >,
sprite_sheet: Option<Res<SpriteSheet>>, sprite_sheet: Option<Res<SpriteSheet>>,
) { ) {
if let Some(sprite_sheet) = sprite_sheet { if let Some(sprite_sheet) = sprite_sheet {
events events
.iter_mut() .iter_mut()
.for_each(|(mut sprite, mut texture_atlas, piece, side, tile)| { .for_each(|(mut sprite, mut texture_atlas, piece, side)| {
info!("Got sprite update event"); debug!("Updating sprite {:?} {:?}", piece, side);
if *texture_atlas != sprite_sheet.handle {
*texture_atlas = sprite_sheet.handle.clone();
}
sprite.index = match (piece, side) {
(Piece::Queen, Side::A) => 2,
(Piece::Queen, Side::B) => 5,
(Piece::Drone, Side::A) => 3,
(Piece::Drone, Side::B) => 6,
(Piece::Pawn, Side::A) => 4,
(Piece::Pawn, Side::B) => 7,
};
});
}
}
fn set_tile_sprite(
mut events: Query<(&mut TextureAtlasSprite, &mut Handle<TextureAtlas>, &Tile), Added<Tile2d>>,
sprite_sheet: Option<Res<SpriteSheet>>,
) {
if let Some(sprite_sheet) = sprite_sheet {
events
.iter_mut()
.for_each(|(mut sprite, mut texture_atlas, tile)| {
*texture_atlas = sprite_sheet.handle.clone(); *texture_atlas = sprite_sheet.handle.clone();
let s = match (piece, side) { let s = match tile {
(Some(Piece::Queen), Some(Side::A)) => 2, Tile::Dark => 0,
(Some(Piece::Queen), Some(Side::B)) => 5, Tile::Light => 1,
(Some(Piece::Drone), Some(Side::A)) => 3,
(Some(Piece::Drone), Some(Side::B)) => 6,
(Some(Piece::Pawn), Some(Side::A)) => 4,
(Some(Piece::Pawn), Some(Side::B)) => 7,
_ => match tile {
Some(Tile::Dark) => 0,
Some(Tile::Light) => 1,
None => 99,
},
}; };
*sprite = TextureAtlasSprite::new(s); *sprite = TextureAtlasSprite::new(s);
debug!("Setting sprite for {:?} {:?}", piece, tile);
}); });
} }
} }
@ -263,7 +269,7 @@ fn active_tile(
&GlobalTransform, &GlobalTransform,
&BoardIndex, &BoardIndex,
)>, )>,
camera_q: Query<(&Camera, &GlobalTransform), With<Camera2d>>, camera_q: Query<(&Camera, &GlobalTransform), With<Display2d>>,
atlases: Res<Assets<TextureAtlas>>, atlases: Res<Assets<TextureAtlas>>,
mut active: ResMut<ActiveTile>, mut active: ResMut<ActiveTile>,
) { ) {
@ -321,10 +327,7 @@ fn select_piece(
((*button, *state) == (MouseButton::Left, ButtonState::Pressed)).then_some(entity) ((*button, *state) == (MouseButton::Left, ButtonState::Pressed)).then_some(entity)
}) })
.for_each(|entity| { .for_each(|entity| {
commands commands.entity(entity).insert(game::Selected);
.entity(entity)
.insert(game::Selected)
.remove_parent();
writer.send(game::GameEvent::SelectPiece); writer.send(game::GameEvent::SelectPiece);
}); });
} }
@ -332,7 +335,7 @@ fn select_piece(
fn move_piece( fn move_piece(
window: Query<&Window, With<PrimaryWindow>>, window: Query<&Window, With<PrimaryWindow>>,
mut query: Query<&mut Transform, (With<game::Selected>, With<Piece2d>)>, mut query: Query<&mut Transform, (With<game::Selected>, With<Piece2d>)>,
camera_query: Query<(&Camera, &GlobalTransform), With<Display2dCamera>>, camera_query: Query<(&Camera, &GlobalTransform), With<Display2d>>,
) { ) {
query.iter_mut().for_each(|mut t| { query.iter_mut().for_each(|mut t| {
let (camera, camera_t) = camera_query.single(); let (camera, camera_t) = camera_query.single();

@ -21,29 +21,20 @@ impl Plugin for Display3dPlugin {
.run_if(in_state(GameState::Display3d)) .run_if(in_state(GameState::Display3d))
.run_if(resource_exists::<debug::DebugEnabled>()), .run_if(resource_exists::<debug::DebugEnabled>()),
) )
.add_systems( .add_systems(OnEnter(GameState::Display3d), activate::<Display3d>)
OnEnter(GameState::Display3d), .add_systems(OnExit(GameState::Display3d), deactivate::<Display3d>);
activate::<Board3d, Display3dCamera>,
)
.add_systems(
OnExit(GameState::Display3d),
deactivate::<Board3d, Display3dCamera>,
);
} }
} }
#[derive(Debug, Component)] #[derive(Debug, Component)]
struct Board3d; struct Display3d;
#[derive(Debug, Component)] #[derive(Debug, Component)]
pub(crate) struct Piece3d; pub(crate) struct Piece3d;
#[derive(Debug, Component)]
struct Display3dCamera;
fn initialize_camera(mut commands: Commands) { fn initialize_camera(mut commands: Commands) {
commands.spawn(( commands.spawn((
Display3dCamera, Display3d,
Camera3dBundle { Camera3dBundle {
camera: Camera { camera: Camera {
is_active: false, is_active: false,
@ -103,12 +94,12 @@ fn initialize_board(
visibility: Visibility::Hidden, visibility: Visibility::Hidden,
..default() ..default()
}, },
Board3d, Display3d,
)) ))
.with_children(|parent| { .with_children(|parent| {
info!("Initializing 3D lights!"); info!("Initializing 3D lights!");
parent.spawn(( parent.spawn((
Board3d, Display3d,
PointLightBundle { PointLightBundle {
point_light: PointLight { point_light: PointLight {
intensity: 1500.0, intensity: 1500.0,
@ -123,7 +114,7 @@ fn initialize_board(
info!("Intializeing 3D Board!"); info!("Intializeing 3D Board!");
parent parent
.spawn(( .spawn((
Board3d, Display3d,
SceneBundle { SceneBundle {
scene: gltf scene: gltf
.named_scenes .named_scenes

@ -13,15 +13,13 @@ impl Plugin for GamePlugin {
( (
update_board::<display2d::Piece2d>.run_if(on_event::<Move>()), update_board::<display2d::Piece2d>.run_if(on_event::<Move>()),
update_board::<display3d::Piece3d>.run_if(on_event::<Move>()), update_board::<display3d::Piece3d>.run_if(on_event::<Move>()),
set_side.run_if(on_event::<GameEvent>()), // TODO: correct run_if? set_side.run_if(on_event::<Move>()), // TODO: correct run_if?
), ),
) )
.add_systems( .add_systems(
PostUpdate, PostUpdate,
( (
debug_hovering debug_hovering.run_if(resource_exists::<debug::DebugEnabled>()),
.run_if(resource_exists::<debug::DebugEnabled>())
.run_if(resource_changed::<ActiveTile>()),
debug_board.run_if(resource_exists::<debug::DebugEnabled>()), debug_board.run_if(resource_exists::<debug::DebugEnabled>()),
), ),
); );
@ -290,6 +288,7 @@ pub(crate) fn set_side(mut events: Query<(&mut Side, &BoardIndex), Changed<Board
.iter_mut() .iter_mut()
.for_each(|(mut side, idx)| match Board::side(idx) { .for_each(|(mut side, idx)| match Board::side(idx) {
Ok(s) => { Ok(s) => {
debug!("Set side event {:?} {:?} -> {:?}", idx, side, s);
if *side != s { if *side != s {
*side = s *side = s
} }

@ -0,0 +1,62 @@
use crate::prelude::*;
pub(crate) struct LoadingPlugin;
impl Plugin for LoadingPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, initialize)
.add_systems(OnEnter(GameState::Loading), activate::<Loading>)
.add_systems(OnExit(GameState::Loading), deactivate::<Loading>);
}
}
#[derive(Debug, Component)]
struct Loading;
fn initialize(mut commands: Commands) {
commands.spawn((
Loading,
Camera2dBundle {
camera: Camera {
is_active: false,
..default()
},
..default()
},
UiCameraConfig { show_ui: true },
));
commands
.spawn((
Loading,
NodeBundle {
style: Style {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
position_type: PositionType::Absolute,
..default()
},
visibility: Visibility::Hidden,
..default()
},
))
.with_children(|parent| {
parent.spawn((TextBundle {
text: Text {
alignment: TextAlignment::Center,
sections: vec![TextSection {
value: "l o a d i n g . . .".into(),
style: TextStyle {
color: Color::WHITE.into(),
..default()
},
}],
..default()
},
background_color: Color::BLACK.with_a(0.5).into(),
..default()
},));
});
}

@ -4,6 +4,7 @@ mod debug;
mod display2d; mod display2d;
mod display3d; mod display3d;
mod game; mod game;
mod loading;
mod menu; mod menu;
mod prelude; mod prelude;
@ -45,12 +46,13 @@ fn main() {
..default() ..default()
}), }),
audio::AudioPlugin, audio::AudioPlugin,
credits::CreditsPlugin,
debug::DebugPlugin, debug::DebugPlugin,
display2d::Display2dPlugin, display2d::Display2dPlugin,
display3d::Display3dPlugin, display3d::Display3dPlugin,
game::GamePlugin, game::GamePlugin,
loading::LoadingPlugin,
menu::MenuPlugin, menu::MenuPlugin,
credits::CreditsPlugin,
)) ))
.run(); .run();
} }
@ -126,9 +128,9 @@ fn toggle_display_mode(
}) })
} }
fn activate<E: Component, C: Component>( fn activate<Marker: Component>(
mut cameras: Query<&mut Camera, With<C>>, mut cameras: Query<&mut Camera, With<Marker>>,
mut entities: Query<&mut Visibility, With<E>>, mut entities: Query<&mut Visibility, With<Marker>>,
) { ) {
cameras.iter_mut().for_each(|mut camera| { cameras.iter_mut().for_each(|mut camera| {
camera.is_active = true; camera.is_active = true;
@ -138,9 +140,9 @@ fn activate<E: Component, C: Component>(
}); });
} }
fn deactivate<E: Component, C: Component>( fn deactivate<Marker: Component>(
mut cameras: Query<&mut Camera, With<C>>, mut cameras: Query<&mut Camera, With<Marker>>,
mut entities: Query<&mut Visibility, With<E>>, mut entities: Query<&mut Visibility, With<Marker>>,
) { ) {
cameras.iter_mut().for_each(|mut camera| { cameras.iter_mut().for_each(|mut camera| {
camera.is_active = false; camera.is_active = false;
@ -149,3 +151,11 @@ fn deactivate<E: Component, C: Component>(
*visibility = Visibility::Hidden; *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()
}

@ -21,23 +21,20 @@ impl Plugin for MenuPlugin {
) )
.run_if(in_state(GameState::Menu)), .run_if(in_state(GameState::Menu)),
) )
.add_systems(OnEnter(GameState::Menu), activate::<MenuRoot, MenuCamera>) .add_systems(OnEnter(GameState::Menu), activate::<Menu>)
.add_systems(OnExit(GameState::Menu), deactivate::<MenuRoot, MenuCamera>); .add_systems(OnExit(GameState::Menu), deactivate::<Menu>);
} }
} }
#[derive(Debug, Component)] #[derive(Debug, Component)]
struct MenuCamera; struct Menu;
#[derive(Debug, Component)]
struct MenuRoot;
#[derive(Debug, Component)] #[derive(Debug, Component)]
struct Quit; struct Quit;
fn init_menu_ui(mut commands: Commands) { fn init_menu_ui(mut commands: Commands) {
commands.spawn(( commands.spawn((
MenuCamera, Menu,
Camera2dBundle { Camera2dBundle {
camera: Camera { camera: Camera {
is_active: false, is_active: false,
@ -50,7 +47,7 @@ fn init_menu_ui(mut commands: Commands) {
commands commands
.spawn(( .spawn((
MenuRoot, Menu,
NodeBundle { NodeBundle {
style: Style { style: Style {
width: Val::Percent(100.0), width: Val::Percent(100.0),

Loading…
Cancel
Save