From 348c71b97b2dfc45d7412ec33f6131e20592bb14 Mon Sep 17 00:00:00 2001 From: mat <27899617+mat-1@users.noreply.github.com> Date: Sun, 10 Dec 2023 00:08:08 -0600 Subject: Climbing (#121) * start implementing climbing * fix tests * fix bots running at lower tick rate --- azalea-entity/src/plugin/mod.rs | 71 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) (limited to 'azalea-entity/src/plugin/mod.rs') diff --git a/azalea-entity/src/plugin/mod.rs b/azalea-entity/src/plugin/mod.rs index 9950e6ba..4b6d9979 100644 --- a/azalea-entity/src/plugin/mod.rs +++ b/azalea-entity/src/plugin/mod.rs @@ -3,6 +3,7 @@ mod relative_updates; use std::collections::HashSet; +use azalea_block::BlockState; use azalea_core::position::{BlockPos, ChunkPos, Vec3}; use azalea_world::{InstanceContainer, InstanceName, MinecraftEntityId}; use bevy_app::{App, Plugin, PreUpdate, Update}; @@ -11,7 +12,8 @@ use derive_more::{Deref, DerefMut}; use tracing::debug; use crate::{ - metadata::Health, Dead, EyeHeight, FluidOnEyes, LocalEntity, LookDirection, Physics, Position, + metadata::Health, Dead, EyeHeight, FluidOnEyes, LocalEntity, LookDirection, OnClimbable, + Physics, Position, }; use indexing::EntityUuidIndex; @@ -48,6 +50,7 @@ impl Plugin for EntityPlugin { add_dead, clamp_look_direction, update_fluid_on_eyes, + update_on_climbable, ), ), ) @@ -106,6 +109,72 @@ pub fn update_fluid_on_eyes( } } +pub fn update_on_climbable( + mut query: Query<(&mut OnClimbable, &Position, &InstanceName)>, + instance_container: Res, +) { + for (mut on_climbable, position, instance_name) in query.iter_mut() { + // TODO: there's currently no gamemode component that can be accessed from here, + // maybe LocalGameMode should be replaced with two components, maybe called + // EntityGameMode and PreviousGameMode? + + // if game_mode == GameMode::Spectator { + // continue; + // } + + let Some(instance) = instance_container.get(instance_name) else { + continue; + }; + + let instance = instance.read(); + + let block_pos = BlockPos::from(position); + let block_state_at_feet = instance.get_block_state(&block_pos).unwrap_or_default(); + let block_at_feet = Box::::from(block_state_at_feet); + let registry_block_at_feet = block_at_feet.as_registry_block(); + + **on_climbable = azalea_registry::tags::blocks::CLIMBABLE.contains(®istry_block_at_feet) + || (azalea_registry::tags::blocks::TRAPDOORS.contains(®istry_block_at_feet) + && is_trapdoor_useable_as_ladder(block_state_at_feet, block_pos, &instance)); + } +} + +fn is_trapdoor_useable_as_ladder( + block_state: BlockState, + block_pos: BlockPos, + instance: &azalea_world::Instance, +) -> bool { + // trapdoor must be open + if !block_state + .property::() + .unwrap_or_default() + { + return false; + } + + // block below must be a ladder + let block_below = instance + .get_block_state(&block_pos.down(1)) + .unwrap_or_default(); + let registry_block_below = + Box::::from(block_below).as_registry_block(); + if registry_block_below != azalea_registry::Block::Ladder { + return false; + } + // and the ladder must be facing the same direction as the trapdoor + let ladder_facing = block_below + .property::() + .expect("ladder block must have facing property"); + let trapdoor_facing = block_state + .property::() + .expect("trapdoor block must have facing property"); + if ladder_facing != trapdoor_facing { + return false; + } + + true +} + /// A component that lists all the local player entities that have this entity /// loaded. If this is empty, the entity will be removed from the ECS. #[derive(Component, Clone, Deref, DerefMut)] -- cgit v1.2.3