azalea_client/plugins/
attack.rs1use azalea_core::{game_type::GameMode, tick::GameTick};
2use azalea_entity::{
3 Attributes, Physics, indexing::EntityIdIndex, metadata::Sprinting, update_bounding_box,
4};
5use azalea_physics::PhysicsSystems;
6use azalea_protocol::packets::game::ServerboundAttack;
7use bevy_app::{App, Plugin, Update};
8use bevy_ecs::prelude::*;
9use derive_more::{Deref, DerefMut};
10use tracing::warn;
11
12use super::packet::game::SendGamePacketEvent;
13use crate::{interact::SwingArmEvent, movement::MoveEventsSystems, respawn::perform_respawn};
14
15pub struct AttackPlugin;
16impl Plugin for AttackPlugin {
17 fn build(&self, app: &mut App) {
18 app.add_message::<AttackEvent>()
19 .add_systems(
20 Update,
21 handle_attack_event
22 .before(update_bounding_box)
23 .before(MoveEventsSystems)
24 .after(perform_respawn),
25 )
26 .add_systems(
27 GameTick,
28 (
29 increment_ticks_since_last_attack,
30 update_attack_strength_scale.after(PhysicsSystems),
31 handle_attack_queued
34 .before(super::movement::update_pose)
35 .before(super::tick_end::game_tick_packet),
36 )
37 .chain(),
38 );
39 }
40}
41
42#[derive(Clone, Component, Debug)]
45pub struct AttackQueued {
46 pub target: Entity,
47}
48#[allow(clippy::type_complexity)]
49pub fn handle_attack_queued(
50 mut commands: Commands,
51 mut query: Query<(
52 Entity,
53 &mut TicksSinceLastAttack,
54 &mut Physics,
55 &mut Sprinting,
56 &AttackQueued,
57 &GameMode,
58 &EntityIdIndex,
59 )>,
60) {
61 for (
62 client_entity,
63 mut ticks_since_last_attack,
64 mut physics,
65 mut sprinting,
66 attack_queued,
67 &game_mode,
68 entity_id_index,
69 ) in &mut query
70 {
71 let target_entity = attack_queued.target;
72
73 commands.entity(client_entity).remove::<AttackQueued>();
76
77 let Some(target_entity_id) = entity_id_index.get_by_ecs_entity(target_entity) else {
78 warn!("tried to attack entity {target_entity} which isn't in our EntityIdIndex");
79 continue;
80 };
81
82 commands.trigger(SendGamePacketEvent::new(
83 client_entity,
84 ServerboundAttack {
85 entity_id: target_entity_id,
86 },
87 ));
88 commands.trigger(SwingArmEvent {
89 entity: client_entity,
90 });
91
92 if game_mode == GameMode::Spectator {
95 continue;
96 };
97
98 ticks_since_last_attack.0 = 0;
99
100 physics.velocity = physics.velocity.multiply(0.6, 1.0, 0.6);
101 **sprinting = false;
102 }
103}
104
105#[derive(Message)]
108pub struct AttackEvent {
109 pub entity: Entity,
111 pub target: Entity,
113}
114pub fn handle_attack_event(mut events: MessageReader<AttackEvent>, mut commands: Commands) {
115 for event in events.read() {
116 commands.entity(event.entity).insert(AttackQueued {
117 target: event.target,
118 });
119 }
120}
121
122#[derive(Bundle, Default)]
123pub struct AttackBundle {
124 pub ticks_since_last_attack: TicksSinceLastAttack,
125 pub attack_strength_scale: AttackStrengthScale,
126}
127
128#[derive(Clone, Component, Default, Deref, DerefMut)]
129pub struct TicksSinceLastAttack(pub u32);
130pub fn increment_ticks_since_last_attack(mut query: Query<&mut TicksSinceLastAttack>) {
131 for mut ticks_since_last_attack in query.iter_mut() {
132 **ticks_since_last_attack += 1;
133 }
134}
135
136#[derive(Clone, Component, Default, Deref, DerefMut)]
137pub struct AttackStrengthScale(pub f32);
138pub fn update_attack_strength_scale(
139 mut query: Query<(&TicksSinceLastAttack, &Attributes, &mut AttackStrengthScale)>,
140) {
141 for (ticks_since_last_attack, attributes, mut attack_strength_scale) in query.iter_mut() {
142 **attack_strength_scale =
144 get_attack_strength_scale(ticks_since_last_attack.0, attributes, 0.5);
145 }
146}
147
148pub fn get_attack_strength_delay(attributes: &Attributes) -> f32 {
150 ((1. / attributes.attack_speed.calculate()) * 20.) as f32
151}
152
153pub fn get_attack_strength_scale(
154 ticks_since_last_attack: u32,
155 attributes: &Attributes,
156 in_ticks: f32,
157) -> f32 {
158 let attack_strength_delay = get_attack_strength_delay(attributes);
159 let attack_strength = (ticks_since_last_attack as f32 + in_ticks) / attack_strength_delay;
160 attack_strength.clamp(0., 1.)
161}