Projectiles ring bells they hit

This commit is contained in:
Kacpherek
2026-10-26 18:45:57 +01:00
parent 1a43fa5859
commit 472da6509d
4 changed files with 73 additions and 19 deletions
+24 -1
View File
@@ -20,7 +20,7 @@ use horizon_client::{
projectiles::{Hit, Projectile, ProjectileKind, ProjectileWorld},
};
use horizon_core::{
geometry::{Aabb, Vec3, traverse_blocks},
geometry::{Aabb, Direction, Vec3, traverse_blocks},
position::BlockPos,
random::RandomSource,
};
@@ -1877,6 +1877,22 @@ impl Game<'_> {
None => self.projectile_hit(p, hit),
}
}
/// `BellBlock.onProjectileHit`: a projectile that strikes the bell proper
/// rings it, and a player's shot counts as the player's ring.
fn bell_projectile_hit(&mut self, p: &Projectile, pos: [i32; 3], at: Vec3, face: [i32; 3]) {
let Some(face) = Direction::ALL.into_iter().find(|d| d.normal() == face) else {
return;
};
let block = BlockPos::new(pos[0], pos[1], pos[2]);
let rang = self
.with_level(|b, level, ticks| b.bell_projectile_hit(level, ticks, block, face, at.y));
if rang {
let _ = self.process_level_events();
if p.owner == Some(Who::Player) {
self.stats.custom("bell_ring", 1);
}
}
}
/// What an arrow's tick asked for: its particles and sounds.
fn arrow_event(&mut self, at: Vec3, event: horizon_client::arrows::ArrowEvent) {
use horizon_client::arrows::{ArrowEvent, ArrowParticle, ArrowSound, BlockReaction};
@@ -2002,6 +2018,13 @@ impl Game<'_> {
}
}
pub(super) fn projectile_hit(&mut self, p: &Projectile, hit: Hit) -> Result<()> {
// Projectile.onHitBlock: BlockState.onProjectileHit, for every
// projectile (a wind charge out of the world's top has no face).
if let Hit::Block { pos, at, face } = hit
&& face != [0; 3]
{
self.bell_projectile_hit(p, pos, at, face);
}
self.combat.projectile_owner = match p.owner {
Some(Who::Mob(id)) => Some(id),
_ => None,
+3 -16
View File
@@ -1,11 +1,9 @@
//! `BlockBehaviour.useWithoutItem` (right-click with an empty hand or an item
//! that does not handle the block itself).
use super::{
Behaviours, LevelAccess, LevelEvent, Sim, Ticks, classes::Class, query::axis_of, update::below,
};
use super::{Behaviours, LevelAccess, LevelEvent, Sim, Ticks, classes::Class, update::below};
use crate::registry::BlockStateId;
use horizon_core::{
geometry::{Axis, Direction, Vec3},
geometry::{Direction, Vec3},
position::BlockPos,
random::RandomSource,
};
@@ -338,18 +336,7 @@ impl<L: LevelAccess + ?Sized> Sim<'_, L> {
}
}
Class::Bell => {
let click_y = hit.y - f64::from(pos.y);
let proper = axis_of(face) != Axis::Y && click_y <= 0.8124 && {
let facing_axis = axis_of(b.facing(state));
match table.text(state, p.attachment) {
"floor" => facing_axis == axis_of(face),
"single_wall" | "double_wall" => facing_axis != axis_of(face),
"ceiling" => true,
_ => false,
}
};
if proper {
self.bell_ring(pos, state, face);
if self.bell_hit(pos, state, face, hit.y - f64::from(pos.y)) {
Success
} else {
Pass
+16
View File
@@ -768,6 +768,22 @@ impl Behaviours {
self.sim(level, ticks)
.big_dripleaf_entity_inside(pos, on_ground, feet_y);
}
/// `BellBlock.onProjectileHit` for a projectile that hit `face` of the
/// block at `pos` at height `hit_y`. False when the block is not a bell
/// or the hit missed the bell proper.
pub fn bell_projectile_hit<L: LevelAccess + ?Sized>(
&self,
level: &mut L,
ticks: &mut Ticks,
pos: BlockPos,
face: Direction,
hit_y: f64,
) -> bool {
let mut sim = self.sim(level, ticks);
let state = sim.state(pos);
self.data(state).is(classes::Class::Bell)
&& sim.bell_hit(pos, state, face, hit_y - f64::from(pos.y))
}
/// Redstone signal queries (`SignalGetter`).
pub fn has_neighbor_signal<L: LevelAccess + ?Sized>(
&self,
+30 -2
View File
@@ -5,13 +5,16 @@ use super::{
LevelAccess, LevelEvent, Sim,
classes::Class,
data::state_flags,
query::{clockwise, counter_clockwise},
query::{axis_of, clockwise, counter_clockwise},
states::BlockId,
update::{HORIZONTAL, above, below, rel},
};
use crate::registry::BlockStateId;
use horizon_core::{
geometry::Direction, position::BlockPos, random::RandomSource, ticks::TickPriority,
geometry::{Axis, Direction},
position::BlockPos,
random::RandomSource,
ticks::TickPriority,
};
const SIGNAL_ORDER: [Direction; 6] = [
@@ -1159,6 +1162,31 @@ impl<L: LevelAccess + ?Sized> Sim<'_, L> {
self.set_block_and_update(pos, b.table.set_bool(state, b.props.powered, signal));
}
}
/// `BellBlock.onHit` with `requireHitFromCorrectSide`: a hit on `face` at
/// height `click_y` in the block rings the bell only when it struck the
/// bell proper. False when the hit missed it.
pub(crate) fn bell_hit(
&mut self,
pos: BlockPos,
state: BlockStateId,
face: Direction,
click_y: f64,
) -> bool {
let b = self.b;
let facing_axis = axis_of(b.facing(state));
let proper = axis_of(face) != Axis::Y
&& click_y <= f64::from(0.8124f32)
&& match b.table.text(state, b.props.attachment) {
"floor" => facing_axis == axis_of(face),
"single_wall" | "double_wall" => facing_axis != axis_of(face),
"ceiling" => true,
_ => false,
};
if proper {
self.bell_ring(pos, state, face);
}
proper
}
/// `BellBlock.attemptToRing`: `BellBlockEntity.onHit` queues block event 1
/// with the struck direction, then the sound and game event.
pub(crate) fn bell_ring(&mut self, pos: BlockPos, state: BlockStateId, direction: Direction) {