Skip to content

Commit 613bb72

Browse files
committed
add clock pickup
1 parent c033e70 commit 613bb72

File tree

4 files changed

+155
-68
lines changed

4 files changed

+155
-68
lines changed

src/game/movement.rs

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct MovementController(pub Vec2);
2626
fn movement(
2727
input: Res<ButtonInput<KeyCode>>,
2828
mut controller_query: Query<(&mut ClockController, &mut Transform), Without<Clock>>,
29-
mut clocks: Query<(&mut Transform, &mut Clock), With<Interactable>>,
29+
mut clocks: Query<(Entity, &mut Transform, &mut Clock), With<Interactable>>,
3030
positions: Res<Positions>,
3131
) {
3232
let mut intent = Vec2::ZERO;
@@ -41,16 +41,12 @@ fn movement(
4141

4242
let (mut controller, mut transform) = controller_query.get_single_mut().unwrap();
4343
controller.direction = intent;
44-
45-
if controller.direction == Vec2::ZERO
46-
|| controller.index == 0 && controller.direction.x < 0.0
47-
|| controller.index == 6 && controller.direction.x > 0.0
48-
{
49-
return;
44+
if controller.index == 0 && controller.direction.x < 0.0 {
45+
controller.index = 0;
46+
} else {
47+
controller.index = (controller.index as i32 + controller.direction.x as i32) as usize;
48+
controller.index = controller.index.min(6);
5049
}
51-
52-
let prev_x = transform.translation.x;
53-
controller.index = (controller.index as i32 + controller.direction.x as i32) as usize;
5450
let position = match controller.index {
5551
0 => positions.clock_spawn,
5652
1 => positions.clock_1,
@@ -62,23 +58,49 @@ fn movement(
6258
_ => panic!("Invalid index"),
6359
};
6460

65-
transform.translation.x = position.x;
66-
67-
let current_clock = clocks.iter_mut().find(|(t, _)| t.translation.x == prev_x);
68-
if current_clock.is_some() {
69-
let mut clock = current_clock.unwrap();
70-
if !clock.1.is_main {
71-
clock.0.translation.y = -220.0;
61+
// pick up clock
62+
if input.just_pressed(KeyCode::Space) {
63+
if controller.held_clock.is_some() {
64+
let clock_count = clocks
65+
.iter_mut()
66+
.filter(|(_, t, _)| t.translation.x == position.x)
67+
.count();
68+
if clock_count == 1 {
69+
let clock = clocks
70+
.iter_mut()
71+
.find(|(e, _, _)| *e == controller.held_clock.unwrap());
72+
if clock.is_some() {
73+
let mut clock = clock.unwrap();
74+
clock.1.translation.y = position.y;
75+
controller.held_clock = None;
76+
}
77+
}
78+
} else {
79+
let target_clock = clocks
80+
.iter_mut()
81+
.find(|(_, t, _)| t.translation.x == position.x);
82+
if target_clock.is_some() {
83+
let mut clock = target_clock.unwrap();
84+
clock.1.translation.y = position.y + 30.0;
85+
controller.held_clock = Some(clock.0);
86+
}
7287
}
7388
}
7489

75-
let target_clock = clocks
76-
.iter_mut()
77-
.find(|(t, _)| t.translation.x == position.x);
78-
if target_clock.is_some() {
79-
let mut clock = target_clock.unwrap();
80-
if !clock.1.is_main {
81-
clock.0.translation.y = -190.0;
90+
// move and move held clock
91+
if controller.direction != Vec2::ZERO {
92+
transform.translation.x = position.x;
93+
let held_clock: Option<(Entity, Mut<Transform>, Mut<Clock>)> =
94+
clocks.iter_mut().find(|(e, _, _)| {
95+
if controller.held_clock.is_some() {
96+
return controller.held_clock.unwrap() == *e;
97+
}
98+
false
99+
});
100+
101+
if held_clock.is_some() {
102+
let mut clock = held_clock.unwrap();
103+
clock.1.translation = transform.translation;
82104
}
83105
}
84106
}

src/game/spawn/clock.rs

Lines changed: 102 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -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)]
6494
pub 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)]
86117
pub 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+
88130
fn 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

225258
fn 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(
343407
fn 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()

src/game/spawn/level.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Spawn the main level by triggering other observers.
22
3-
use bevy::{asset::transformer, prelude::*};
3+
use bevy::prelude::*;
44

55
use crate::{
66
game::assets::{HandleMap, ImageKey},
@@ -69,10 +69,6 @@ fn spawn_level(_trigger: Trigger<SpawnLevel>, mut commands: Commands) {
6969
commands.trigger(SpawnTable);
7070
commands.trigger(SpawnMainClock);
7171
commands.trigger(SpawnClock);
72-
commands.trigger(SpawnClock);
73-
commands.trigger(SpawnClock);
74-
commands.trigger(SpawnClock);
75-
commands.trigger(SpawnClock);
7672
commands.trigger(SpawnOil);
7773
commands.trigger(SpawnScore);
7874
commands.trigger(SpawnClockTable);

src/game/spawn/player.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,12 @@ fn spawn_player(
125125
ClockController {
126126
index: 1,
127127
oil_level: 100.0,
128-
..default()
128+
direction: Vec2::new(0.0, 0.0),
129+
held_clock: None,
130+
setting: false,
131+
winding: false,
132+
time_setting: 0.0,
133+
time_winding: 0.0,
129134
},
130135
StateScoped(Screen::Playing),
131136
))

0 commit comments

Comments
 (0)