diff src/th06/enemy.rs @ 688:1adecaddf442

ecl_vm: implement SetBulletInterval and SetBulletIntervalEx.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 17 Aug 2019 13:50:54 +0200
parents ac092b70c39a
children 482d81f50a11
line wrap: on
line diff
--- a/src/th06/enemy.rs
+++ b/src/th06/enemy.rs
@@ -206,8 +206,8 @@ pub struct Enemy {
     pub(crate) low_life_trigger: Option<u32>,
     pub(crate) timeout: Option<u32>,
     pub(crate) remaining_lives: u32,
-    pub(crate) bullet_launch_interval: u32,
-    pub(crate) bullet_launch_timer: u32,
+    bullet_launch_interval: u32,
+    bullet_launch_timer: u32,
     pub(crate) death_anim: i32,
     pub(crate) direction: Direction,
     pub(crate) update_mode: u32,
@@ -349,6 +349,14 @@ impl Enemy {
         }
     }
 
+    /// Sets the bullet launch interval.
+    pub(crate) fn set_bullet_launch_interval(&mut self, rand_start: u32, interval: i32) {
+        let coeff_interval = interval / 5;
+        let difficulty_modifier = coeff_interval + (-coeff_interval * 2) * self.get_difficulty() / 32;
+        self.bullet_launch_interval = (interval + difficulty_modifier) as u32;
+        self.bullet_launch_timer = rand_start % self.bullet_launch_interval;
+    }
+
     /// Run all interpolators and such, and update internal variables once per
     /// frame.
     pub fn update(&mut self) {
@@ -402,6 +410,15 @@ impl Enemy {
 
         self.pos = Position { x, y };
 
+        if self.bullet_launch_interval != 0 {
+            if self.bullet_launch_timer == 0 {
+                self.bullet_attributes.fire();
+                self.bullet_launch_timer = self.bullet_launch_interval;
+            }
+            self.bullet_launch_timer += 1;
+            self.bullet_launch_timer %= self.bullet_launch_interval;
+        }
+
         self.frame += 1;
     }