@@ -7,6 +7,7 @@ use enum_map::EnumMap;
77
88use crate :: asset:: LoadResource ;
99use crate :: camera:: TERRAIN_LAYER ;
10+ use crate :: game:: light:: LightBeamSource ;
1011use crate :: game:: lighting:: LineLight2d ;
1112use crate :: game:: lyra:: spawn_lyra;
1213use crate :: ldtk:: LdtkParam ;
@@ -25,6 +26,8 @@ pub const LYRA_SHARD_HEIGHT_DELTA: f32 = 4.;
2526pub const LYRA_SHARD_Y_OFFSET : f32 = -4. ;
2627pub const LYRA_SHARD_PEAK_SHIFT_RATE : f32 = 1.2 ;
2728pub 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
2932pub struct LightIndicatorPlugin ;
3033
@@ -190,14 +193,28 @@ impl Default for ShardMovingState {
190193 }
191194}
192195
196+ #[ derive( Component ) ]
197+ pub struct WasChilded ;
198+
193199pub 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