changeset 693:14fddc27e6f5

ecl_vm: implement TargetPlayer, and add a dummy Player to Game.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 17 Aug 2019 15:33:45 +0200
parents a35df65e0d57
children 3ff1af76e413
files src/th06/ecl_vm.rs src/th06/enemy.rs
diffstat 2 files changed, 34 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/th06/ecl_vm.rs	Sat Aug 17 14:09:39 2019 +0200
+++ b/src/th06/ecl_vm.rs	Sat Aug 17 15:33:45 2019 +0200
@@ -532,16 +532,16 @@
                 enemy.angle = angle;
             }
             // 51
-             /*
-            SubInstruction::TargetPlayer(angle, speed) => {
+            SubInstruction::TargetPlayer(delta_angle, speed) => {
+                let speed = self.get_f32(speed);
                 let mut enemy = self.enemy.borrow_mut();
-                let player = enemy.select_player();
+                let game = enemy.game.upgrade().unwrap();
+                let player = game.borrow().get_player();
                 enemy.update_mode = 0;
                 enemy.speed = speed;
-                enemy.angle = enemy.get_angle(player) + angle;
+                enemy.angle = enemy.get_angle_to(player) + delta_angle;
+            }
 
-            }
-            */
             // 52 to 64 are different interlacing fields
 
             // 65
--- a/src/th06/enemy.rs	Sat Aug 17 14:09:39 2019 +0200
+++ b/src/th06/enemy.rs	Sat Aug 17 15:33:45 2019 +0200
@@ -47,6 +47,16 @@
     }
 }
 
+impl std::ops::Sub<Position> for Position {
+    type Output = Offset;
+    fn sub(self, other: Position) -> Offset {
+        Offset {
+            dx: other.x - self.x,
+            dy: other.y - self.y,
+        }
+    }
+}
+
 #[derive(Debug, Clone)]
 struct Callback;
 
@@ -60,10 +70,16 @@
 #[derive(Debug, Clone, Default)]
 struct Process;
 
+/// Struct representing the player.
+pub struct Player {
+    pos: Position,
+}
+
 /// God struct of our game.
 pub struct Game {
     enemies: Vec<Rc<RefCell<Enemy>>>,
     anmrunners: Vec<Rc<RefCell<AnmRunner>>>,
+    player: Rc<RefCell<Player>>,
     prng: Rc<RefCell<Prng>>,
     rank: Rank,
     difficulty: i32,
@@ -75,6 +91,7 @@
         Game {
             enemies: Vec::new(),
             anmrunners: Vec::new(),
+            player: Rc::new(RefCell::new(Player { pos: Position { x: 192., y: 384. } })),
             prng,
             rank,
             difficulty: 0,
@@ -107,6 +124,10 @@
         }
         sprites
     }
+
+    pub(crate) fn get_player(&self) -> Rc<RefCell<Player>> {
+        self.player.clone()
+    }
 }
 
 /// Common to all elements in game.
@@ -440,6 +461,13 @@
         let game = game.borrow();
         game.difficulty
     }
+
+    // TODO: use a trait for positionable entities.
+    pub(crate) fn get_angle_to(&self, player: Rc<RefCell<Player>>) -> f32 {
+        let player = player.borrow();
+        let offset = self.pos - player.pos;
+        offset.dy.atan2(offset.dx)
+    }
 }
 
 trait Renderable {