Skip to content

Commit 4e6829a

Browse files
committed
update light indicator
1 parent c87d019 commit 4e6829a

2 files changed

Lines changed: 108 additions & 21 deletions

File tree

src/game/lyra/beam.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ use crate::{
1818
LightBeamSource, LightColor,
1919
},
2020
lighting::LineLight2d,
21-
lyra::Lyra,
21+
lyra::{
22+
indicator::{LYRA_SHARD_HOLD_X, LYRA_SHARD_HOLD_Y},
23+
Lyra,
24+
},
2225
Layers, LevelSystems,
2326
},
2427
save::SaveParam,
@@ -284,20 +287,22 @@ pub fn handle_shoot_inputs(
284287
pub fn process_beam_actions(
285288
mut commands: Commands,
286289
mut beam_actions: MessageReader<BeamAction>,
287-
lyra: Single<(&Transform, &mut PlayerLightInventory), With<Lyra>>,
290+
lyra: Single<(&Transform, &mut PlayerLightInventory, &Sprite), With<Lyra>>,
288291
cursor: Single<&CursorWorldCoords>,
289292
// beam_assets: Res<BeamSourceAssets>,
290293
) {
291-
let (player_transform, player_inventory) = lyra.into_inner();
294+
let (player_transform, player_inventory, lyra_sprite) = lyra.into_inner();
292295
let player_inventory = player_inventory.into_inner();
293296
for action in beam_actions.read() {
294297
match action {
295298
BeamAction::Shoot => {
296299
if !player_inventory.can_shoot() {
297300
continue;
298301
}
302+
let mult = if lyra_sprite.flip_x { -1. } else { 1. };
303+
let desired_shard_pos = Vec3::new(mult * LYRA_SHARD_HOLD_X, LYRA_SHARD_HOLD_Y, 0.);
299304

300-
let ray_pos = player_transform.translation.truncate();
305+
let ray_pos = (player_transform.translation + desired_shard_pos).truncate();
301306
let mut ray_dir = (cursor.pos - ray_pos).normalize_or_zero();
302307
if player_inventory.snapping {
303308
ray_dir = snap_ray(ray_dir);
@@ -348,6 +353,11 @@ pub fn process_beam_actions(
348353
player_inventory.snapping = *val;
349354
}
350355
BeamAction::Preview => {
356+
if player_inventory.current_color == None
357+
|| !player_inventory.can_shoot_color(player_inventory.current_color.unwrap())
358+
{
359+
continue;
360+
}
351361
player_inventory.previewing = true;
352362
player_inventory.should_shoot = true;
353363
}
@@ -377,21 +387,25 @@ pub struct LightPreviewGizmos;
377387

378388
pub fn preview_light_path(
379389
spatial_query: SpatialQuery,
380-
lyra: Single<(&Transform, &PlayerLightInventory), With<Lyra>>,
390+
lyra: Single<(&Transform, &PlayerLightInventory, &Sprite), With<Lyra>>,
381391
cursor: Single<&CursorWorldCoords>,
382392
mut gizmos: Gizmos,
383393
q_mirror: Query<&Mirror>,
384394
// q_black_ray: Query<(Entity, &BlackRayComponent)>,
385395
) {
386-
let (transform, inventory) = lyra.into_inner();
396+
let (transform, inventory, lyra_sprite) = lyra.into_inner();
387397
if !inventory.can_shoot() || !inventory.previewing {
388398
return;
389399
}
390400

391401
let shoot_color = inventory.current_color.unwrap();
392402

393-
let ray_pos = transform.translation.truncate();
403+
let mult = if lyra_sprite.flip_x { -1. } else { 1. };
404+
let desired_shard_pos = Vec3::new(mult * LYRA_SHARD_HOLD_X, LYRA_SHARD_HOLD_Y, 0.);
405+
406+
let ray_pos = (transform.translation + desired_shard_pos).truncate();
394407
let mut ray_dir = (cursor.pos - ray_pos).normalize_or_zero();
408+
395409
if inventory.snapping {
396410
ray_dir = snap_ray(ray_dir);
397411
}

src/game/lyra/indicator.rs

Lines changed: 87 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use enum_map::EnumMap;
77

88
use crate::asset::LoadResource;
99
use crate::camera::TERRAIN_LAYER;
10+
use crate::game::light::LightBeamSource;
1011
use crate::game::lighting::LineLight2d;
1112
use crate::game::lyra::spawn_lyra;
1213
use crate::ldtk::LdtkParam;
@@ -25,6 +26,8 @@ pub const LYRA_SHARD_HEIGHT_DELTA: f32 = 4.;
2526
pub const LYRA_SHARD_Y_OFFSET: f32 = -4.;
2627
pub const LYRA_SHARD_PEAK_SHIFT_RATE: f32 = 1.2;
2728
pub const LYRA_SHARD_MOVING_DELAY_MILLIS: u64 = 250;
29+
pub const LYRA_SHARD_HOLD_X: f32 = 4.;
30+
pub const LYRA_SHARD_HOLD_Y: f32 = -2.;
2831

2932
pub struct LightIndicatorPlugin;
3033

@@ -190,14 +193,28 @@ impl Default for ShardMovingState {
190193
}
191194
}
192195

196+
#[derive(Component)]
197+
pub struct WasChilded;
198+
193199
pub fn update_light_indicator(
194-
lyra: Single<(&PlayerLightInventory, &Transform, &LinearVelocity), With<Lyra>>,
200+
lyra: Single<
201+
(
202+
Entity,
203+
&PlayerLightInventory,
204+
&Transform,
205+
&LinearVelocity,
206+
&Sprite,
207+
),
208+
With<Lyra>,
209+
>,
210+
q_transforms: Query<(&Transform, Has<WasChilded>), (Without<Lyra>, Without<LightBeamSource>)>,
211+
q_beam_sources: Query<(&Transform, &LightBeamSource)>,
195212
mut commands: Commands,
196213
mut light_indicators: ResMut<LightIndicators>,
197214
time: Res<Time>,
198215
mut shard_moving_state: Local<ShardMovingState>,
199216
) {
200-
let (inventory, base_transform, linear_velocity) = lyra.into_inner();
217+
let (lyra_entity, inventory, base_transform, linear_velocity, lyra_sprite) = lyra.into_inner();
201218
light_indicators.angle_offset += time.delta_secs();
202219

203220
for (color, entity) in light_indicators.indicators {
@@ -212,29 +229,85 @@ pub fn update_light_indicator(
212229
let indicators: Vec<_> = light_indicators
213230
.indicators
214231
.iter()
215-
.filter_map(|(c, entity)| {
216-
if !inventory.can_shoot_color(c) {
217-
return None;
218-
}
219-
entity.map(|e| (c, e))
220-
})
232+
.filter_map(|(c, e)| e.and_then(|e| Some((c, e))))
221233
.collect();
222234

223235
let handle_shooting = |commands: &mut Commands, c: LightColor, entity: Entity| -> bool {
224-
let is_shooting =
225-
inventory.current_color.is_some_and(|color| color == c) && inventory.can_shoot();
226-
236+
let is_selected = inventory.current_color.is_some_and(|color| color == c);
237+
let is_shooting = is_selected && inventory.can_shoot();
238+
let Ok((indicator_transform, was_childed)) = q_transforms.get(entity) else {
239+
return false;
240+
};
241+
// TODO: fixme use inverse transforms instead
227242
if is_shooting {
243+
if !was_childed {
244+
commands
245+
.entity(entity)
246+
.insert(Transform::from_translation(
247+
(indicator_transform.translation - base_transform.translation).with_z(1.),
248+
))
249+
.insert(WasChilded)
250+
.insert(ChildOf(lyra_entity));
251+
}
252+
let mult = if lyra_sprite.flip_x { -1. } else { 1. };
253+
commands.entity(entity).insert(LerpTranslation {
254+
to: LerpTranslationTarget::Position(Vec3::new(
255+
mult * LYRA_SHARD_HOLD_X,
256+
LYRA_SHARD_HOLD_Y,
257+
0.,
258+
)),
259+
lerp: 0.2,
260+
});
261+
return true;
262+
} else if !inventory.can_shoot_color(c) {
263+
if was_childed {
264+
commands
265+
.entity(entity)
266+
.remove::<ChildOf>()
267+
.remove::<WasChilded>()
268+
.insert(Transform::from_translation(
269+
base_transform.translation + indicator_transform.translation,
270+
));
271+
}
272+
let Some((source_transform, _)) =
273+
q_beam_sources.iter().find(|(_, source)| source.color == c)
274+
else {
275+
return true;
276+
};
228277
commands.entity(entity).insert(LerpTranslation {
229278
to: LerpTranslationTarget::Position(
230-
base_transform
279+
source_transform
231280
.translation
232-
.with_z(base_transform.translation.z - 0.1),
281+
.with_z(source_transform.translation.z + 1.),
233282
),
234-
lerp: 1.0,
283+
lerp: 0.2,
284+
});
285+
return true;
286+
} else if is_selected {
287+
if !was_childed {
288+
commands
289+
.entity(entity)
290+
.insert(Transform::from_translation(
291+
(indicator_transform.translation - base_transform.translation).with_z(1.),
292+
))
293+
.insert(WasChilded)
294+
.insert(ChildOf(lyra_entity));
295+
}
296+
commands.entity(entity).insert(LerpTranslation {
297+
to: LerpTranslationTarget::Position(Vec3::new(0., 13., 0.)),
298+
lerp: 0.2,
235299
});
236300
return true;
237301
}
302+
if was_childed {
303+
commands
304+
.entity(entity)
305+
.remove::<ChildOf>()
306+
.remove::<WasChilded>()
307+
.insert(Transform::from_translation(
308+
base_transform.translation + indicator_transform.translation,
309+
));
310+
}
238311
false
239312
};
240313

0 commit comments

Comments
 (0)