changeset 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
files src/th06/ecl_vm.rs src/th06/enemy.rs
diffstat 2 files changed, 31 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/th06/ecl_vm.rs	Sat Aug 17 13:49:31 2019 +0200
+++ b/src/th06/ecl_vm.rs	Sat Aug 17 13:50:54 2019 +0200
@@ -607,7 +607,19 @@
                                          flags);
             }
 
+            // 76
+            SubInstruction::SetBulletInterval(interval) => {
+                let mut enemy = self.enemy.borrow_mut();
+                enemy.set_bullet_launch_interval(0, interval);
+            }
 
+            // 77
+            SubInstruction::SetBulletIntervalEx(interval) => {
+                let rand_start = self.get_prng().borrow_mut().get_u32();
+
+                let mut enemy = self.enemy.borrow_mut();
+                enemy.set_bullet_launch_interval(rand_start, interval);
+            }
 
             // 78-79 are more interpolation flags 
             // 78
--- a/src/th06/enemy.rs	Sat Aug 17 13:49:31 2019 +0200
+++ b/src/th06/enemy.rs	Sat Aug 17 13:50:54 2019 +0200
@@ -206,8 +206,8 @@
     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 @@
         }
     }
 
+    /// 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 @@
 
         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;
     }