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)),
),
)
.add_systems(
OnEnter(GameState::Credits),
activate::<Credits, CreditsCamera>,
)
.add_systems(
OnExit(GameState::Credits),
deactivate::<Credits, CreditsCamera>,
);
.add_systems(OnEnter(GameState::Credits), activate::<Credits>)
.add_systems(OnExit(GameState::Credits), deactivate::<Credits>);
}
}
@ -77,12 +71,9 @@ fn parse_credits(bytes: &[u8]) -> Result<CreditsText, Utf8Error> {
#[derive(Debug, Component)]
struct Credits;
#[derive(Debug, Component)]
struct CreditsCamera;
fn init_credits_ui(mut commands: Commands, server: Res<AssetServer>) {
commands.spawn((
CreditsCamera,
Credits,
Camera2dBundle {
camera: Camera {
is_active: false,

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

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

@ -13,15 +13,13 @@ impl Plugin for GamePlugin {
(
update_board::<display2d::Piece2d>.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(
PostUpdate,
(
debug_hovering
.run_if(resource_exists::<debug::DebugEnabled>())
.run_if(resource_changed::<ActiveTile>()),
debug_hovering.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()
.for_each(|(mut side, idx)| match Board::side(idx) {
Ok(s) => {
debug!("Set side event {:?} {:?} -> {:?}", idx, side, s);
if *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 display3d;
mod game;
mod loading;
mod menu;
mod prelude;
@ -45,12 +46,13 @@ fn main() {
..default()
}),
audio::AudioPlugin,
credits::CreditsPlugin,
debug::DebugPlugin,
display2d::Display2dPlugin,
display3d::Display3dPlugin,
game::GamePlugin,
loading::LoadingPlugin,
menu::MenuPlugin,
credits::CreditsPlugin,
))
.run();
}
@ -126,9 +128,9 @@ fn toggle_display_mode(
})
}
fn activate<E: Component, C: Component>(
mut cameras: Query<&mut Camera, With<C>>,
mut entities: Query<&mut Visibility, With<E>>,
fn activate<Marker: Component>(
mut cameras: Query<&mut Camera, With<Marker>>,
mut entities: Query<&mut Visibility, With<Marker>>,
) {
cameras.iter_mut().for_each(|mut camera| {
camera.is_active = true;
@ -138,9 +140,9 @@ fn activate<E: Component, C: Component>(
});
}
fn deactivate<E: Component, C: Component>(
mut cameras: Query<&mut Camera, With<C>>,
mut entities: Query<&mut Visibility, With<E>>,
fn deactivate<Marker: Component>(
mut cameras: Query<&mut Camera, With<Marker>>,
mut entities: Query<&mut Visibility, With<Marker>>,
) {
cameras.iter_mut().for_each(|mut camera| {
camera.is_active = false;
@ -149,3 +151,11 @@ fn deactivate<E: Component, C: Component>(
*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)),
)
.add_systems(OnEnter(GameState::Menu), activate::<MenuRoot, MenuCamera>)
.add_systems(OnExit(GameState::Menu), deactivate::<MenuRoot, MenuCamera>);
.add_systems(OnEnter(GameState::Menu), activate::<Menu>)
.add_systems(OnExit(GameState::Menu), deactivate::<Menu>);
}
}
#[derive(Debug, Component)]
struct MenuCamera;
#[derive(Debug, Component)]
struct MenuRoot;
struct Menu;
#[derive(Debug, Component)]
struct Quit;
fn init_menu_ui(mut commands: Commands) {
commands.spawn((
MenuCamera,
Menu,
Camera2dBundle {
camera: Camera {
is_active: false,
@ -50,7 +47,7 @@ fn init_menu_ui(mut commands: Commands) {
commands
.spawn((
MenuRoot,
Menu,
NodeBundle {
style: Style {
width: Val::Percent(100.0),

Loading…
Cancel
Save