Skip to content

Commit b969b32

Browse files
committed
Added swimming test
1 parent 7f3991c commit b969b32

File tree

1 file changed

+77
-1
lines changed

1 file changed

+77
-1
lines changed

azalea-physics/tests/physics.rs

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ use azalea_core::{
1010
registry_holder::RegistryHolder,
1111
tick::GameTick,
1212
};
13-
use azalea_entity::{EntityBundle, EntityPlugin, HasClientLoaded, LocalEntity, Physics, Position};
13+
use azalea_entity::{
14+
EntityBundle, EntityPlugin, HasClientLoaded, LocalEntity, Physics, Position,
15+
metadata::{Sprinting, Swimming},
16+
};
1417
use azalea_physics::PhysicsPlugin;
1518
use azalea_registry::builtin::{BlockKind, EntityKind};
1619
use azalea_world::{Chunk, PartialWorld, World, WorldName, Worlds};
@@ -556,3 +559,76 @@ fn test_afk_pool() {
556559
);
557560
assert_eq!(loops_done, 1);
558561
}
562+
563+
#[test]
564+
fn test_swimming() {
565+
let mut app = make_test_app();
566+
let world_lock = insert_overworld(&mut app);
567+
let mut partial_world = PartialWorld::default();
568+
569+
partial_world.chunks.set(
570+
&ChunkPos { x: 0, z: 0 },
571+
Some(Chunk::default()),
572+
&mut world_lock.write().chunks,
573+
);
574+
575+
// Set water blocks at (0, 64, 0) and (0, 65, 0)
576+
let water = BlockState::from(azalea_block::blocks::Water {
577+
level: WaterLevel::from(to_or_from_legacy_fluid_level(8) as BlockStateIntegerRepr),
578+
});
579+
world_lock
580+
.write()
581+
.chunks
582+
.set_block_state(BlockPos { x: 0, y: 64, z: 0 }, water);
583+
world_lock
584+
.write()
585+
.chunks
586+
.set_block_state(BlockPos { x: 0, y: 65, z: 0 }, water);
587+
588+
let entity = app
589+
.world_mut()
590+
.spawn((
591+
EntityBundle::new(
592+
Uuid::nil(),
593+
Vec3 {
594+
x: 0.5,
595+
y: 64.0, // Eye at ~65.62, in water at y=65
596+
z: 0.5,
597+
},
598+
EntityKind::Player,
599+
WorldName::new("minecraft:overworld"),
600+
),
601+
MinecraftEntityId(0),
602+
LocalEntity,
603+
HasClientLoaded,
604+
Sprinting(false),
605+
Swimming(false),
606+
))
607+
.id();
608+
609+
// Run initial tick to set up physics
610+
app.world_mut().run_schedule(GameTick);
611+
app.update();
612+
613+
// Initially not swimming
614+
assert!(!app.world().get::<Swimming>(entity).unwrap().0);
615+
// Should be touching water
616+
assert!(
617+
app.world()
618+
.get::<Physics>(entity)
619+
.unwrap()
620+
.was_touching_water
621+
);
622+
623+
// Set sprinting, should start swimming
624+
*app.world_mut().get_mut::<Sprinting>(entity).unwrap() = Sprinting(true);
625+
app.world_mut().run_schedule(GameTick);
626+
app.update();
627+
assert!(app.world().get::<Swimming>(entity).unwrap().0);
628+
629+
// Stop sprinting, should stop swimming
630+
*app.world_mut().get_mut::<Sprinting>(entity).unwrap() = Sprinting(false);
631+
app.world_mut().run_schedule(GameTick);
632+
app.update();
633+
assert!(!app.world().get::<Swimming>(entity).unwrap().0);
634+
}

0 commit comments

Comments
 (0)