1
1
use std:: { io:: Read , sync:: Mutex } ;
2
2
3
- use ggez:: {
4
- event:: KeyCode ,
5
- graphics:: { self , Color , DrawMode , DrawParam , Scale , Text } ,
6
- nalgebra:: Point2 ,
7
- Context , GameResult ,
8
- } ;
3
+ use ggez:: { Context , GameResult , event:: KeyCode , graphics:: { self , Color , DrawMode , DrawParam , Scale , Text } , nalgebra:: Point2 , timer} ;
9
4
use ggez_goodies:: { camera:: Camera , nalgebra_glm:: Vec2 } ;
10
5
use graphics:: { Font , Image , Mesh , TextFragment } ;
11
6
use rand:: Rng ;
@@ -29,6 +24,7 @@ pub struct Game {
29
24
30
25
camera : Camera ,
31
26
elapsed_shake : Option < ( f32 , Vec2 ) > ,
27
+ tics : Option < i32 >
32
28
}
33
29
34
30
impl Game {
@@ -124,7 +120,8 @@ impl Game {
124
120
camera,
125
121
126
122
consolas : graphics:: Font :: new ( ctx, "/fonts/Consolas.ttf" ) . unwrap ( ) ,
127
- elapsed_shake : None
123
+ elapsed_shake : None ,
124
+ tics : None ,
128
125
} )
129
126
}
130
127
@@ -193,7 +190,21 @@ impl Game {
193
190
graphics:: present ( ctx)
194
191
}
195
192
196
- pub fn update ( & mut self , _ctx : & mut Context ) -> GameResult < Option < crate :: Screen > > {
193
+ pub fn update ( & mut self , ctx : & mut Context ) -> GameResult < Option < crate :: Screen > > {
194
+ if let Some ( t) = self . tics {
195
+ if timer:: ticks ( ctx) % t as usize == 0 {
196
+ return self . inner_update ( ctx) ;
197
+ }
198
+ }
199
+
200
+ else {
201
+ return self . inner_update ( ctx) ;
202
+ }
203
+
204
+ Ok ( None )
205
+ }
206
+
207
+ pub fn inner_update ( & mut self , _ctx : & mut Context ) -> GameResult < Option < crate :: Screen > > {
197
208
let ferris_pos_x = self . player . pos_x ;
198
209
let mut ferris_is_falling_down: bool = true ;
199
210
@@ -283,17 +294,30 @@ impl Game {
283
294
if let Some ( fish) = self . player . shoot ( ) {
284
295
self . player_bullets . push ( fish) ;
285
296
}
286
- }
297
+ } ,
298
+ KeyCode :: F3 => {
299
+ self . tics = Some ( 6 ) ;
300
+ } ,
287
301
_ => ( ) ,
288
302
}
289
303
290
304
None
291
305
}
292
306
307
+ pub fn key_up_event ( & mut self , keycode : KeyCode ) {
308
+ match keycode {
309
+ KeyCode :: F3 => {
310
+ self . tics = None ;
311
+ } ,
312
+
313
+ _ => ( )
314
+ }
315
+ }
316
+
293
317
pub fn camera_shakeke ( & mut self ) {
294
318
let mut rng = rand:: thread_rng ( ) ;
295
319
296
- let magnitude = 2 .;
320
+ let magnitude = 3 .;
297
321
let elapsed = self . elapsed_shake . unwrap ( ) ;
298
322
299
323
let x = rng. gen_range ( -1.0 , 1.0 ) * magnitude;
0 commit comments