survivor

graphical action game for the linux terminal
git clone https://git.ce9e.org/survivor.git

commit
51d2a2a36707c386026f473ce5ae6b1c2b8d38c0
parent
75d2f3d58c7ca3abc0e0adf0ae3eca51e0750ec0
Author
Viktor Bahr <viktorbahr@posteo.net>
Date
2023-03-26 16:41
feat: spawn / despawn projectiles

Diffstat

M src/game.rs 34 +++++++++++++++++++++++++++++++++-

1 files changed, 33 insertions, 1 deletions


diff --git a/src/game.rs b/src/game.rs

@@ -15,7 +15,7 @@ const PERK_RECOVER: usize = 5;
   15    15 const PERK_ATTRACT: usize = 6;
   16    16 const PERK_XP: usize = 7;
   17    17 
   18    -1 #[derive(PartialEq)]
   -1    18 #[derive(PartialEq, Clone, Copy)]
   19    19 pub enum Dir {
   20    20     Up,
   21    21     Right,
@@ -23,6 +23,7 @@ pub enum Dir {
   23    23     Left,
   24    24 }
   25    25 
   -1    26 #[derive(Clone, Copy)]
   26    27 pub struct Pos {
   27    28     pub x: f32,
   28    29     pub y: f32,
@@ -98,6 +99,8 @@ pub struct Game {
   98    99     pub diamonds: Vec<Pos>,
   99   100     pub enemies: Vec<enemies::Enemy>,
  100   101     pub projectiles: Vec<weapons::Projectile>,
   -1   102     pub projectiles_cooldown: f32,
   -1   103     pub projectiles_last: f32,
  101   104     pub i_enemy: usize,
  102   105     rng: random::Rng,
  103   106 }
@@ -107,6 +110,8 @@ impl Game {
  107   110         return Self {
  108   111             enemies: vec![],
  109   112             projectiles: vec![],
   -1   113             projectiles_last: 0.0,
   -1   114             projectiles_cooldown: 4.0,
  110   115             diamonds: vec![],
  111   116             i_enemy: 0,
  112   117             player: Player::new(),
@@ -191,6 +196,31 @@ impl Game {
  191   196             .collect();
  192   197     }
  193   198 
   -1   199     fn spawn_projectiles(&mut self, dt: f32) {
   -1   200         self.projectiles_last += dt;
   -1   201         if self.projectiles_last > self.projectiles_cooldown {
   -1   202             self.projectiles_last -= self.projectiles_cooldown;
   -1   203             self.projectiles.push(weapons::Projectile {
   -1   204                 p: self.player.p,
   -1   205                 dir: match &self.player.dir {
   -1   206                     Some(dir) => dir.clone(),
   -1   207                     None => self.player.face,
   -1   208                 },
   -1   209                 t: &weapons::KNIFE,
   -1   210             });
   -1   211         }
   -1   212     }
   -1   213 
   -1   214     fn despawn_projectiles(&mut self, width: f32, height: f32) {
   -1   215         self.projectiles = std::mem::take(&mut self.projectiles)
   -1   216             .into_iter()
   -1   217             .filter(|proj| {
   -1   218                 (proj.p.y - self.player.p.y).abs() < height
   -1   219                     && (proj.p.x - self.player.p.x).abs() < width
   -1   220             })
   -1   221             .collect();
   -1   222     }
   -1   223 
  194   224     fn apply_damage(&mut self, dt: f32) {
  195   225         for enemy in self.enemies.iter_mut() {
  196   226             let dx = self.player.p.x - enemy.p.x;
@@ -257,6 +287,7 @@ impl Game {
  257   287         self.move_player(dt);
  258   288         self.move_enemies(dt);
  259   289         self.despawn_enemies(width, height);
   -1   290         self.despawn_projectiles(width, height);
  260   291 
  261   292         self.apply_damage(dt);
  262   293         self.pick_diamonds();
@@ -264,6 +295,7 @@ impl Game {
  264   295         self.player.recover(dt);
  265   296         self.player.levelup(&mut self.rng);
  266   297         self.spawn_enemies(dt, width, height);
   -1   298         self.spawn_projectiles(dt);
  267   299     }
  268   300 
  269   301     pub fn render(&mut self, win: &mut win::Window) {