Skip to content

Commit de400c0

Browse files
Add Some(slow_motion)
1 parent 7371b43 commit de400c0

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

src/game.rs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
use std::{io::Read, sync::Mutex};
22

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};
94
use ggez_goodies::{camera::Camera, nalgebra_glm::Vec2};
105
use graphics::{Font, Image, Mesh, TextFragment};
116
use rand::Rng;
@@ -29,6 +24,7 @@ pub struct Game {
2924

3025
camera: Camera,
3126
elapsed_shake: Option<(f32, Vec2)>,
27+
tics: Option<i32>
3228
}
3329

3430
impl Game {
@@ -124,7 +120,8 @@ impl Game {
124120
camera,
125121

126122
consolas: graphics::Font::new(ctx, "/fonts/Consolas.ttf").unwrap(),
127-
elapsed_shake: None
123+
elapsed_shake: None,
124+
tics: None,
128125
})
129126
}
130127

@@ -193,7 +190,21 @@ impl Game {
193190
graphics::present(ctx)
194191
}
195192

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>> {
197208
let ferris_pos_x = self.player.pos_x;
198209
let mut ferris_is_falling_down: bool = true;
199210

@@ -283,17 +294,30 @@ impl Game {
283294
if let Some(fish) = self.player.shoot() {
284295
self.player_bullets.push(fish);
285296
}
286-
}
297+
},
298+
KeyCode::F3 => {
299+
self.tics = Some(6);
300+
},
287301
_ => (),
288302
}
289303

290304
None
291305
}
292306

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+
293317
pub fn camera_shakeke(&mut self) {
294318
let mut rng = rand::thread_rng();
295319

296-
let magnitude = 2.;
320+
let magnitude = 3.;
297321
let elapsed = self.elapsed_shake.unwrap();
298322

299323
let x = rng.gen_range(-1.0, 1.0) * magnitude;

src/main.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ impl EventHandler for MyGame {
9898
}
9999
}
100100

101+
fn key_up_event(&mut self, _ctx: &mut Context, keycode: KeyCode, _keymods: KeyMods) {
102+
match self.screen {
103+
Screen::Play => self.game_screen.lock().unwrap().key_up_event(keycode),
104+
_ => ()
105+
}
106+
}
107+
101108
fn key_down_event(
102109
&mut self,
103110
_ctx: &mut Context,

0 commit comments

Comments
 (0)