mirror of
https://github.com/mat-1/azalea.git
synced 2025-08-02 23:44:38 +00:00
take Entity instead of MinecraftEntityId in Client::attack
This commit is contained in:
parent
f82cf7fa85
commit
319d144995
2 changed files with 27 additions and 14 deletions
|
@ -16,6 +16,7 @@ is breaking anyways, semantic versioning is not followed.
|
||||||
|
|
||||||
- Renamed `azalea_entity::EntityKind` to `EntityKindComponent` to disambiguate with `azalea_registry::EntityKind`.
|
- Renamed `azalea_entity::EntityKind` to `EntityKindComponent` to disambiguate with `azalea_registry::EntityKind`.
|
||||||
- Moved functions and types related to hit results from `azalea::interact` to `azalea::interact::pick`.
|
- Moved functions and types related to hit results from `azalea::interact` to `azalea::interact::pick`.
|
||||||
|
- `Client::attack` now takes `Entity` instead of `MinecraftEntityId`.
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,16 @@
|
||||||
use azalea_core::{game_type::GameMode, tick::GameTick};
|
use azalea_core::{game_type::GameMode, tick::GameTick};
|
||||||
use azalea_entity::{
|
use azalea_entity::{
|
||||||
Attributes, Physics,
|
Attributes, Physics,
|
||||||
|
indexing::EntityIdIndex,
|
||||||
metadata::{ShiftKeyDown, Sprinting},
|
metadata::{ShiftKeyDown, Sprinting},
|
||||||
update_bounding_box,
|
update_bounding_box,
|
||||||
};
|
};
|
||||||
use azalea_physics::PhysicsSet;
|
use azalea_physics::PhysicsSet;
|
||||||
use azalea_protocol::packets::game::s_interact::{self, ServerboundInteract};
|
use azalea_protocol::packets::game::s_interact::{self, ServerboundInteract};
|
||||||
use azalea_world::MinecraftEntityId;
|
|
||||||
use bevy_app::{App, Plugin, Update};
|
use bevy_app::{App, Plugin, Update};
|
||||||
use bevy_ecs::prelude::*;
|
use bevy_ecs::prelude::*;
|
||||||
use derive_more::{Deref, DerefMut};
|
use derive_more::{Deref, DerefMut};
|
||||||
|
use tracing::warn;
|
||||||
|
|
||||||
use super::packet::game::SendPacketEvent;
|
use super::packet::game::SendPacketEvent;
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -45,10 +46,10 @@ impl Plugin for AttackPlugin {
|
||||||
|
|
||||||
impl Client {
|
impl Client {
|
||||||
/// Attack the entity with the given id.
|
/// Attack the entity with the given id.
|
||||||
pub fn attack(&self, entity_id: MinecraftEntityId) {
|
pub fn attack(&self, entity: Entity) {
|
||||||
self.ecs.lock().send_event(AttackEvent {
|
self.ecs.lock().send_event(AttackEvent {
|
||||||
entity: self.entity,
|
entity: self.entity,
|
||||||
target: entity_id,
|
target: entity,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,41 +88,51 @@ impl Client {
|
||||||
/// entity next tick.
|
/// entity next tick.
|
||||||
#[derive(Component, Clone, Debug)]
|
#[derive(Component, Clone, Debug)]
|
||||||
pub struct AttackQueued {
|
pub struct AttackQueued {
|
||||||
pub target: MinecraftEntityId,
|
pub target: Entity,
|
||||||
}
|
}
|
||||||
pub fn handle_attack_queued(
|
pub fn handle_attack_queued(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
mut query: Query<(
|
mut query: Query<(
|
||||||
Entity,
|
Entity,
|
||||||
&AttackQueued,
|
|
||||||
&LocalGameMode,
|
|
||||||
&mut TicksSinceLastAttack,
|
&mut TicksSinceLastAttack,
|
||||||
&mut Physics,
|
&mut Physics,
|
||||||
&mut Sprinting,
|
&mut Sprinting,
|
||||||
|
&AttackQueued,
|
||||||
|
&LocalGameMode,
|
||||||
&ShiftKeyDown,
|
&ShiftKeyDown,
|
||||||
|
&EntityIdIndex,
|
||||||
)>,
|
)>,
|
||||||
) {
|
) {
|
||||||
for (
|
for (
|
||||||
entity,
|
client_entity,
|
||||||
attack_queued,
|
|
||||||
game_mode,
|
|
||||||
mut ticks_since_last_attack,
|
mut ticks_since_last_attack,
|
||||||
mut physics,
|
mut physics,
|
||||||
mut sprinting,
|
mut sprinting,
|
||||||
|
attack_queued,
|
||||||
|
game_mode,
|
||||||
sneaking,
|
sneaking,
|
||||||
|
entity_id_index,
|
||||||
) in &mut query
|
) in &mut query
|
||||||
{
|
{
|
||||||
commands.entity(entity).remove::<AttackQueued>();
|
let target_entity = attack_queued.target;
|
||||||
|
let Some(target_entity_id) = entity_id_index.get_by_ecs_entity(target_entity) else {
|
||||||
|
warn!("tried to attack entity {target_entity} which isn't in our EntityIdIndex");
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
commands.entity(client_entity).remove::<AttackQueued>();
|
||||||
|
|
||||||
commands.trigger(SendPacketEvent::new(
|
commands.trigger(SendPacketEvent::new(
|
||||||
entity,
|
client_entity,
|
||||||
ServerboundInteract {
|
ServerboundInteract {
|
||||||
entity_id: attack_queued.target,
|
entity_id: target_entity_id,
|
||||||
action: s_interact::ActionType::Attack,
|
action: s_interact::ActionType::Attack,
|
||||||
using_secondary_action: **sneaking,
|
using_secondary_action: **sneaking,
|
||||||
},
|
},
|
||||||
));
|
));
|
||||||
commands.trigger(SwingArmEvent { entity });
|
commands.trigger(SwingArmEvent {
|
||||||
|
entity: client_entity,
|
||||||
|
});
|
||||||
|
|
||||||
// we can't attack if we're in spectator mode but it still sends the attack
|
// we can't attack if we're in spectator mode but it still sends the attack
|
||||||
// packet
|
// packet
|
||||||
|
@ -142,7 +153,8 @@ pub fn handle_attack_queued(
|
||||||
pub struct AttackEvent {
|
pub struct AttackEvent {
|
||||||
/// Our client entity that will send the packets to attack.
|
/// Our client entity that will send the packets to attack.
|
||||||
pub entity: Entity,
|
pub entity: Entity,
|
||||||
pub target: MinecraftEntityId,
|
/// The entity that will be attacked.
|
||||||
|
pub target: Entity,
|
||||||
}
|
}
|
||||||
pub fn handle_attack_event(mut events: EventReader<AttackEvent>, mut commands: Commands) {
|
pub fn handle_attack_event(mut events: EventReader<AttackEvent>, mut commands: Commands) {
|
||||||
for event in events.read() {
|
for event in events.read() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue