@@ -23,6 +23,7 @@ struct SkiFree {
2323 status : GameStatus ,
2424 gate_combo : u16 ,
2525 trick_combo : u16 ,
26+ input : InputRepeat ,
2627}
2728
2829impl SkiFree {
@@ -38,6 +39,7 @@ impl SkiFree {
3839 status : GameStatus :: Ready ,
3940 gate_combo : 0 ,
4041 trick_combo : 0 ,
42+ input : InputRepeat :: default ( ) ,
4143 }
4244 }
4345
@@ -48,6 +50,7 @@ impl SkiFree {
4850 self . status = GameStatus :: Ready ;
4951 self . gate_combo = 0 ;
5052 self . trick_combo = 0 ;
53+ self . input = InputRepeat :: default ( ) ;
5154 }
5255
5356 fn start ( & mut self ) {
@@ -71,18 +74,18 @@ impl SkiFree {
7174 self . start ( ) ;
7275 }
7376
74- if is_key_pressed ( KeyCode :: Left ) {
77+ if self . input . consume ( KeyCode :: Left ) {
7578 self . start ( ) ;
7679 self . player . left ( ) ;
7780 }
78- if is_key_pressed ( KeyCode :: Right ) {
81+ if self . input . consume ( KeyCode :: Right ) {
7982 self . start ( ) ;
8083 self . player . right ( ) ;
8184 }
82- if is_key_pressed ( KeyCode :: Up ) {
85+ if is_key_down ( KeyCode :: Up ) {
8386 self . player . brake ( ) ;
8487 }
85- if is_key_pressed ( KeyCode :: Down ) {
88+ if is_key_down ( KeyCode :: Down ) {
8689 self . start ( ) ;
8790 self . player . down ( ) ;
8891 }
@@ -239,6 +242,46 @@ enum GameStatus {
239242 Eaten ,
240243}
241244
245+ #[ derive( Default ) ]
246+ struct InputRepeat {
247+ left : KeyRepeat ,
248+ right : KeyRepeat ,
249+ }
250+
251+ impl InputRepeat {
252+ fn consume ( & mut self , key : KeyCode ) -> bool {
253+ match key {
254+ KeyCode :: Left => self . left . consume ( key) ,
255+ KeyCode :: Right => self . right . consume ( key) ,
256+ _ => false ,
257+ }
258+ }
259+ }
260+
261+ #[ derive( Default ) ]
262+ struct KeyRepeat {
263+ held_frames : u8 ,
264+ }
265+
266+ impl KeyRepeat {
267+ const INITIAL_DELAY_FRAMES : u8 = 8 ;
268+ const REPEAT_FRAMES : u8 = 5 ;
269+
270+ fn consume ( & mut self , key : KeyCode ) -> bool {
271+ if !is_key_down ( key) {
272+ self . held_frames = 0 ;
273+ return false ;
274+ }
275+
276+ let should_fire = self . held_frames == 0
277+ || ( self . held_frames >= Self :: INITIAL_DELAY_FRAMES
278+ && ( self . held_frames - Self :: INITIAL_DELAY_FRAMES )
279+ . is_multiple_of ( Self :: REPEAT_FRAMES ) ) ;
280+ self . held_frames = self . held_frames . saturating_add ( 1 ) ;
281+ should_fire
282+ }
283+ }
284+
242285struct OverlayLine {
243286 text : & ' static str ,
244287 y_offset : f32 ,
0 commit comments