-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_look.rs
54 lines (45 loc) · 1.52 KB
/
auto_look.rs
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
use azalea::{
app::{App, Plugin},
ecs::prelude::*,
entity::{metadata::Player, EyeHeight, Position},
nearest_entity::EntityFinder,
physics::PhysicsSet,
prelude::*,
LookAtEvent,
};
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(PhysicsSet));
}
}
impl AutoLookPlugin {
pub fn handle_auto_look(
mut query: Query<(Entity, &GameTicks, &LocalSettings)>,
entities: EntityFinder<With<Player>>,
targets: Query<(&Position, Option<&EyeHeight>)>,
mut look_at_events: EventWriter<LookAtEvent>,
) {
for (entity, game_ticks, local_settings) in &mut query {
if !local_settings.auto_look.enabled {
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_eye_height)) = targets.get(target) else {
continue;
};
let mut position = **target_pos;
if let Some(eye_height) = target_eye_height {
position.y += f64::from(**eye_height);
}
look_at_events.send(LookAtEvent { entity, position });
}
}
}