1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 06:16:04 +00:00
This commit is contained in:
mat 2023-03-13 15:38:44 -05:00
parent 5c38b34dc1
commit b44dc94274
4 changed files with 78 additions and 17 deletions

View file

@ -68,15 +68,20 @@ pub struct BlockInteractEvent {
#[derive(Component, Copy, Clone, Debug, Default, Deref, DerefMut)]
pub struct CurrentSequenceNumber(u32);
/// A component that contains the block that the player is currently looking at.
#[derive(Component, Clone, Debug, Deref, DerefMut)]
pub struct HitResultComponent(BlockHitResult);
fn handle_block_interact_event(
mut events: EventReader<BlockInteractEvent>,
mut query: Query<(&LocalPlayer, &mut CurrentSequenceNumber)>,
mut query: Query<(
&LocalPlayer,
&mut CurrentSequenceNumber,
&HitResultComponent,
)>,
) {
for event in events.iter() {
let Ok((local_player, mut sequence_number)) = query.get_mut(event.entity) else {
let Ok((local_player, mut sequence_number, hit_result)) = query.get_mut(event.entity) else {
warn!("Sent BlockInteractEvent for entity that isn't LocalPlayer");
continue;
};
@ -88,15 +93,31 @@ fn handle_block_interact_event(
// minecraft also does the interaction client-side (so it looks like clicking a
// button is instant) but we don't really need that
// the block_hit data will depend on whether we're looking at the block and
// whether we can reach it
let block_hit = if hit_result.block_pos == event.position {
// we're looking at the block :)
BlockHit {
block_pos: hit_result.block_pos,
direction: hit_result.direction,
location: hit_result.location,
inside: hit_result.inside,
}
} else {
// we're not looking at the block, so make up some numbers
BlockHit {
block_pos: event.position,
direction: Direction::Up,
location: event.position.center(),
inside: false,
}
};
local_player.write_packet(
ServerboundUseItemOnPacket {
hand: InteractionHand::MainHand,
block_hit: BlockHit {
block_pos: event.position,
direction: Direction::Up,
location: event.position.center(),
inside: false,
},
block_hit,
sequence: sequence_number.0,
}
.get(),
@ -148,6 +169,11 @@ fn update_hit_result_component(
}
}
/// Get the block that a player would be looking at if their eyes were at the
/// given direction and position.
///
/// If you need to get the block the player is looking at right now, use
/// [`HitResultComponent`].
pub fn pick(
look_direction: &LookDirection,
eye_position: &Vec3,

View file

@ -269,6 +269,26 @@ pub fn add_dead(mut commands: Commands, query: Query<(Entity, &Health), Changed<
/// an entity, and when raytracing from the entity.
#[derive(Component, Clone, Copy, Debug, PartialEq, Deref, DerefMut)]
pub struct EyeHeight(f32);
impl From<EyeHeight> for f32 {
fn from(value: EyeHeight) -> Self {
value.0
}
}
impl From<EyeHeight> for f64 {
fn from(value: EyeHeight) -> Self {
value.0 as f64
}
}
impl From<&EyeHeight> for f32 {
fn from(value: &EyeHeight) -> Self {
value.0
}
}
impl From<&EyeHeight> for f64 {
fn from(value: &EyeHeight) -> Self {
value.0 as f64
}
}
/// A component NewType for [`azalea_registry::EntityKind`].
///

View file

@ -4,7 +4,7 @@
use azalea::ecs::query::With;
use azalea::entity::metadata::Player;
use azalea::entity::Position;
use azalea::entity::{EyeHeight, Position};
use azalea::interact::HitResultComponent;
use azalea::inventory::InventoryComponent;
use azalea::pathfinder::BlockPosGoal;
@ -124,10 +124,11 @@ async fn handle(mut bot: Client, event: Event, _state: State) -> anyhow::Result<
bot.goto(BlockPosGoal::from(target_pos));
}
"look" => {
let entity_pos = bot.entity_component::<Position>(entity);
let target_pos: BlockPos = entity_pos.into();
println!("target_pos: {target_pos:?}");
bot.look_at(target_pos.center());
let entity_pos = bot
.entity_component::<Position>(entity)
.up(bot.entity_component::<EyeHeight>(entity).into());
println!("entity_pos: {entity_pos:?}");
bot.look_at(entity_pos);
}
"jump" => {
bot.set_jumping(true);
@ -166,6 +167,19 @@ async fn handle(mut bot: Client, event: Event, _state: State) -> anyhow::Result<
bot.chat("no diamond block found");
}
}
"lever" => {
let target_pos = bot
.world()
.read()
.find_block(bot.position(), &azalea_registry::Block::Lever.into());
let Some(target_pos) = target_pos else {
bot.chat("no lever found");
return Ok(())
};
bot.goto(BlockPosGoal::from(target_pos));
bot.look_at(target_pos.center());
bot.block_interact(target_pos);
}
"hitresult" => {
let hit_result = bot.get_component::<HitResultComponent>();
bot.chat(&format!("hit_result: {hit_result:?}",));

View file

@ -9,7 +9,7 @@ use crate::ecs::{
};
use azalea_core::Vec3;
use azalea_physics::{force_jump_listener, PhysicsSet};
use azalea_world::entity::{clamp_look_direction, LookDirection};
use azalea_world::entity::{clamp_look_direction, EyeHeight, LookDirection};
use azalea_world::entity::{metadata::Player, Jumping, Local, Position};
use std::f64::consts::PI;
@ -102,11 +102,12 @@ pub struct LookAtEvent {
}
fn look_at_listener(
mut events: EventReader<LookAtEvent>,
mut query: Query<(&Position, &mut LookDirection)>,
mut query: Query<(&Position, &EyeHeight, &mut LookDirection)>,
) {
for event in events.iter() {
if let Ok((position, mut look_direction)) = query.get_mut(event.entity) {
let (y_rot, x_rot) = direction_looking_at(position, &event.position);
if let Ok((position, eye_height, mut look_direction)) = query.get_mut(event.entity) {
let (y_rot, x_rot) =
direction_looking_at(&position.up(eye_height.into()), &event.position);
(look_direction.y_rot, look_direction.x_rot) = (y_rot, x_rot);
}
}