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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
use azalea_core::{game_type::GameMode, tick::GameTick};
use azalea_entity::{
Attributes, Physics,
metadata::{ShiftKeyDown, Sprinting},
update_bounding_box,
};
use azalea_physics::PhysicsSet;
use azalea_protocol::packets::game::s_interact::{self, ServerboundInteract};
use azalea_world::MinecraftEntityId;
use bevy_app::{App, Plugin, Update};
use bevy_ecs::prelude::*;
use derive_more::{Deref, DerefMut};
use super::packet::game::SendPacketEvent;
use crate::{
Client, interact::SwingArmEvent, local_player::LocalGameMode, movement::MoveEventsSet,
respawn::perform_respawn,
};
pub struct AttackPlugin;
impl Plugin for AttackPlugin {
fn build(&self, app: &mut App) {
app.add_event::<AttackEvent>()
.add_systems(
Update,
handle_attack_event
.before(update_bounding_box)
.before(MoveEventsSet)
.after(perform_respawn),
)
.add_systems(
GameTick,
(
increment_ticks_since_last_attack,
update_attack_strength_scale.after(PhysicsSet),
handle_attack_queued
.before(super::tick_end::game_tick_packet)
.after(super::movement::send_sprinting_if_needed)
.before(super::movement::send_position),
)
.chain(),
);
}
}
impl Client {
/// Attack the entity with the given id.
pub fn attack(&self, entity_id: MinecraftEntityId) {
self.ecs.lock().send_event(AttackEvent {
entity: self.entity,
target: entity_id,
});
}
/// Whether the player has an attack cooldown.
pub fn has_attack_cooldown(&self) -> bool {
let Some(AttackStrengthScale(ticks_since_last_attack)) =
self.get_component::<AttackStrengthScale>()
else {
// they don't even have an AttackStrengthScale so they probably can't attack
// lmao, just return false
return false;
};
ticks_since_last_attack < 1.0
}
}
/// A component that indicates that this client will be attacking the given
/// entity next tick.
#[derive(Component, Clone, Debug)]
pub struct AttackQueued {
pub target: MinecraftEntityId,
}
pub fn handle_attack_queued(
mut commands: Commands,
mut query: Query<(
Entity,
&AttackQueued,
&LocalGameMode,
&mut TicksSinceLastAttack,
&mut Physics,
&mut Sprinting,
&ShiftKeyDown,
)>,
) {
for (
entity,
attack_queued,
game_mode,
mut ticks_since_last_attack,
mut physics,
mut sprinting,
sneaking,
) in &mut query
{
commands.entity(entity).remove::<AttackQueued>();
commands.trigger(SendPacketEvent::new(
entity,
ServerboundInteract {
entity_id: attack_queued.target,
action: s_interact::ActionType::Attack,
using_secondary_action: **sneaking,
},
));
commands.trigger(SwingArmEvent { entity });
// we can't attack if we're in spectator mode but it still sends the attack
// packet
if game_mode.current == GameMode::Spectator {
continue;
};
ticks_since_last_attack.0 = 0;
physics.velocity = physics.velocity.multiply(0.6, 1.0, 0.6);
**sprinting = false;
}
}
/// Queues up an attack packet for next tick by inserting the [`AttackQueued`]
/// component to our client.
#[derive(Event)]
pub struct AttackEvent {
/// Our client entity that will send the packets to attack.
pub entity: Entity,
pub target: MinecraftEntityId,
}
pub fn handle_attack_event(mut events: EventReader<AttackEvent>, mut commands: Commands) {
for event in events.read() {
commands.entity(event.entity).insert(AttackQueued {
target: event.target,
});
}
}
#[derive(Default, Bundle)]
pub struct AttackBundle {
pub ticks_since_last_attack: TicksSinceLastAttack,
pub attack_strength_scale: AttackStrengthScale,
}
#[derive(Default, Component, Clone, Deref, DerefMut)]
pub struct TicksSinceLastAttack(pub u32);
pub fn increment_ticks_since_last_attack(mut query: Query<&mut TicksSinceLastAttack>) {
for mut ticks_since_last_attack in query.iter_mut() {
**ticks_since_last_attack += 1;
}
}
#[derive(Default, Component, Clone, Deref, DerefMut)]
pub struct AttackStrengthScale(pub f32);
pub fn update_attack_strength_scale(
mut query: Query<(&TicksSinceLastAttack, &Attributes, &mut AttackStrengthScale)>,
) {
for (ticks_since_last_attack, attributes, mut attack_strength_scale) in query.iter_mut() {
// look 0.5 ticks into the future because that's what vanilla does
**attack_strength_scale =
get_attack_strength_scale(ticks_since_last_attack.0, attributes, 0.5);
}
}
/// Returns how long it takes for the attack cooldown to reset (in ticks).
pub fn get_attack_strength_delay(attributes: &Attributes) -> f32 {
((1. / attributes.attack_speed.calculate()) * 20.) as f32
}
pub fn get_attack_strength_scale(
ticks_since_last_attack: u32,
attributes: &Attributes,
in_ticks: f32,
) -> f32 {
let attack_strength_delay = get_attack_strength_delay(attributes);
let attack_strength = (ticks_since_last_attack as f32 + in_ticks) / attack_strength_delay;
attack_strength.clamp(0., 1.)
}
|