From b5e820ca459c64f4de010e47b6c71f19ced51301 Mon Sep 17 00:00:00 2001 From: Kumpelinus Date: Thu, 12 Dec 2024 19:02:18 +0100 Subject: [PATCH] WIP --- .../hyperion/src/egress/sync_entity_state.rs | 87 ++++++++++++++++--- crates/hyperion/src/simulation/mod.rs | 70 --------------- events/tag/src/command/shoot.rs | 2 +- 3 files changed, 77 insertions(+), 82 deletions(-) diff --git a/crates/hyperion/src/egress/sync_entity_state.rs b/crates/hyperion/src/egress/sync_entity_state.rs index 3aa02d61..b3295571 100644 --- a/crates/hyperion/src/egress/sync_entity_state.rs +++ b/crates/hyperion/src/egress/sync_entity_state.rs @@ -10,18 +10,12 @@ use valence_protocol::{ ByteAngle, RawBytes, VarInt, packets::play::{self}, }; +use valence_server::BlockState; use crate::{ - Prev, - net::{Compose, ConnectionId, DataBundle}, - simulation::{ - Pitch, Position, Velocity, Xp, Yaw, - animation::ActiveAnimation, - blocks::Blocks, - entity_kind::EntityKind, - handlers::is_grounded, - metadata::{MetadataChanges, get_and_clear_metadata}, - }, + net::{Compose, ConnectionId, DataBundle}, simulation::{ + animation::ActiveAnimation, blocks::Blocks, entity_kind::EntityKind, get_direction_from_rotation, get_rotation_from_velocity, handlers::is_grounded, metadata::{get_and_clear_metadata, MetadataChanges}, Pitch, Position, Velocity, Xp, Yaw + }, Prev }; #[derive(Component)] @@ -232,6 +226,77 @@ impl Module for EntityStateSyncModule { } }); + system!( + "update_projectile_positions", + world, + &mut Position, + &mut Yaw, + &mut Pitch, + &mut Velocity, + ?&ConnectionId + ) + .multi_threaded() + .kind::() + .with_enum_wildcard::() + .each_iter(|it, row, (position, yaw, pitch, velocity, connection_id)| { + if let Some(_connection_id) = connection_id { + return; + } + + let world = it.system().world(); + let _entity = it.entity(row); + + + if velocity.0 != Vec3::ZERO { + position.x += velocity.0.x; + position.y += velocity.0.y; + position.z += velocity.0.z; + + debug!( + "entity velocity: ({}, {}, {})", + velocity.0.x, velocity.0.y, velocity.0.z + ); + + // re calculate yaw and pitch based on velocity + let (new_yaw, new_pitch) = get_rotation_from_velocity(velocity.0); + *yaw = Yaw::new(new_yaw); + *pitch = Pitch::new(new_pitch); + + let center = **position; + + let direction = get_direction_from_rotation(new_yaw, new_pitch); + + + let ray = geometry::ray::Ray::new(center, direction); + + + #[allow(clippy::excessive_nesting)] + world.get::<&mut Blocks>(|blocks| { + // calculate distance limit based on velocity + let distance_limit = velocity.0.length(); + let Some(collision) = blocks.first_collision(ray, distance_limit) else { + //drag + velocity.0 *= 0.99 - 0.05; + + velocity.0.y -= 0.05; + return; + }; + debug!("distance_limit = {}", distance_limit); + + debug!("collision = {collision:?}"); + + velocity.0 = Vec3::ZERO; + + // Set arrow position to the collision location + **position = collision.normal; + + blocks + .set_block(collision.location, BlockState::DIRT) + .unwrap(); + }); + } + }); + // What ever you do DO NOT!!! I REPEAT DO NOT SET VELOCITY ANYWHERE // IF YOU WANT TO APPLY VELOCITY SEND 1 VELOCITY PAKCET WHEN NEEDED LOOK in events/tag/src/module/attack.rs system!( @@ -247,7 +312,7 @@ impl Module for EntityStateSyncModule { &Pitch, ) .multi_threaded() - .kind::() + .kind::() .each_iter( |it, row, diff --git a/crates/hyperion/src/simulation/mod.rs b/crates/hyperion/src/simulation/mod.rs index 38dfc4cf..39f35a55 100644 --- a/crates/hyperion/src/simulation/mod.rs +++ b/crates/hyperion/src/simulation/mod.rs @@ -697,76 +697,6 @@ impl Module for SimModule { }); }); - system!( - "update_projectile_positions", - world, - &mut Position, - &mut Yaw, - &mut Pitch, - &mut Velocity, - ?&ConnectionId - ) - .multi_threaded() - .kind::() - .with_enum_wildcard::() - .each_iter(|it, row, (position, yaw, pitch, velocity, connection_id)| { - if let Some(_connection_id) = connection_id { - return; - } - - let world = it.system().world(); - let _entity = it.entity(row); - - - if velocity.0 != Vec3::ZERO { - position.x += velocity.0.x; - position.y += velocity.0.y; - position.z += velocity.0.z; - - debug!( - "entity velocity: ({}, {}, {})", - velocity.0.x, velocity.0.y, velocity.0.z - ); - - // re calculate yaw and pitch based on velocity - let (new_yaw, new_pitch) = get_rotation_from_velocity(velocity.0); - *yaw = Yaw::new(new_yaw); - *pitch = Pitch::new(new_pitch); - - let center = **position; - - let direction = get_direction_from_rotation(new_yaw, new_pitch); - - - let ray = geometry::ray::Ray::new(center, direction); - - - #[allow(clippy::excessive_nesting)] - world.get::<&mut Blocks>(|blocks| { - // calculate distance limit based on velocity - let distance_limit = velocity.0.length(); - let Some(collision) = blocks.first_collision(ray, distance_limit) else { - //drag - velocity.0 *= 0.99 - 0.05; - - velocity.0.y -= 0.05; - return; - }; - debug!("distance_limit = {}", distance_limit); - - debug!("collision = {collision:?}"); - - velocity.0 = Vec3::ZERO; - - // Set arrow position to the collision location - **position = collision.normal; - - blocks - .set_block(collision.location, BlockState::DIRT) - .unwrap(); - }); - } - }); } } diff --git a/events/tag/src/command/shoot.rs b/events/tag/src/command/shoot.rs index 77a441f8..d8cc9456 100644 --- a/events/tag/src/command/shoot.rs +++ b/events/tag/src/command/shoot.rs @@ -31,7 +31,7 @@ impl MinecraftCommand for ShootCommand { let direction = super::raycast::get_direction_from_rotation(**yaw, **pitch); // Spawn arrow slightly in front of player to avoid self-collision - let spawn_pos = Vec3::new(pos.x, pos.y + EYE_HEIGHT, pos.z) + direction * 0.5; + let spawn_pos = Vec3::new(pos.x, pos.y + EYE_HEIGHT, pos.z) + direction * 1.0; // Calculate velocity with base multiplier let velocity = direction * (self.velocity * BASE_VELOCITY);