# HG changeset patch # User Emmanuel Gil Peyrot # Date 1566048825 -7200 # Node ID 14fddc27e6f5865f8502937dbb16a9cc179133ee # Parent a35df65e0d57c3650f816df00b1eb8406729d1bd ecl_vm: implement TargetPlayer, and add a dummy Player to Game. diff --git a/src/th06/ecl_vm.rs b/src/th06/ecl_vm.rs --- a/src/th06/ecl_vm.rs +++ b/src/th06/ecl_vm.rs @@ -532,16 +532,16 @@ impl EclRunner { 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 diff --git a/src/th06/enemy.rs b/src/th06/enemy.rs --- a/src/th06/enemy.rs +++ b/src/th06/enemy.rs @@ -47,6 +47,16 @@ impl std::ops::Add for Position } } +impl std::ops::Sub 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>>, anmrunners: Vec>>, + player: Rc>, prng: Rc>, 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> { + 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>) -> f32 { + let player = player.borrow(); + let offset = self.pos - player.pos; + offset.dy.atan2(offset.dx) + } } trait Renderable {