diff src/th06/enemy.rs @ 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 05e0425a8bc5
line wrap: on
line diff
--- a/src/th06/enemy.rs
+++ b/src/th06/enemy.rs
@@ -47,6 +47,16 @@ impl std::ops::Add<Offset> for Position 
     }
 }
 
+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 @@ pub struct Laser {
 #[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 @@ impl Game {
         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 @@ impl Game {
         }
         sprites
     }
+
+    pub(crate) fn get_player(&self) -> Rc<RefCell<Player>> {
+        self.player.clone()
+    }
 }
 
 /// Common to all elements in game.
@@ -440,6 +461,13 @@ impl Enemy {
         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 {