-
-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add error handling when sending score #53
Draft
Vrixyz
wants to merge
2
commits into
vleue:main
Choose a base branch
from
Vrixyz:error_handling_send_score
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ fn main() { | |
option_env!("JORNET_LEADERBOARD_ID").unwrap_or("a920de64-3bdb-4f8e-87a8-e7bf20f00f81"), | ||
option_env!("JORNET_LEADERBOARD_KEY").unwrap_or("a797039b-a91d-43e6-8e1c-94f9ca0aa1d6"), | ||
)) | ||
.add_plugin(debug::DebugPlugin) | ||
.add_startup_system(setup) | ||
.add_state(GameState::Menu) | ||
.add_plugin(menu::MenuPlugin) | ||
|
@@ -29,6 +30,105 @@ fn main() { | |
.run(); | ||
} | ||
|
||
mod debug { | ||
use crate::{BACKGROUND, TEXT}; | ||
use bevy::prelude::*; | ||
use bevy_jornet::ErrorEvent; | ||
|
||
pub struct DebugPlugin; | ||
|
||
impl Plugin for DebugPlugin { | ||
fn build(&self, app: &mut bevy::prelude::App) { | ||
app.add_startup_system(setup) | ||
.add_system_to_stage(CoreStage::PostUpdate, react_to_errors) | ||
.add_system(despawn_after); | ||
} | ||
} | ||
|
||
#[derive(Component)] | ||
struct DebugContainer; | ||
|
||
/// Despawns recursively host entity when `Time.seconds_since_startup()` exceeds than value | ||
#[derive(Component)] | ||
struct DespawnAfter(f64); | ||
|
||
fn setup(mut commands: Commands) { | ||
commands | ||
.spawn_bundle(NodeBundle { | ||
style: Style { | ||
align_self: AlignSelf::FlexEnd, | ||
flex_direction: FlexDirection::ColumnReverse, | ||
position_type: PositionType::Absolute, | ||
position: UiRect { | ||
top: Val::Px(5.0), | ||
right: Val::Px(15.0), | ||
..default() | ||
}, | ||
max_size: Size { | ||
width: Val::Px(400.), | ||
height: Val::Undefined, | ||
}, | ||
align_items: AlignItems::FlexEnd, | ||
..default() | ||
}, | ||
color: Color::hex(BACKGROUND).unwrap().into(), | ||
..default() | ||
}) | ||
.insert(DebugContainer); | ||
} | ||
|
||
fn react_to_errors( | ||
mut commands: Commands, | ||
asset_server: Res<AssetServer>, | ||
time: Res<Time>, | ||
mut error_event: EventReader<bevy_jornet::ErrorEvent>, | ||
query_error_container: Query<Entity, With<DebugContainer>>, | ||
) { | ||
for error in error_event.iter() { | ||
for e in query_error_container.iter() { | ||
commands.entity(e).with_children(|parent| { | ||
parent | ||
.spawn_bundle( | ||
TextBundle::from_section( | ||
match error { | ||
bevy_jornet::ErrorEvent::SendScoreFailed(error) => { | ||
format!("{error}") | ||
} | ||
}, | ||
TextStyle { | ||
font: asset_server.load("FiraSans-Bold.ttf"), | ||
font_size: 50.0, | ||
color: Color::rgb(0.8, 0.2, 0.7), | ||
}, | ||
) | ||
.with_text_alignment(TextAlignment::CENTER) | ||
.with_style(Style { | ||
align_self: AlignSelf::FlexEnd, | ||
margin: UiRect::all(Val::Auto), | ||
justify_content: JustifyContent::Center, | ||
flex_direction: FlexDirection::ColumnReverse, | ||
..default() | ||
}), | ||
) | ||
.insert(DespawnAfter(time.seconds_since_startup() + 3f64)); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
fn despawn_after( | ||
time: Res<Time>, | ||
mut commands: Commands, | ||
q_to_unspawn: Query<(Entity, &DespawnAfter)>, | ||
) { | ||
for (e, despawn) in q_to_unspawn.iter() { | ||
if despawn.0 < time.seconds_since_startup() { | ||
commands.entity(e).despawn_recursive(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[derive(Clone, PartialEq, Eq, Debug, Hash)] | ||
enum GameState { | ||
Game, | ||
|
@@ -53,6 +153,9 @@ mod menu { | |
use crate::{GameState, BACKGROUND, BUTTON, TEXT}; | ||
pub struct MenuPlugin; | ||
|
||
#[derive(Component)] | ||
struct MenuUI; | ||
|
||
impl Plugin for MenuPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_system_set(SystemSet::on_enter(GameState::Menu).with_system(display_menu)) | ||
|
@@ -89,6 +192,7 @@ mod menu { | |
color: Color::hex(BACKGROUND).unwrap().into(), | ||
..default() | ||
}) | ||
.insert(MenuUI) | ||
.with_children(|parent| { | ||
parent.spawn_bundle(TextBundle::from_section( | ||
"Whac-A-Square", | ||
|
@@ -205,6 +309,7 @@ mod menu { | |
..default() | ||
}), | ||
) | ||
.insert(MenuUI) | ||
.insert(PlayerName); | ||
|
||
leaderboard.refresh_leaderboard(); | ||
|
@@ -252,7 +357,10 @@ mod menu { | |
} | ||
} | ||
|
||
fn despawn_menu(mut commands: Commands, root_ui: Query<Entity, (With<Node>, Without<Parent>)>) { | ||
fn despawn_menu( | ||
mut commands: Commands, | ||
root_ui: Query<Entity, (With<Node>, With<MenuUI>, Without<Parent>)>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added more control on what exactly is despawned to be able to keep the debug UI for errors display |
||
) { | ||
for entity in &root_ui { | ||
commands.entity(entity).despawn_recursive(); | ||
} | ||
|
@@ -317,6 +425,11 @@ mod game { | |
} | ||
} | ||
|
||
#[derive(Component)] | ||
struct ScoreTextMarker; | ||
#[derive(Component)] | ||
struct TimerMarker; | ||
|
||
fn setup_game(mut commands: Commands, asset_server: Res<AssetServer>) { | ||
commands.insert_resource(WinitSettings { | ||
focused_mode: UpdateMode::Reactive { | ||
|
@@ -329,41 +442,45 @@ mod game { | |
time_to_click: Timer::from_seconds(10.0, false), | ||
since_start: Stopwatch::new(), | ||
}); | ||
commands.spawn_bundle( | ||
TextBundle::from_section( | ||
"0", | ||
TextStyle { | ||
font: asset_server.load("FiraSans-Bold.ttf"), | ||
font_size: 50.0, | ||
color: Color::hex(TEXT).unwrap(), | ||
}, | ||
) | ||
.with_style(Style { | ||
align_self: AlignSelf::FlexEnd, | ||
position_type: PositionType::Absolute, | ||
position: UiRect { | ||
top: Val::Px(10.0), | ||
left: Val::Px(15.0), | ||
commands | ||
.spawn_bundle( | ||
TextBundle::from_section( | ||
"0", | ||
TextStyle { | ||
font: asset_server.load("FiraSans-Bold.ttf"), | ||
font_size: 50.0, | ||
color: Color::hex(TEXT).unwrap(), | ||
}, | ||
) | ||
.with_style(Style { | ||
align_self: AlignSelf::FlexEnd, | ||
position_type: PositionType::Absolute, | ||
position: UiRect { | ||
top: Val::Px(10.0), | ||
left: Val::Px(15.0), | ||
..default() | ||
}, | ||
..default() | ||
}, | ||
..default() | ||
}), | ||
); | ||
commands.spawn_bundle(NodeBundle { | ||
style: Style { | ||
align_self: AlignSelf::FlexEnd, | ||
position_type: PositionType::Absolute, | ||
position: UiRect { | ||
top: Val::Px(0.0), | ||
left: Val::Px(15.0), | ||
}), | ||
) | ||
.insert(ScoreTextMarker); | ||
commands | ||
.spawn_bundle(NodeBundle { | ||
style: Style { | ||
align_self: AlignSelf::FlexEnd, | ||
position_type: PositionType::Absolute, | ||
position: UiRect { | ||
top: Val::Px(0.0), | ||
left: Val::Px(15.0), | ||
..default() | ||
}, | ||
size: Size::new(Val::Px(200.0), Val::Px(8.0)), | ||
..default() | ||
}, | ||
size: Size::new(Val::Px(200.0), Val::Px(8.0)), | ||
color: Color::hex(SQUARE).unwrap().into(), | ||
..default() | ||
}, | ||
color: Color::hex(SQUARE).unwrap().into(), | ||
..default() | ||
}); | ||
}) | ||
.insert(TimerMarker); | ||
} | ||
|
||
#[derive(Component)] | ||
|
@@ -440,8 +557,8 @@ mod game { | |
|
||
fn game_state( | ||
mut status: ResMut<GameStatus>, | ||
mut score_text: Query<&mut Text>, | ||
mut timer: Query<&mut Style, Without<Text>>, | ||
mut score_text: Query<&mut Text, With<ScoreTextMarker>>, | ||
mut timer: Query<&mut Style, (Without<Text>, With<TimerMarker>)>, | ||
time: Res<Time>, | ||
mut state: ResMut<State<GameState>>, | ||
) { | ||
|
@@ -457,7 +574,7 @@ mod game { | |
status: Res<GameStatus>, | ||
leaderboard: Res<Leaderboard>, | ||
mut commands: Commands, | ||
game_ui: Query<Entity, With<Node>>, | ||
game_ui: Query<Entity, (With<Node>, Or<(With<ScoreTextMarker>, With<TimerMarker>)>)>, | ||
squares: Query<Entity, With<Sprite>>, | ||
) { | ||
for entity in &game_ui { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be after
GameState
andsetup()