11use std:: collections:: HashSet ;
22
3- use bevy:: { input :: common_conditions :: input_just_pressed , prelude:: * } ;
3+ use bevy:: prelude:: * ;
44
5- use crate :: gameplay:: { FactorySystems , world:: tilemap:: coord:: Coord } ;
5+ use crate :: {
6+ gameplay:: { FactorySystems , world:: tilemap:: coord:: Coord } ,
7+ input:: input_map:: { Action , InputActions , action_just_pressed} ,
8+ } ;
69
7- pub const DEMOLISH_BUTTON : KeyCode = KeyCode :: KeyF ;
8- pub const DEMOLISH_CANCEL_BUTTON : KeyCode = KeyCode :: Escape ;
910pub const DEMOLISH_DURATION_SECS : f32 = 1.0 ;
1011
1112pub ( super ) fn plugin ( app : & mut App ) {
@@ -14,20 +15,21 @@ pub(super) fn plugin(app: &mut App) {
1415
1516 app. add_message :: < Demolished > ( ) ;
1617
18+ app. add_observer ( add_to_selection) ;
19+ app. add_observer ( remove_from_selection) ;
20+
1721 app. add_systems (
18- FixedUpdate ,
22+ Update ,
1923 (
20- add_to_selection. run_if ( on_message :: < Pointer < Over > > ) ,
21- remove_from_selection. run_if ( on_message :: < Pointer < Out > > ) ,
22- clear_selection. run_if ( input_just_pressed ( DEMOLISH_CANCEL_BUTTON ) ) ,
24+ clear_selection. run_if ( action_just_pressed ( Action :: Dismiss ) ) ,
25+ tick_demolish_timer,
2326 )
24- . in_set ( FactorySystems :: Demolish ) ,
27+ . chain ( ) ,
2528 ) ;
2629
2730 app. add_systems (
2831 FixedUpdate ,
2932 (
30- tick_demolish_timer,
3133 highlight_demolition,
3234 demolish_selection. run_if ( demolish_timer_finished) ,
3335 )
@@ -63,48 +65,40 @@ impl Default for DemolishTimer {
6365
6466fn tick_demolish_timer (
6567 mut timer : ResMut < DemolishTimer > ,
66- keys : Res < ButtonInput < KeyCode > > ,
6768 time : Res < Time > ,
69+ input_actions : Res < InputActions > ,
6870) {
69- if keys. just_released ( DEMOLISH_BUTTON ) {
70- timer. reset ( ) ;
71- }
72-
73- if keys. pressed ( DEMOLISH_BUTTON ) {
71+ if input_actions. pressed . contains ( & Action :: Demolish ) {
7472 timer. tick ( time. delta ( ) ) ;
73+ } else {
74+ timer. reset ( ) ;
7575 }
7676}
7777
7878fn demolish_timer_finished ( timer : Res < DemolishTimer > ) -> bool {
79- timer. just_finished ( )
79+ timer. is_finished ( )
8080}
8181
8282fn add_to_selection (
83- mut pointer_overs : MessageReader < Pointer < Over > > ,
84- mut selection : ResMut < DemolishSelection > ,
83+ pointer_over : On < Pointer < Over > > ,
8584 demolishables : Query < Entity , With < Demolishable > > ,
85+ mut selection : ResMut < DemolishSelection > ,
8686) {
87- for pointer_over in pointer_overs. read ( ) {
88- if !demolishables. contains ( pointer_over. entity ) {
89- continue ;
90- }
91-
87+ if demolishables. contains ( pointer_over. entity ) {
9288 selection. insert ( pointer_over. entity ) ;
9389 }
9490}
9591
9692fn remove_from_selection (
97- mut pointer_outs : MessageReader < Pointer < Out > > ,
93+ pointer_out : On < Pointer < Out > > ,
94+ input_actions : Res < InputActions > ,
9895 mut selection : ResMut < DemolishSelection > ,
99- keys : Res < ButtonInput < KeyCode > > ,
10096) {
101- for pointer_out in pointer_outs. read ( ) {
102- if keys. pressed ( KeyCode :: ShiftLeft ) {
103- continue ;
104- }
105-
106- selection. remove ( & pointer_out. entity ) ;
97+ if input_actions. pressed . contains ( & Action :: MultiSelect ) {
98+ return ;
10799 }
100+
101+ selection. remove ( & pointer_out. entity ) ;
108102}
109103
110104fn clear_selection ( mut selection : ResMut < DemolishSelection > ) {
0 commit comments