@@ -46,6 +46,7 @@ struct Player {
46
46
entity : Option < Entity > ,
47
47
i : usize ,
48
48
j : usize ,
49
+ move_cooldown : Timer ,
49
50
}
50
51
51
52
#[ derive( Default ) ]
@@ -97,6 +98,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut game: ResMu
97
98
game. score = 0 ;
98
99
game. player . i = BOARD_SIZE_I / 2 ;
99
100
game. player . j = BOARD_SIZE_J / 2 ;
101
+ game. player . move_cooldown = Timer :: from_seconds ( 0.3 , false ) ;
100
102
101
103
commands. spawn_bundle ( PointLightBundle {
102
104
transform : Transform :: from_xyz ( 4.0 , 10.0 , 4.0 ) ,
@@ -189,49 +191,54 @@ fn move_player(
189
191
keyboard_input : Res < Input < KeyCode > > ,
190
192
mut game : ResMut < Game > ,
191
193
mut transforms : Query < & mut Transform > ,
194
+ time : Res < Time > ,
192
195
) {
193
- let mut moved = false ;
194
- let mut rotation = 0.0 ;
195
- if keyboard_input. just_pressed ( KeyCode :: Up ) {
196
- if game. player . i < BOARD_SIZE_I - 1 {
197
- game. player . i += 1 ;
196
+ if game. player . move_cooldown . tick ( time. delta ( ) ) . finished ( ) {
197
+ let mut moved = false ;
198
+ let mut rotation = 0.0 ;
199
+
200
+ if keyboard_input. pressed ( KeyCode :: Up ) {
201
+ if game. player . i < BOARD_SIZE_I - 1 {
202
+ game. player . i += 1 ;
203
+ }
204
+ rotation = -std:: f32:: consts:: FRAC_PI_2 ;
205
+ moved = true ;
198
206
}
199
- rotation = -std :: f32 :: consts :: FRAC_PI_2 ;
200
- moved = true ;
201
- }
202
- if keyboard_input . just_pressed ( KeyCode :: Down ) {
203
- if game . player . i > 0 {
204
- game . player . i -= 1 ;
207
+ if keyboard_input . pressed ( KeyCode :: Down ) {
208
+ if game . player . i > 0 {
209
+ game . player . i -= 1 ;
210
+ }
211
+ rotation = std :: f32 :: consts :: FRAC_PI_2 ;
212
+ moved = true ;
205
213
}
206
- rotation = std :: f32 :: consts :: FRAC_PI_2 ;
207
- moved = true ;
208
- }
209
- if keyboard_input . just_pressed ( KeyCode :: Right ) {
210
- if game . player . j < BOARD_SIZE_J - 1 {
211
- game . player . j += 1 ;
214
+ if keyboard_input . pressed ( KeyCode :: Right ) {
215
+ if game . player . j < BOARD_SIZE_J - 1 {
216
+ game . player . j += 1 ;
217
+ }
218
+ rotation = std :: f32 :: consts :: PI ;
219
+ moved = true ;
212
220
}
213
- rotation = std :: f32 :: consts :: PI ;
214
- moved = true ;
215
- }
216
- if keyboard_input . just_pressed ( KeyCode :: Left ) {
217
- if game . player . j > 0 {
218
- game . player . j -= 1 ;
221
+ if keyboard_input . pressed ( KeyCode :: Left ) {
222
+ if game . player . j > 0 {
223
+ game . player . j -= 1 ;
224
+ }
225
+ rotation = 0.0 ;
226
+ moved = true ;
219
227
}
220
- rotation = 0.0 ;
221
- moved = true ;
222
- }
223
228
224
- // move on the board
225
- if moved {
226
- * transforms. get_mut ( game. player . entity . unwrap ( ) ) . unwrap ( ) = Transform {
227
- translation : Vec3 :: new (
228
- game. player . i as f32 ,
229
- game. board [ game. player . j ] [ game. player . i ] . height ,
230
- game. player . j as f32 ,
231
- ) ,
232
- rotation : Quat :: from_rotation_y ( rotation) ,
233
- ..Default :: default ( )
234
- } ;
229
+ // move on the board
230
+ if moved {
231
+ game. player . move_cooldown . reset ( ) ;
232
+ * transforms. get_mut ( game. player . entity . unwrap ( ) ) . unwrap ( ) = Transform {
233
+ translation : Vec3 :: new (
234
+ game. player . i as f32 ,
235
+ game. board [ game. player . j ] [ game. player . i ] . height ,
236
+ game. player . j as f32 ,
237
+ ) ,
238
+ rotation : Quat :: from_rotation_y ( rotation) ,
239
+ ..Default :: default ( )
240
+ } ;
241
+ }
235
242
}
236
243
237
244
// eat the cake!
0 commit comments