Files

117 lines
4.8 KiB
JavaScript
Raw Permalink 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.
// node test/logic.test.mjs —— 不需要浏览器/GPU:世界生成、网格数据、物理、射线
import { World, CY, CX, CZ } from '../js/world.js'
import { createPlayer, stepPlayer, raycast, dirFromAngles, canPlaceAt } from '../js/player.js'
import { BLOCKS, HOTBAR, tileUV, isOpaque } from '../js/blocks.js'
let pass = 0, fail = 0
const ok = (cond, label, extra = '') => {
if (cond) { pass++; console.log('✓', label) }
else { fail++; console.log('✗', label, extra) }
}
const world = new World(12345)
// 1. 地形
const h1 = world.heightAt(0, 0)
ok(h1 === world.heightAt(0, 0), `heightAt 确定性 (${h1})`)
const heights = []
for (let i = 0; i < 200; i++) heights.push(world.heightAt(i * 3, i * 7))
ok(new Set(heights).size > 3, `地形有起伏(${Math.min(...heights)}–${Math.max(...heights)})`)
ok(Math.min(...heights) >= 3 && Math.max(...heights) <= CY - 12, '地形高度在范围内')
// 2. 方块读写
world.set(5, 30, 5, 10)
ok(world.get(5, 30, 5) === 10, 'set/get 砖块')
world.set(5, 30, 5, 0)
ok(world.get(5, 30, 5) === 0, '挖掉后是空气')
ok(world.get(5, 40, 5) === 0, '高处是空气')
ok(world.get(5, 0, 5) === 11, 'y=0 是基岩')
// 3. 网格
const c = world.chunkAt(0, 0)
world.generate(c)
const { solid, water } = world.buildMesh(c)
ok(solid.pos.length > 0, `顶点数 ${solid.pos.length / 3}`)
ok(solid.pos.length / 3 === solid.uv.length / 2, 'position/uv 数量一致')
ok(solid.idx.length % 6 === 0, `三角形数 ${solid.idx.length / 6}`)
ok(solid.idx.every((i) => i >= 0 && i < solid.pos.length / 3), '索引不越界')
ok(solid.uv.every((v) => v >= 0 && v <= 1), 'UV 在 0–1')
// 4. 三角形朝外:先清空整块,只放一个方块,法线应与「方块中心→三角重心」同向
const lone = new World(1)
for (let y = 0; y < CY; y++) for (let z = 0; z < CZ; z++) for (let x = 0; x < CX; x++) lone.set(x, y, z, 0)
lone.set(4, 30, 4, 3)
const loneMesh = lone.buildMesh(lone.chunkAt(0, 0))
ok(loneMesh.solid.idx.length / 3 === 12, `孤立方块 12 个三角形(实际 ${loneMesh.solid.idx.length / 3})`)
{
const p = loneMesh.solid.pos, ix = loneMesh.solid.idx
const cx0 = 4.5, cy0 = 30.5, cz0 = 4.5
let outward = 0, inward = 0
for (let t = 0; t < ix.length; t += 3) {
const a = ix[t], b = ix[t + 1], d = ix[t + 2]
const ax = p[a * 3], ay = p[a * 3 + 1], az = p[a * 3 + 2]
const e1 = [p[b * 3] - ax, p[b * 3 + 1] - ay, p[b * 3 + 2] - az]
const e2 = [p[d * 3] - ax, p[d * 3 + 1] - ay, p[d * 3 + 2] - az]
const n = [e1[1] * e2[2] - e1[2] * e2[1], e1[2] * e2[0] - e1[0] * e2[2], e1[0] * e2[1] - e1[1] * e2[0]]
const gx = (ax + p[b * 3] + p[d * 3]) / 3 - cx0
const gy = (ay + p[b * 3 + 1] + p[d * 3 + 1]) / 3 - cy0
const gz = (az + p[b * 3 + 2] + p[d * 3 + 2]) / 3 - cz0
const dot = n[0] * gx + n[1] * gy + n[2] * gz
if (dot > 0) outward++
else inward++
}
ok(inward === 0 && outward === 12, `三角形全部朝外(外 ${outward} / 内 ${inward})`)
}
// 5. 明暗 0–1
ok(solid.col.every((v) => v > 0 && v <= 1.001), '明暗系数在 0–1')
// 7. 物理:下落
const w2 = new World(999)
const gy = w2.heightAt(10, 10)
const P = createPlayer({ x: 10.5, y: gy + 8, z: 10.5 })
for (let i = 0; i < 300; i++) stepPlayer(w2, P, { fwd: 0, side: 0, jump: false }, 1 / 60)
ok(Math.abs(P.pos.y - (gy + 1)) < 0.2, `落地在 y≈${gy + 1}(实际 ${P.pos.y.toFixed(2)})`)
// 8. 物理:撞墙
const wall = new World(7)
wall.set(12, 30, 10, 3)
wall.set(12, 31, 10, 3)
const Q = createPlayer({ x: 10.5, y: 31.2, z: 10.5 })
Q.fly = true
Q.yaw = -Math.PI / 2 // 面朝 +x
for (let i = 0; i < 180; i++) stepPlayer(wall, Q, { fwd: 1, side: 0 })
ok(Q.pos.x < 12, `撞墙被挡住(x=${Q.pos.x.toFixed(2)} < 12)`)
// 9. 射线
const eye = { x: 10.5, y: gy + 3, z: 10.5 }
const hit = raycast(w2, eye, dirFromAngles(-Math.PI / 2, 0))
ok(hit && hit.y === gy, `向下射线命中地面 y=${hit?.y}`)
ok(hit && hit.prev, '记录命中前的空气格')
// 10. 放方块
const P2 = createPlayer({ x: 10.5, y: gy + 1, z: 10.5 })
ok(canPlaceAt(w2, P2, 10, gy + 1, 10) === false, '不能把方块放进自己身体里')
ok(canPlaceAt(w2, P2, 14, gy + 1, 14) === true, '旁边空地可以放')
// 11. 方块表
ok(Object.keys(BLOCKS).length >= 14, `方块种类 ${Object.keys(BLOCKS).length}`)
ok(HOTBAR.every((id) => BLOCKS[id]), '热键栏方块都存在')
ok(tileUV('stone').u0 < tileUV('stone').u1, 'UV 方向正确')
ok(isOpaque(3) && !isOpaque(9) && !isOpaque(5), '玻璃/水不遮挡')
// 12. 存档往返
const edits = [...world.edits.entries()]
const w3 = new World(world.seed)
for (const [k, v] of edits) w3.edits.set(k, v)
w3.applyEdits()
let restored = 0
for (const [k, v] of edits) {
const [x, y, z] = k.split(',').map(Number)
if (w3.get(x, y, z) === v) restored++
}
ok(restored === edits.length, `存档恢复 ${restored}/${edits.length} 个改动`)
console.log(`\n${pass} 通过 / ${fail} 失败`)
process.exit(fail ? 1 : 0)