@@ -2,7 +2,10 @@ use avian2d::{math::*, prelude::*};
22use bevy:: { ecs:: query:: Has , prelude:: * } ;
33
44use crate :: {
5- game:: { lyra:: Lyra , LevelSystems } ,
5+ game:: {
6+ lyra:: { Lyra , LyraWallCaster } ,
7+ LevelSystems ,
8+ } ,
69 shared:: PlayState ,
710} ;
811
@@ -107,10 +110,10 @@ pub fn keyboard_input(
107110 if keyboard_input. just_released ( KeyCode :: Space ) {
108111 movement_writer. write ( MovementAction :: JumpCut ) ;
109112 }
110- if keyboard_input. just_pressed ( KeyCode :: ShiftLeft ) {
113+ if keyboard_input. just_pressed ( KeyCode :: ControlLeft ) {
111114 movement_writer. write ( MovementAction :: Crouch ) ;
112115 }
113- if keyboard_input. just_released ( KeyCode :: ShiftLeft ) {
116+ if keyboard_input. just_released ( KeyCode :: ControlLeft ) {
114117 movement_writer. write ( MovementAction :: Stand ) ;
115118 }
116119}
@@ -130,78 +133,99 @@ pub fn update_grounded(
130133}
131134
132135pub fn movement (
133- time : Res < Time > ,
134136 mut movement_reader : MessageReader < MovementAction > ,
135- mut controllers : Query < (
136- & mut MovementInfo ,
137- & mut LinearVelocity ,
138- & ShapeHits ,
139- Has < Grounded > ,
140- ) > ,
137+ lyra : Single <
138+ (
139+ & mut MovementInfo ,
140+ & mut LinearVelocity ,
141+ & ShapeHits ,
142+ Has < Grounded > ,
143+ ) ,
144+ With < Lyra > ,
145+ > ,
146+ wall_casters : Query < ( & ShapeHits , & LyraWallCaster ) , Without < Lyra > > ,
141147) {
142- let delta = time. delta_secs ( ) * 64. ;
143- for ( mut movement_info, mut linear_velocity, shape_hits, is_grounded) in & mut controllers {
144- if is_grounded {
145- movement_info. coyote_time_ticks = COYOTE_TIME_TICKS ;
146- }
148+ let ( mut movement_info, mut linear_velocity, shape_hits, is_grounded) = lyra. into_inner ( ) ;
149+ if is_grounded {
150+ movement_info. coyote_time_ticks = COYOTE_TIME_TICKS ;
151+ }
147152
148- let mut moved = false ;
149- let mut crouch = false ;
150- for event in movement_reader. read ( ) {
151- match event {
152- MovementAction :: Move ( direction) => {
153- linear_velocity. x += * direction * PLAYER_MOVE_VEL * 64. * delta;
154- moved = true ;
155- }
156- MovementAction :: Jump => {
157- movement_info. should_jump_ticks = SHOULD_JUMP_TICKS ;
158- }
159- MovementAction :: JumpCut => {
160- if linear_velocity. y > 0. {
161- linear_velocity. y /= 3. ;
162- movement_info. jump_boost_ticks = 0 ;
163- movement_info. should_jump_ticks = 0 ;
164- }
153+ let mut moved = false ;
154+ for event in movement_reader. read ( ) {
155+ match event {
156+ MovementAction :: Move ( direction) => {
157+ linear_velocity. x += * direction * PLAYER_MOVE_VEL * 64. ;
158+ moved = true ;
159+ }
160+ MovementAction :: Jump => {
161+ movement_info. should_jump_ticks = SHOULD_JUMP_TICKS ;
162+ }
163+ MovementAction :: JumpCut => {
164+ if linear_velocity. y > 0. {
165+ linear_velocity. y /= 3. ;
166+ movement_info. jump_boost_ticks = 0 ;
167+ movement_info. should_jump_ticks = 0 ;
165168 }
166- MovementAction :: Crouch => crouch = true ,
167- MovementAction :: Stand => crouch = false ,
168169 }
170+ MovementAction :: Crouch => movement_info. crouched = true ,
171+ MovementAction :: Stand => movement_info. crouched = false ,
169172 }
173+ }
170174
171- if movement_info. should_jump_ticks > 0 && movement_info. coyote_time_ticks > 0 {
172- movement_info. jump_boost_ticks = JUMP_BOOST_TICKS ;
173- }
175+ if movement_info. should_jump_ticks > 0 && movement_info. coyote_time_ticks > 0 {
176+ movement_info. jump_boost_ticks = JUMP_BOOST_TICKS ;
177+ }
174178
175- let too_close = shape_hits. iter ( ) . any ( |hit| hit. distance < 0.25 ) ;
176- if movement_info. jump_boost_ticks > 0 {
177- linear_velocity. y = PLAYER_JUMP_VEL * 64. ;
178- } else if too_close && linear_velocity. y < 0.5 {
179- linear_velocity. y = 0.45 ;
180- } else if is_grounded && linear_velocity. y < 0.5 {
181- linear_velocity. y = 0. ;
182- } else {
183- linear_velocity. y -= PLAYER_GRAVITY * 64. * delta ;
184- }
179+ let too_close = shape_hits. iter ( ) . any ( |hit| hit. distance < 0.25 ) ;
180+ if movement_info. jump_boost_ticks > 0 {
181+ linear_velocity. y = PLAYER_JUMP_VEL * 64. ;
182+ } else if too_close && linear_velocity. y < 0.5 {
183+ linear_velocity. y = 0.45 ;
184+ } else if is_grounded && linear_velocity. y < 0.5 {
185+ linear_velocity. y = 0. ;
186+ } else {
187+ linear_velocity. y -= PLAYER_GRAVITY * 64. ;
188+ }
185189
186- linear_velocity. y = linear_velocity
187- . y
188- . clamp ( -PLAYER_MAX_Y_VEL * 64. , PLAYER_MAX_Y_VEL * 64. ) ;
190+ linear_velocity. y = linear_velocity
191+ . y
192+ . clamp ( -PLAYER_MAX_Y_VEL * 64. , PLAYER_MAX_Y_VEL * 64. ) ;
189193
190- let crouch_modif = if crouch { 0.5 } else { 1.0 } ;
191- linear_velocity. x = linear_velocity. x . clamp (
192- -PLAYER_MAX_H_VEL * 64. * crouch_modif,
193- PLAYER_MAX_H_VEL * 64. * crouch_modif,
194- ) ;
194+ if !moved {
195+ linear_velocity. x *= 0.6 ;
196+ if linear_velocity. x . abs ( ) < 0.1 {
197+ linear_velocity. x = 0. ;
198+ }
199+ }
195200
196- if !moved {
197- linear_velocity. x *= 0.6 ;
198- if linear_velocity. x . abs ( ) < 0.1 {
199- linear_velocity. x = 0. ;
201+ for ( wall_hits, side) in wall_casters. iter ( ) {
202+ let too_close = wall_hits. iter ( ) . any ( |hit| hit. distance < 0.25 ) ;
203+ let any_hit = wall_hits. iter ( ) . next ( ) . is_some ( ) ;
204+ match side {
205+ LyraWallCaster :: Left => {
206+ if too_close && linear_velocity. x < 0.5 {
207+ linear_velocity. x = 0.45 ;
208+ } else if any_hit && linear_velocity. x < 0.5 {
209+ linear_velocity. x = 0. ;
210+ }
211+ }
212+ LyraWallCaster :: Right => {
213+ if too_close && linear_velocity. x > -0.5 {
214+ linear_velocity. x = -0.45 ;
215+ } else if any_hit && linear_velocity. x > -0.5 {
216+ linear_velocity. x = 0. ;
217+ }
200218 }
201219 }
202-
203- movement_info. should_jump_ticks -= 1 ;
204- movement_info. jump_boost_ticks -= 1 ;
205- movement_info. coyote_time_ticks -= 1 ;
206220 }
221+
222+ let crouch_modif = if movement_info. crouched { 0.5 } else { 1.0 } ;
223+ linear_velocity. x = linear_velocity. x . clamp (
224+ -PLAYER_MAX_H_VEL * 64. * crouch_modif,
225+ PLAYER_MAX_H_VEL * 64. * crouch_modif,
226+ ) ;
227+
228+ movement_info. should_jump_ticks -= 1 ;
229+ movement_info. jump_boost_ticks -= 1 ;
230+ movement_info. coyote_time_ticks -= 1 ;
207231}
0 commit comments