docs: add comments explaining the enemy movement system

This commit is contained in:
Muhammad Nauman Raza 2024-05-27 23:09:38 +01:00
parent b7611b88c9
commit 3d082b8cc8
No known key found for this signature in database
GPG key ID: B0EF3A98B29ADB1D

View file

@ -3,7 +3,7 @@ use std::f32::consts::PI;
use crate::player::*;
// Define the player component
// Define the enemy component
#[derive(Component)]
pub struct Enemy {
pub name: String,
@ -29,19 +29,24 @@ pub fn enemy_movement(
) {
let enemy = enemy_query.single_mut();
// Bring the player translation into scope
let mut player_translation = Vec3::ZERO;
for player_transform in set.p0().iter_mut() {
player_translation = player_transform.translation;
}
// Get the enemy's movement distance (based on movement speed)
let movement_distance = enemy.movement_speed * time.delta_seconds();
for mut enemy_transform in set.p1().iter_mut() {
// Calculate the angle from the enemy to the player's position
let difference = player_translation - enemy_transform.translation;
let angle = difference.y.atan2(difference.x) - PI/2.;
let angle = difference.y.atan2(difference.x) - PI/2.; // Subtract PI/2. to orient the enemy correctly
// Return a Quat for the enemy's movement direction based on the angle
let movement_direction = Quat::from_axis_angle(Vec3::new(0., 0., 1.), angle);
// Update the rotation and translation of the enemy
enemy_transform.rotation = movement_direction;
enemy_transform.translation += movement_direction * Vec3::Y * movement_distance;
}