-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathauto_look.rs
More file actions
59 lines (49 loc) · 1.74 KB
/
auto_look.rs
File metadata and controls
59 lines (49 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use azalea::{
app::{App, Plugin},
bot::LookAtEvent,
ecs::prelude::*,
entity::{Position, dimensions::EntityDimensions, metadata::Player},
nearest_entity::EntityFinder,
pathfinder::Pathfinder,
physics::PhysicsSystems,
prelude::*,
};
use crate::prelude::{GameTicks, LocalSettings};
/// Automatically look at the closest player in range
pub struct AutoLookPlugin;
impl Plugin for AutoLookPlugin {
fn build(&self, app: &mut App) {
app.add_systems(GameTick, Self::handle_auto_look.before(PhysicsSystems));
}
}
impl AutoLookPlugin {
pub fn handle_auto_look(
mut query: Query<(Entity, &Pathfinder, &GameTicks, &LocalSettings)>,
entities: EntityFinder<With<Player>>,
targets: Query<(&Position, Option<&EntityDimensions>)>,
mut look_at_events: MessageWriter<LookAtEvent>,
) {
for (entity, pathfinder, game_ticks, local_settings) in &mut query {
if !local_settings.auto_look.enabled {
continue;
}
if let Some(_goal) = &pathfinder.goal {
continue;
}
if game_ticks.0 % local_settings.auto_look.delay_ticks != 0 {
continue;
}
let Some(target) = entities.nearest_to_entity(entity, f64::MAX) else {
continue;
};
let Ok((target_pos, target_entity_dimensions)) = targets.get(target) else {
continue;
};
let mut position = **target_pos;
if let Some(entity_dimensions) = target_entity_dimensions {
position.y += f64::from(entity_dimensions.eye_height);
}
look_at_events.write(LookAtEvent { entity, position });
}
}
}