changeset 662:107bb5ca5cc8

Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 11 Aug 2019 18:14:01 +0200
parents 598f3125cbac
children 994f41154be8
files examples/eclrenderer.rs src/th06/enemy.rs
diffstat 2 files changed, 35 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/examples/eclrenderer.rs
+++ b/examples/eclrenderer.rs
@@ -96,7 +96,7 @@ fn main() {
         return;
     }
     let ecl_filename = &args[1];
-    let sub = args[2].parse().expect("number");
+    let sub: u8 = args[2].parse().expect("number");
     let anm_filename = &args[3];
     let png_filename = &args[4];
 
@@ -115,7 +115,7 @@ fn main() {
     let anm0 = Anm0::from_slice(&buf).unwrap();
     let anm0 = Rc::new(RefCell::new(anm0));
 
-    if ecl.subs.len() < sub {
+    if ecl.subs.len() < sub as usize {
         eprintln!("This ecl doesn’t contain a sub named {}.", sub);
         return;
     }
@@ -129,7 +129,7 @@ fn main() {
 
     // And the enemy object.
     let enemy = Enemy::new(Position::new(0., 0.), 500, 0, 640, Rc::downgrade(&anm0), Rc::downgrade(&game));
-    let mut ecl_runner = EclRunner::new(&ecl, enemy.clone(), 0);
+    let mut ecl_runner = EclRunner::new(&ecl, enemy.clone(), sub);
 
     assert_eq!(std::mem::size_of::<Vertex>(), std::mem::size_of::<FakeVertex>());
     let vertices: [Vertex; 4] = unsafe { std::mem::uninitialized() };
@@ -151,7 +151,6 @@ fn main() {
 
     let mut back_buffer = Framebuffer::back_buffer(surface.size());
 
-    let mut frame = 0;
     'app: loop {
         for event in surface.poll_events() {
             match event {
@@ -168,7 +167,6 @@ fn main() {
         if ecl_runner.running == false {
             break;
         }
-        frame += 1;
 
         {
             let mut slice = tess
@@ -176,6 +174,10 @@ fn main() {
                 .unwrap();
 
             ecl_runner.run_frame();
+            {
+                let mut enemy = enemy.borrow_mut();
+                enemy.update();
+            }
             let mut game = game.borrow_mut();
             game.run_frame();
             let sprites = game.get_sprites();
--- a/src/th06/enemy.rs
+++ b/src/th06/enemy.rs
@@ -234,6 +234,34 @@ impl Enemy {
         self.hitbox_half_size = [width, height];
     }
 
+    /// Run all interpolators and such, and update internal variables once per
+    /// frame.
+    pub fn update(&mut self) {
+        let Position { mut x, mut y } = self.pos;
+
+        let speed = if self.update_mode == 1 {
+            0.
+        } else {
+            let speed = self.speed;
+            self.speed += self.acceleration;
+            self.angle += self.rotation_speed;
+            speed
+        };
+
+        let dx = self.angle.cos() * speed;
+        let dy = self.angle.sin() * speed;
+        if self.type_ & 2 != 0 {
+            x -= dx;
+        } else {
+            x += dx;
+        }
+        y += dy;
+
+        self.pos = Position { x, y };
+
+        self.frame += 1;
+    }
+
     pub(crate) fn get_rank(&self) -> i32 {
         let game = self.game.upgrade().unwrap();
         let game = game.borrow();