aboutsummaryrefslogtreecommitdiff
path: root/azalea-physics/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'azalea-physics/src/lib.rs')
-rw-r--r--azalea-physics/src/lib.rs117
1 files changed, 41 insertions, 76 deletions
diff --git a/azalea-physics/src/lib.rs b/azalea-physics/src/lib.rs
index 27250f61..fb62b48b 100644
--- a/azalea-physics/src/lib.rs
+++ b/azalea-physics/src/lib.rs
@@ -4,6 +4,7 @@
pub mod clip;
pub mod collision;
pub mod fluids;
+pub mod local_player;
pub mod travel;
use std::collections::HashSet;
@@ -16,18 +17,17 @@ use azalea_core::{
};
use azalea_entity::{
Attributes, EntityKindComponent, HasClientLoaded, Jumping, LocalEntity, LookDirection,
- OnClimbable, Physics, Pose, Position, metadata::Sprinting, move_relative,
+ OnClimbable, Physics, Pose, Position, dimensions::EntityDimensions, metadata::Sprinting,
+ move_relative,
};
use azalea_registry::{Block, EntityKind};
use azalea_world::{Instance, InstanceContainer, InstanceName};
use bevy_app::{App, Plugin};
use bevy_ecs::prelude::*;
use clip::box_traverse_blocks;
-use collision::{
- BLOCK_SHAPE, BlockWithShape, MoverType, VoxelShape,
- entity_collisions::{CollidableEntityQuery, PhysicsQuery},
- move_colliding,
-};
+use collision::{BLOCK_SHAPE, BlockWithShape, VoxelShape, move_colliding};
+
+use crate::collision::MoveCtx;
/// A Bevy [`SystemSet`] for running physics that makes entities do things.
#[derive(SystemSet, Debug, Hash, PartialEq, Eq, Clone)]
@@ -161,12 +161,12 @@ fn jump_in_liquid(physics: &mut Physics) {
#[allow(clippy::type_complexity)]
pub fn apply_effects_from_blocks(
mut query: Query<
- (&mut Physics, &Position, &InstanceName),
+ (&mut Physics, &Position, &EntityDimensions, &InstanceName),
(With<LocalEntity>, With<HasClientLoaded>),
>,
instance_container: Res<InstanceContainer>,
) {
- for (mut physics, position, world_name) in &mut query {
+ for (mut physics, position, dimensions, world_name) in &mut query {
let Some(world_lock) = instance_container.get(world_name) else {
continue;
};
@@ -189,12 +189,13 @@ pub fn apply_effects_from_blocks(
to: **position,
}];
- check_inside_blocks(&mut physics, &world, &movement_this_tick);
+ check_inside_blocks(&mut physics, dimensions, &world, &movement_this_tick);
}
}
fn check_inside_blocks(
physics: &mut Physics,
+ dimensions: &EntityDimensions,
world: &Instance,
movements: &[EntityMovement],
) -> Vec<BlockState> {
@@ -202,8 +203,7 @@ fn check_inside_blocks(
let mut visited_blocks = HashSet::<BlockState>::new();
for movement in movements {
- let bounding_box_at_target = physics
- .dimensions
+ let bounding_box_at_target = dimensions
.make_bounding_box(movement.to)
.deflate_all(1.0E-5);
@@ -242,7 +242,7 @@ fn check_inside_blocks(
movement.to,
traversed_block,
entity_inside_collision_shape,
- physics,
+ dimensions,
)
{
continue;
@@ -262,9 +262,9 @@ fn collided_with_shape_moving_from(
to: Vec3,
traversed_block: BlockPos,
entity_inside_collision_shape: &VoxelShape,
- physics: &Physics,
+ dimensions: &EntityDimensions,
) -> bool {
- let bounding_box_from = physics.dimensions.make_bounding_box(from);
+ let bounding_box_from = dimensions.make_bounding_box(from);
let delta = to - from;
bounding_box_from.collided_along_vector(
delta,
@@ -367,63 +367,27 @@ fn get_block_pos_below_that_affects_movement(position: Position) -> BlockPos {
)
}
-/// Options for [`handle_relative_friction_and_calculate_movement`]
-struct HandleRelativeFrictionAndCalculateMovementOpts<'a, 'b, 'world, 'state> {
- block_friction: f32,
- world: &'a Instance,
- physics: &'a mut Physics,
- direction: LookDirection,
- position: Mut<'a, Position>,
- attributes: &'a Attributes,
- is_sprinting: bool,
- on_climbable: OnClimbable,
- pose: Option<Pose>,
- jumping: Jumping,
- entity: Entity,
- physics_query: &'a PhysicsQuery<'world, 'state, 'b>,
- collidable_entity_query: &'a CollidableEntityQuery<'world, 'state>,
-}
-fn handle_relative_friction_and_calculate_movement(
- HandleRelativeFrictionAndCalculateMovementOpts {
- block_friction,
- world,
- physics,
- direction,
- mut position,
- attributes,
- is_sprinting,
- on_climbable,
- pose,
- jumping,
- entity,
- physics_query,
- collidable_entity_query,
- }: HandleRelativeFrictionAndCalculateMovementOpts<'_, '_, '_, '_>,
-) -> Vec3 {
+fn handle_relative_friction_and_calculate_movement(ctx: &mut MoveCtx, block_friction: f32) -> Vec3 {
move_relative(
- physics,
- direction,
- get_friction_influenced_speed(physics, attributes, block_friction, is_sprinting),
+ ctx.physics,
+ ctx.direction,
+ get_friction_influenced_speed(ctx.physics, ctx.attributes, block_friction, ctx.sprinting),
Vec3::new(
- physics.x_acceleration as f64,
- physics.y_acceleration as f64,
- physics.z_acceleration as f64,
+ ctx.physics.x_acceleration as f64,
+ ctx.physics.y_acceleration as f64,
+ ctx.physics.z_acceleration as f64,
),
);
- physics.velocity = handle_on_climbable(physics.velocity, on_climbable, *position, world, pose);
-
- move_colliding(
- MoverType::Own,
- physics.velocity,
- world,
- &mut position,
- physics,
- Some(entity),
- physics_query,
- collidable_entity_query,
- )
- .expect("Entity should exist");
+ ctx.physics.velocity = handle_on_climbable(
+ ctx.physics.velocity,
+ ctx.on_climbable,
+ *ctx.position,
+ ctx.world,
+ ctx.pose,
+ );
+
+ move_colliding(ctx, ctx.physics.velocity).expect("Entity should exist");
// let delta_movement = entity.delta;
// ladders
// if ((entity.horizontalCollision || entity.jumping) && (entity.onClimbable()
@@ -431,19 +395,20 @@ fn handle_relative_friction_and_calculate_movement(
// PowderSnowBlock.canEntityWalkOnPowderSnow(entity))) { var3 = new
// Vec3(var3.x, 0.2D, var3.z); }
- if physics.horizontal_collision || *jumping {
- let block_at_feet: Block = world
+ if ctx.physics.horizontal_collision || *ctx.jumping {
+ let block_at_feet: Block = ctx
+ .world
.chunks
- .get_block_state((*position).into())
+ .get_block_state(BlockPos::from(*ctx.position))
.unwrap_or_default()
.into();
- if *on_climbable || block_at_feet == Block::PowderSnow {
- physics.velocity.y = 0.2;
+ if *ctx.on_climbable || block_at_feet == Block::PowderSnow {
+ ctx.physics.velocity.y = 0.2;
}
}
- physics.velocity
+ ctx.physics.velocity
}
fn handle_on_climbable(
@@ -467,7 +432,7 @@ fn handle_on_climbable(
// sneaking on ladders/vines
if y < 0.0
- && pose == Some(Pose::Sneaking)
+ && pose == Some(Pose::Crouching)
&& azalea_registry::Block::from(
world
.chunks
@@ -488,15 +453,15 @@ fn get_friction_influenced_speed(
physics: &Physics,
attributes: &Attributes,
friction: f32,
- is_sprinting: bool,
+ sprinting: Sprinting,
) -> f32 {
// TODO: have speed & flying_speed fields in entity
if physics.on_ground() {
- let speed: f32 = attributes.speed.calculate() as f32;
+ let speed = attributes.movement_speed.calculate() as f32;
speed * (0.21600002f32 / (friction * friction * friction))
} else {
// entity.flying_speed
- if is_sprinting { 0.025999999f32 } else { 0.02 }
+ if *sprinting { 0.025999999f32 } else { 0.02 }
}
}