Skip to content

Commit d8417dd

Browse files
committed
fix show / hide stats
1 parent 829c5cd commit d8417dd

5 files changed

Lines changed: 210 additions & 44 deletions

File tree

solitaire-game/src/hints.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ pub struct HintsPlugin;
88

99
impl Plugin for HintsPlugin {
1010
fn build(&self, app: &mut App) {
11-
app.insert_resource(ShowHints);
1211
app.add_plugins(Shape2dPlugin::default());
1312
app.add_observer(update_hints);
1413
app.add_systems(

solitaire-game/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use bevy::{camera::ScalingMode, prelude::*};
22
use bevy_vector_shapes::{prelude::ShapePainter, shapes::DiscPainter};
3-
use solitaire_solver::{Board, Solution};
3+
use solitaire_solver::Board;
44

55
use crate::{
66
animation::PegAnimation,

solitaire-game/src/stats.rs

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,60 @@
1-
use bevy::{prelude::*, sprite::Anchor, text::TextBounds, window::RequestRedraw};
1+
use bevy::{
2+
ecs::entity_disabling::Disabled, prelude::*, sprite::Anchor, text::TextBounds,
3+
window::RequestRedraw,
4+
};
25

36
use crate::{
4-
BoardPosition, CurrentBoard, SolutionEvent,
7+
BoardPosition, CurrentBoard,
58
solver::{FeasibleConstellations, RandomMoveChances},
69
total_progress::TotalProgress,
710
};
811

12+
#[derive(Default, Event)]
13+
pub struct ToggleStats;
14+
15+
#[derive(Resource)]
16+
struct ShowStats;
17+
18+
fn toggle_stats(
19+
_: On<ToggleStats>,
20+
mut commands: Commands,
21+
show_stats: Option<Res<ShowStats>>,
22+
stats: Query<
23+
Entity,
24+
(
25+
Or<(
26+
With<SolutionText>,
27+
With<OverallSuccessRatioText>,
28+
With<TotalProgressText>,
29+
With<NextMoveChanceText>,
30+
)>,
31+
Or<(With<Disabled>, Without<Disabled>)>,
32+
),
33+
>,
34+
) {
35+
if show_stats.is_none() {
36+
info!("Hiding Stats");
37+
commands.insert_resource(ShowStats);
38+
let mut i = 0;
39+
for e in &stats {
40+
info!("Hiding Stats ({i})");
41+
i += 1;
42+
let mut e = commands.entity(e);
43+
e.insert(Disabled);
44+
}
45+
} else {
46+
info!("Showing Stats");
47+
commands.remove_resource::<ShowStats>();
48+
let mut i = 0;
49+
for e in &stats {
50+
info!("Showing Stats ({i})");
51+
i += 1;
52+
let mut e = commands.entity(e);
53+
e.remove::<Disabled>();
54+
}
55+
}
56+
}
57+
958
pub struct StatsPlugin;
1059

1160
impl Plugin for StatsPlugin {
@@ -23,6 +72,7 @@ impl Plugin for StatsPlugin {
2372
app.add_observer(update_overall_success);
2473
app.add_observer(update_total_progress);
2574
app.add_observer(update_solution_count);
75+
app.add_observer(toggle_stats);
2676
}
2777
}
2878

@@ -39,7 +89,7 @@ struct TotalProgressText;
3989
struct SolutionText;
4090

4191
#[derive(Component)]
42-
struct OverallSuccessRatio;
92+
struct OverallSuccessRatioText;
4393

4494
fn update_stats(mut commands: Commands) {
4595
commands.trigger(UpdateStats);
@@ -70,25 +120,20 @@ fn add_text(mut commands: Commands, asset_server: Res<AssetServer>) {
70120
Vec3::from((BoardPosition { x: 4, y: 1 }.to_world_space(), 1.)) + Vec3::new(0.5, -0.5, 0.0);
71121
let title_pos_3 =
72122
Vec3::from((BoardPosition { x: 1, y: 1 }.to_world_space(), 1.)) + Vec3::new(0.5, -0.5, 0.0);
73-
let text_pos = title_pos - 1.0 * Vec3::Y;
74123
commands
75124
.spawn((
76125
Text2d::new("\u{1D4AB}(\u{1D437}) \u{2248} "),
77126
Transform::from_scale(Vec3::new(0.005, 0.005, 0.005)).with_translation(title_pos),
78127
medium_font.clone(),
79128
TextLayout::new_with_justify(Justify::Left),
80129
Anchor::TOP_LEFT,
81-
OverallSuccessRatio,
130+
OverallSuccessRatioText,
82131
))
83-
.with_child((TextSpan(" ... ?".into()), medium_font.clone()));
84-
commands.spawn((
85-
Text2d::new("“chance of winning by chosing moves at random”"),
86-
Transform::from_scale(Vec3::new(0.004, 0.004, 0.004)).with_translation(text_pos),
87-
small_font.clone(),
88-
TextLayout::new(Justify::Center, LineBreak::WordBoundary),
89-
TextBounds::from(Vec2::new(600.0, 300.0)),
90-
Anchor::TOP_LEFT,
91-
));
132+
.with_child((TextSpan(" ... ?".into()), medium_font.clone()))
133+
.with_child((
134+
TextSpan("\n“chance of winning by\nchosing moves at random”".into()),
135+
small_font.clone(),
136+
));
92137
commands
93138
.spawn((
94139
Text2d::new(""),
@@ -137,7 +182,7 @@ fn add_text(mut commands: Commands, asset_server: Res<AssetServer>) {
137182

138183
fn update_overall_success(
139184
_trigger: On<UpdateStats>,
140-
overall_success_text: Query<Entity, With<OverallSuccessRatio>>,
185+
overall_success_text: Query<Entity, With<OverallSuccessRatioText>>,
141186
board: Res<CurrentBoard>,
142187
p_success: Option<Res<RandomMoveChances>>,
143188
mut writer: TextUiWriter,

solitaire-game/src/total_progress.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ pub struct TotalProgressPlugin;
2222
pub struct TotalProgress {
2323
/// all states that have ever been seen
2424
pub explored_states: HashSet<Board>,
25+
/// explored states by number of pegs
26+
pub explored_states_by_pegs: [HashSet<Board>; Board::SLOTS - 1],
2527
/// all unique solutions that have been explored
2628
pub unique_solutions: HashSet<Solution>,
2729
/// number of times the boared has been solved
@@ -41,7 +43,9 @@ fn update_total_progress(
4143
mut total_progress: ResMut<TotalProgress>,
4244
board: Res<CurrentBoard>,
4345
) {
44-
total_progress.explored_states.insert(board.0);
46+
let board = board.0;
47+
total_progress.explored_states.insert(board);
48+
total_progress.explored_states_by_pegs[board.count_balls() as usize - 1].insert(board);
4549
}
4650

4751
fn update_solutions(solution: On<SolutionEvent>, mut total_progress: ResMut<TotalProgress>) {

0 commit comments

Comments
 (0)