1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 06:16:04 +00:00

start adding clip

This commit is contained in:
mat 2023-03-11 17:11:35 -06:00
parent e1f04989fb
commit b70d9aa489
5 changed files with 105 additions and 5 deletions

View file

@ -8,3 +8,15 @@ pub struct BlockHitResult {
pub miss: bool,
pub inside: bool,
}
impl BlockHitResult {
pub fn miss(location: Vec3, direction: Direction, block_pos: BlockPos) -> Self {
Self {
location,
direction,
block_pos,
miss: true,
inside: false,
}
}
}

View file

@ -1,5 +1,7 @@
use azalea_buf::McBuf;
use crate::Vec3;
#[derive(Clone, Copy, Debug, McBuf, Default)]
pub enum Direction {
#[default]
@ -11,6 +13,43 @@ pub enum Direction {
East,
}
impl Direction {
pub fn nearest(vec: Vec3) -> Direction {
let mut best_direction = Direction::North;
let mut best_direction_amount = 0.0;
for dir in [
Direction::Down,
Direction::Up,
Direction::North,
Direction::South,
Direction::West,
Direction::East,
]
.iter()
{
let amount = dir.normal().dot(vec);
if amount > best_direction_amount {
best_direction = *dir;
best_direction_amount = amount;
}
}
best_direction
}
pub fn normal(self) -> Vec3 {
match self {
Direction::Down => Vec3::new(0.0, -1.0, 0.0),
Direction::Up => Vec3::new(0.0, 1.0, 0.0),
Direction::North => Vec3::new(0.0, 0.0, -1.0),
Direction::South => Vec3::new(0.0, 0.0, 1.0),
Direction::West => Vec3::new(-1.0, 0.0, 0.0),
Direction::East => Vec3::new(1.0, 0.0, 0.0),
}
}
}
// TODO: make azalea_block use this instead of FacingCardinal
#[derive(Clone, Copy, Debug, McBuf)]
pub enum CardinalDirection {

View file

@ -36,6 +36,10 @@ macro_rules! vec3_impl {
z: self.z,
}
}
pub fn dot(&self, other: Self) -> $type {
self.x * other.x + self.y * other.y + self.z * other.z
}
}
impl Add for &$name {

View file

@ -1,11 +1,52 @@
use azalea_core::{lerp, BlockPos, Vec3, EPSILON};
use azalea_core::{lerp, BlockHitResult, BlockPos, Direction, Vec3, EPSILON};
use azalea_world::ChunkStorage;
pub struct ClipContext {
pub from: Vec3,
pub to: Vec3,
pub block: BlockClipContext,
pub fluid: FluidClipContext,
pub collision_context: CollisionContext,
}
pub struct BlockClipContext {
pub shape_getter
}
pub struct FluidClipContext {}
pub struct CollisionContext {}
pub fn clip(chunk_storage: &mut ChunkStorage, context: ClipContext) -> BlockHitResult {
traverse_blocks(
context.from,
context.to,
context,
|context, block_pos| {
let block_state = chunk_storage.get_block_state(block_pos);
// TODO: add fluid stuff to this (see getFluidState in vanilla source)
let block_shape = context.block_shape(block_state, chunk_storage, block_pos);
let block_hit_result =
chunk_storage.clip_with_interaction_override(context.from, context.to, block_pos);
// let block_distance =
None
},
|context| {
let vec = context.from - context.to;
BlockHitResult::miss(
context.to,
Direction::nearest(vec),
BlockPos::from(context.to),
)
},
)
}
pub fn traverse_blocks<C, T>(
from: Vec3,
to: Vec3,
context: C,
get_hit_result: fn(&C, &BlockPos) -> Option<T>,
get_miss_result: fn(&C) -> T,
get_hit_result: impl Fn(&C, &BlockPos) -> Option<T>,
get_miss_result: impl Fn(&C) -> T,
) -> T {
if from == to {
return get_miss_result(&context);

View file

@ -21,9 +21,13 @@ azalea-brigadier = { path = "../azalea-brigadier", version = "^0.6.0", features
] }
azalea-buf = { path = "../azalea-buf", version = "^0.6.0" }
azalea-chat = { path = "../azalea-chat", version = "^0.6.0" }
azalea-core = { path = "../azalea-core", optional = true, version = "^0.6.0", features = ["serde"]}
azalea-core = { path = "../azalea-core", optional = true, version = "^0.6.0", features = [
"serde",
] }
azalea-crypto = { path = "../azalea-crypto", version = "^0.6.0" }
azalea-nbt = { path = "../azalea-nbt", version = "^0.6.0", features = ["serde"] }
azalea-nbt = { path = "../azalea-nbt", version = "^0.6.0", features = [
"serde",
] }
azalea-protocol-macros = { path = "./azalea-protocol-macros", version = "^0.6.0" }
azalea-registry = { path = "../azalea-registry", version = "^0.6.0" }
azalea-world = { path = "../azalea-world", version = "^0.6.0" }