Files
ngzz-mc/internal/raycast/raycast_test.go

61 lines
1.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package raycast
import "testing"
// solidAt 只在指定方块返回 solid。
func solidAt(sx, sy, sz int32) Solid {
return func(x, y, z int32) bool { return x == sx && y == sy && z == sz }
}
// TestCastDown 垂直向下命中顶面:face=(0,1,0),prev 在方块上方。
func TestCastDown(t *testing.T) {
h, ok := Cast([3]float64{0.5, 70, 0.5}, [3]float64{0, -1, 0}, 100, solidAt(0, 64, 0))
if !ok {
t.Fatal("应命中方块")
}
if h.X != 0 || h.Y != 64 || h.Z != 0 {
t.Fatalf("命中坐标异常: %+v", h)
}
if h.Face != [3]int32{0, 1, 0} {
t.Fatalf("面法线期望 (0,1,0),实际 %+v", h.Face)
}
if h.Prev != [3]int32{0, 65, 0} {
t.Fatalf("放置位期望 (0,65,0),实际 %+v", h.Prev)
}
}
// TestCastMiss 未命中(距离超限)。
func TestCastMiss(t *testing.T) {
if _, ok := Cast([3]float64{0.5, 70, 0.5}, [3]float64{1, 0, 0}, 3, solidAt(100, 70, 100)); ok {
t.Fatal("不应命中")
}
}
// TestCastDiagonal 沿 +z 直线命中方块北面(face=(0,0,-1))。
func TestCastDiagonal(t *testing.T) {
h, ok := Cast([3]float64{1.0, 0.5, -5}, [3]float64{0, 0, 1}, 20, solidAt(1, 0, 1))
if !ok {
t.Fatal("应命中")
}
if h.X != 1 || h.Z != 1 {
t.Fatalf("命中坐标异常: %+v", h)
}
if h.Face != [3]int32{0, 0, -1} {
t.Fatalf("面法线期望 (0,0,-1),实际 %+v", h.Face)
}
if h.Prev != [3]int32{1, 0, 0} {
t.Fatalf("放置位期望 (1,0,0),实际 %+v", h.Prev)
}
}
// TestCastNegative 负坐标区域命中(负坐标安全回归)。
func TestCastNegative(t *testing.T) {
h, ok := Cast([3]float64{-0.5, 70.5, -0.5}, [3]float64{0, -1, 0}, 100, solidAt(-1, 64, -1))
if !ok {
t.Fatal("负坐标应命中")
}
if h.X != -1 || h.Z != -1 {
t.Fatalf("负坐标命中异常: %+v", h)
}
}