@@ -31,14 +31,44 @@ pub(super) fn plugin(app: &mut App) {
3131 . run_if ( in_state ( Screen :: Playing ) ) ,
3232 ) ;
3333 app. insert_resource ( Positions {
34- clock_spawn : Vec2 :: new ( -550.0 , -180 .0) ,
35- clock_1 : Vec2 :: new ( -330.0 , -190 .0) ,
34+ clock_spawn : Vec2 :: new ( -550.0 , -185 .0) ,
35+ clock_1 : Vec2 :: new ( -330.0 , -220 .0) ,
3636 clock_2 : Vec2 :: new ( -180.0 , -220.0 ) ,
3737 clock_3 : Vec2 :: new ( -30.0 , -220.0 ) ,
3838 clock_4 : Vec2 :: new ( 120.0 , -220.0 ) ,
3939 clock_5 : Vec2 :: new ( 270.0 , -220.0 ) ,
4040 oil_can : Vec2 :: new ( 550.0 , -200.0 ) ,
4141 } ) ;
42+
43+ app. insert_resource ( Clocks {
44+ clocks : vec ! [
45+ ClockData {
46+ time_left: 0.0 ,
47+ hour_start_rotation: 0.0 ,
48+ minute_start_rotation: 1.0 ,
49+ } ,
50+ ClockData {
51+ time_left: 0.0 ,
52+ hour_start_rotation: 2.0 ,
53+ minute_start_rotation: 3.0 ,
54+ } ,
55+ ClockData {
56+ time_left: 0.0 ,
57+ hour_start_rotation: 4.0 ,
58+ minute_start_rotation: 5.0 ,
59+ } ,
60+ ClockData {
61+ time_left: 0.0 ,
62+ hour_start_rotation: 6.0 ,
63+ minute_start_rotation: 7.0 ,
64+ } ,
65+ ClockData {
66+ time_left: 0.0 ,
67+ hour_start_rotation: 8.0 ,
68+ minute_start_rotation: 9.0 ,
69+ } ,
70+ ] ,
71+ } ) ;
4272}
4373
4474#[ derive( Event , Debug ) ]
@@ -59,9 +89,10 @@ enum ClockHandType {
5989 Minute ,
6090}
6191
62- #[ derive( Component , Reflect , Default ) ]
92+ #[ derive( Component , Reflect ) ]
6393#[ reflect( Component ) ]
6494pub struct ClockController {
95+ pub held_clock : Option < Entity > ,
6596 pub index : usize ,
6697 pub setting : bool ,
6798 pub time_setting : f32 ,
@@ -85,13 +116,24 @@ pub struct Positions {
85116#[ derive( Component ) ]
86117pub struct Interactable ;
87118
119+ #[ derive( Resource ) ]
120+ pub struct Clocks {
121+ clocks : Vec < ClockData > ,
122+ }
123+
124+ pub struct ClockData {
125+ pub time_left : f32 ,
126+ pub hour_start_rotation : f32 ,
127+ pub minute_start_rotation : f32 ,
128+ }
129+
88130fn record_clock_controller (
89131 time : Res < Time > ,
90132 input : Res < ButtonInput < KeyCode > > ,
91133 mut controller_query : Query < & mut ClockController > ,
92134) {
93135 for mut controller in & mut controller_query {
94- if input. pressed ( KeyCode :: Space ) {
136+ if input. pressed ( KeyCode :: KeyS ) {
95137 if controller. setting {
96138 controller. time_setting += time. delta_seconds ( ) ;
97139 } else {
@@ -121,7 +163,7 @@ fn apply_clock_control(
121163 time : Res < Time > ,
122164 positions : Res < Positions > ,
123165 mut control_query : Query < & mut ClockController , Without < Interactable > > ,
124- mut clocks : Query < ( & mut Clock , & Transform , & Children ) , With < Interactable > > ,
166+ mut clocks : Query < ( Entity , & mut Clock , & Transform , & Children ) , With < Interactable > > ,
125167 mut q_child : Query <
126168 ( & mut Transform , & ClockHandType ) ,
127169 ( Without < Interactable > , Without < ClockController > ) ,
@@ -132,7 +174,7 @@ fn apply_clock_control(
132174 return ;
133175 }
134176 let mut controller = result. unwrap ( ) ;
135- if ! controller. winding && !controller . setting {
177+ if controller. held_clock . is_none ( ) {
136178 return ;
137179 }
138180
@@ -146,29 +188,20 @@ fn apply_clock_control(
146188 controller. time_setting = controller. time_setting . min ( 3.0 ) ;
147189 }
148190
149- let position = match controller. index {
150- 0 => positions. clock_spawn . x ,
151- 1 => positions. clock_1 . x ,
152- 2 => positions. clock_2 . x ,
153- 3 => positions. clock_3 . x ,
154- 4 => positions. clock_4 . x ,
155- 5 => positions. clock_5 . x ,
156- 6 => positions. oil_can . x ,
157- _ => panic ! ( "Invalid index" ) ,
158- } ;
159-
160- let children = clocks. iter_mut ( ) . find ( |t| t. 1 . translation . x == position) ;
191+ let children = clocks
192+ . iter_mut ( )
193+ . find ( |t| controller. held_clock . is_some ( ) && t. 0 == controller. held_clock . unwrap ( ) ) ;
161194 if children. is_none ( ) {
162195 return ;
163196 }
164197 let mut children = children. unwrap ( ) ;
165198
166199 if controller. winding {
167- children. 0 . time_left += time. delta_seconds ( ) * 6.0 ;
200+ children. 1 . time_left += time. delta_seconds ( ) * 6.0 ;
168201 }
169202
170203 if controller. setting {
171- for & child in children. 2 . iter ( ) {
204+ for & child in children. 3 . iter ( ) {
172205 let child_result = q_child. get_mut ( child) ;
173206
174207 if let Ok ( ( mut transform, hand_type) ) = child_result {
@@ -223,6 +256,7 @@ fn tick_clocks(
223256}
224257
225258fn score_clocks (
259+ mut commands : Commands ,
226260 time : Res < Time > ,
227261 mut score : Query < ( & mut Score , & mut Text ) > ,
228262 clocks : Query < ( & Clock , & Children ) > ,
@@ -243,12 +277,42 @@ fn score_clocks(
243277 let hour_diff = main_rotations. hour . angle_between ( clock_rotation. hour ) ;
244278 let minute_diff = main_rotations. minute . angle_between ( clock_rotation. minute ) ;
245279
280+ println ! ( "Score! {} {}" , hour_diff, minute_diff) ;
246281 if hour_diff < 0.1 && minute_diff < 0.1 {
247282 score. 0 += 1.0 * time. delta_seconds ( ) ;
248- println ! ( "Score! {} {}" , hour_diff, minute_diff) ;
249283 }
250284 }
251285
286+ let clock_count = clocks. iter ( ) . count ( ) - 1 ;
287+
288+ match clock_count {
289+ 1 => {
290+ if score. 0 > 25.0 {
291+ commands. trigger ( SpawnClock ) ;
292+ }
293+ }
294+ 2 => {
295+ if score. 0 > 100.0 {
296+ commands. trigger ( SpawnClock ) ;
297+ }
298+ }
299+ 3 => {
300+ if score. 0 > 250.0 {
301+ commands. trigger ( SpawnClock ) ;
302+ }
303+ }
304+ 4 => {
305+ if score. 0 > 500.0 {
306+ commands. trigger ( SpawnClock ) ;
307+ }
308+ }
309+ 5 => {
310+ if score. 0 > 1000.0 {
311+ commands. trigger ( SpawnClock ) ;
312+ }
313+ }
314+ _ => { }
315+ }
252316 text. sections [ 0 ] . value = format ! ( "{:.0}" , score. 0 ) ;
253317}
254318
@@ -343,20 +407,26 @@ fn spawn_main_clock(
343407fn spawn_interact_clock (
344408 _trigger : Trigger < SpawnClock > ,
345409 positions : Res < Positions > ,
410+ clock_data : Res < Clocks > ,
346411 mut commands : Commands ,
347412 image_handles : Res < HandleMap < ImageKey > > ,
348- clocks : Query < ( & Clock , & Transform ) > ,
413+ clocks : Query < ( & Clock , & Transform ) , With < Interactable > > ,
349414) {
350415 let clock_count = clocks. iter ( ) . count ( ) ;
351- let translation = match clock_count {
352- 1 => positions. clock_1 ,
353- 2 => positions. clock_2 ,
354- 3 => positions. clock_3 ,
355- 4 => positions. clock_4 ,
356- 5 => positions. clock_5 ,
357- _ => positions. clock_spawn ,
416+ let translation = positions. clock_spawn ;
417+ let clock_data = & clock_data. clocks [ clock_count + 1 ] ;
418+
419+ let mut hour_transform = Transform {
420+ translation : Vec3 :: new ( 0.0 , 0.0 , 30.0 ) ,
421+ ..default ( )
358422 } ;
423+ // hour_transform.rotate_z(clock_data.hour_start_rotation);
359424
425+ let mut minute_transform = Transform {
426+ translation : Vec3 :: new ( 0.0 , 0.0 , 40.0 ) ,
427+ ..default ( )
428+ } ;
429+ // minute_transform.rotate_z(clock_data.minute_start_rotation);
360430 commands
361431 . spawn ( (
362432 Name :: new ( "Clock" ) ,
@@ -376,18 +446,15 @@ fn spawn_interact_clock(
376446 StateScoped ( Screen :: Playing ) ,
377447 Clock {
378448 is_main : false ,
379- time_left : 0.0 ,
449+ time_left : clock_data . time_left ,
380450 } ,
381451 Interactable ,
382452 ) )
383453 . with_children ( |parent| {
384454 parent. spawn ( (
385455 SpriteBundle {
386456 texture : image_handles[ & ImageKey :: ClockHour ] . clone_weak ( ) ,
387- transform : Transform {
388- translation : Vec3 :: new ( 0.0 , 0.0 , 30.0 ) ,
389- ..Default :: default ( )
390- } ,
457+ transform : hour_transform,
391458 sprite : Sprite {
392459 custom_size : Some ( Vec2 :: new ( 90.0 , 90.0 ) ) ,
393460 ..default ( )
@@ -400,10 +467,7 @@ fn spawn_interact_clock(
400467 parent. spawn ( (
401468 SpriteBundle {
402469 texture : image_handles[ & ImageKey :: ClockMinute ] . clone_weak ( ) ,
403- transform : Transform {
404- translation : Vec3 :: new ( 0.0 , 0.0 , 40.0 ) ,
405- ..Default :: default ( )
406- } ,
470+ transform : minute_transform,
407471 sprite : Sprite {
408472 custom_size : Some ( Vec2 :: new ( 90.0 , 90.0 ) ) ,
409473 ..default ( )
0 commit comments