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
36use 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+
958pub struct StatsPlugin ;
1059
1160impl 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;
3989struct SolutionText ;
4090
4191#[ derive( Component ) ]
42- struct OverallSuccessRatio ;
92+ struct OverallSuccessRatioText ;
4393
4494fn 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\n chosing 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
138183fn 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 ,
0 commit comments