Skip to content

Commit 4b1dbc5

Browse files
Lerp the shoot value
1 parent 165dd44 commit 4b1dbc5

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

src/components/player.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub struct Player {
1818
pub pos_x: f32,
1919
pub pos_y: f32,
2020

21-
pub ammo: i32,
21+
pub ammo: f32,
2222
pub health: i32,
2323

2424
gravity: f32,
@@ -32,7 +32,7 @@ impl Player {
3232
pub fn new(pos_x: f32) -> Self {
3333
Self {
3434
pos_x,
35-
ammo: 10,
35+
ammo: 10.,
3636
pos_y: 0.,
3737
gravity: 0.1,
3838
velocity: 0.,
@@ -122,9 +122,7 @@ impl Player {
122122
pub fn shoot(&mut self) -> Option<Turbofish> {
123123
const HEIGHT2: f32 = HEIGHT / 2.;
124124

125-
if self.ammo != 0 {
126-
self.ammo -= 1;
127-
125+
if self.ammo as i32 != 0 {
128126
return Some(Turbofish::new(
129127
self.pos_x + 220.,
130128
(-HEIGHT2 + 106.) + self.pos_y,

src/game.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{io::Read, sync::Mutex};
1+
use std::{collections::HashMap, io::Read, sync::Mutex};
22

33
use ggez::{
44
audio::{SoundSource, Source},
@@ -60,9 +60,11 @@ pub struct Game {
6060
consolas: Font,
6161

6262
camera: Camera,
63+
6364
elapsed_shake: Option<(f32, Vec2, f32)>,
6465
tics: Option<i32>,
6566
particles: Vec<(ParticleSystem, f32, f32, i32)>,
67+
ui_lerp: HashMap<String, f32>,
6668

6769
dim_shader: ShaderGeneric<GlBackendSpec, Dim>,
6870
dim_constant: Dim,
@@ -150,6 +152,10 @@ impl Game {
150152
}
151153

152154
let mut player = player.expect("No player found!");
155+
let mut ui_lerp = HashMap::new();
156+
157+
ui_lerp.insert(String::from("ammo"), player.ammo as f32);
158+
ui_lerp.insert(String::from("health"), player.health as f32);
153159

154160
player.pos_y += 40.;
155161
player.going_boom = true;
@@ -209,9 +215,11 @@ impl Game {
209215
camera,
210216

211217
consolas: graphics::Font::new(ctx, "/fonts/Consolas.ttf").unwrap(),
218+
212219
elapsed_shake: None,
213220
tics: None,
214221
particles: vec![],
222+
ui_lerp,
215223

216224
dim_shader,
217225
dim_constant,
@@ -551,6 +559,20 @@ impl Game {
551559
}
552560
}
553561

562+
for v in &mut self.ui_lerp {
563+
match v.0.as_str() {
564+
"ammo" => {
565+
self.player.ammo = lerp(self.player.ammo, *v.1, 0.3);
566+
},
567+
568+
"health" => {
569+
// TODO: Health lerping
570+
}
571+
572+
_ => panic!()
573+
}
574+
}
575+
554576
Ok(None)
555577
}
556578

@@ -570,11 +592,16 @@ impl Game {
570592
self.player.go_boom();
571593
}
572594
KeyCode::S => {
595+
let ui_lerp = self.ui_lerp.clone();
596+
573597
if let Some(fish) = self.player.shoot() {
574598
self.audio_resources[0]
575599
.play()
576600
.expect("Cannot play Some(turbofish_shoot).mp3");
577601

602+
let cur_ammo = ui_lerp.get("ammo").unwrap();
603+
self.ui_lerp.insert(String::from("ammo"), *cur_ammo - 1.);
604+
578605
self.player_bullets.push(fish);
579606
}
580607
}

src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
pub fn lerp(from: f32, to: f32, dt: f32) -> f32 {
2-
return from + dt * (to - from);
2+
from + dt * (to - from)
33
}
44

55
pub fn remap(n: f32, start1: f32, stop1: f32, start2: f32, stop2: f32) -> f32 {

0 commit comments

Comments
 (0)