Files
horizon-airdef/crates/horizon-world/src/level/creaking.rs
T
Ludovic Marie 2ad11eb68d Add the creaking and the creaking heart
A creaking heart in a pale oak trunk now follows its logs: placed or updated between two logs on its axis it wakes at night (12600 to 23400 in the overworld) or goes dormant by day, and stays uprooted without them. An awake heart with a player within 32 blocks spawns a linked creaking within 16 blocks, keeps its UUID in the saved block entity, reports the distance to it as a comparator signal, and lets it go when the day comes, it strays more than 34 blocks or a player stands inside it. The creaking freezes while any player looks at it, wakes when one looks from within 12 blocks, then hunts the nearest visible player and hits every 40 ticks. Bound to a heart it takes no damage; hits only make it sway and, when a player did it, make the heart send trail particles and sometimes spread resin clumps over nearby logs. Breaking the heart makes its creaking twitch to death, and removing the block any other way tears it down in a burst of crumbling pale oak. It has the vanilla model, animations (walk, attack, invulnerable, death), glowing eyes, sounds and NBT home_pos, and the trail particle exists now.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-09 13:15:05 +02:00

84 lines
3.2 KiB
Rust

//! The creaking heart block (`CreakingHeartBlock`): its state follows the
//! pale oak logs on both ends of its axis and the `creaking_active`
//! environment attribute. The block entity (protector, particles, resin)
//! lives with the entities in the client.
use super::{Behaviours, Dimension, LevelAccess, Sim, Ticks, update::rel};
use crate::registry::BlockStateId;
use horizon_core::{geometry::Direction, position::BlockPos};
/// `Direction.Axis.getDirections()`.
fn axis_directions(axis: &str) -> [Direction; 2] {
match axis {
"x" => [Direction::West, Direction::East],
"z" => [Direction::North, Direction::South],
_ => [Direction::Down, Direction::Up],
}
}
impl<L: LevelAccess + ?Sized> Sim<'_, L> {
/// `EnvironmentAttributes.CREAKING_ACTIVE`: the overworld day timeline
/// turns it on from 12600 to 23400.
pub(crate) fn creaking_active(&self) -> bool {
self.level.dimension() == Dimension::Overworld
&& (12600..23401).contains(&self.level.day_time().rem_euclid(24000))
}
/// `CreakingHeartBlock.hasRequiredLogs`: pale oak logs along the axis.
pub(crate) fn heart_has_required_logs(&self, state: BlockStateId, pos: BlockPos) -> bool {
let b = self.b;
let axis = b.table.text(state, b.props.axis);
axis_directions(axis).into_iter().all(|d| {
let neighbour = self.state(rel(pos, d));
b.in_set(neighbour, &b.tags.pale_oak_logs)
&& b.table.text(neighbour, b.props.axis) == axis
})
}
/// `CreakingHeartBlock.updateState`: an uprooted heart with its logs wakes
/// (at night) or goes dormant.
pub(crate) fn heart_update_state(&self, state: BlockStateId, pos: BlockPos) -> BlockStateId {
let b = self.b;
let uprooted = b.table.text(state, b.props.creaking_heart_state) == "uprooted";
if uprooted && self.heart_has_required_logs(state, pos) {
let next = if self.creaking_active() {
"awake"
} else {
"dormant"
};
b.table.set_text(state, b.props.creaking_heart_state, next)
} else {
state
}
}
/// `CreakingHeartBlock.tick` (scheduled by `updateShape`).
pub(crate) fn heart_tick(&mut self, state: BlockStateId, pos: BlockPos) {
let next = self.heart_update_state(state, pos);
if next != state {
self.set_block_and_update(pos, next);
}
}
/// `CreakingHeartBlock.getStateForPlacement` after the axis is set.
pub(crate) fn heart_placement(&self, state: BlockStateId, pos: BlockPos) -> BlockStateId {
self.heart_update_state(state, pos)
}
}
impl Behaviours {
/// `EnvironmentAttributes.CREAKING_ACTIVE` at the level's time.
pub fn creaking_active<L: LevelAccess + ?Sized>(
&self,
level: &mut L,
ticks: &mut Ticks,
) -> bool {
self.sim(level, ticks).creaking_active()
}
/// `CreakingHeartBlock.hasRequiredLogs`.
pub fn creaking_heart_has_logs<L: LevelAccess + ?Sized>(
&self,
level: &mut L,
ticks: &mut Ticks,
pos: BlockPos,
state: BlockStateId,
) -> bool {
self.sim(level, ticks).heart_has_required_logs(state, pos)
}
}