Files

131 lines
4.0 KiB
JavaScript

// 玩家物理与交互(纯逻辑,不依赖 three / DOM,方便在 node 里测试)
export const PLAYER_W = 0.6
export const PLAYER_H = 1.78
export const REACH = 6
export function createPlayer(spawn = { x: 8, y: 40, z: 8 }) {
return {
pos: { x: spawn.x, y: spawn.y, z: spawn.z },
vel: { x: 0, y: 0, z: 0 },
yaw: 0,
pitch: 0,
onGround: false,
fly: false,
walkPhase: 0,
}
}
const isFree = (id) => id === 0 || id === 5
export function solidAt(world, x, y, z) {
return !isFree(world.get(Math.floor(x), Math.floor(y), Math.floor(z)))
}
export function collides(world, p) {
const r = PLAYER_W / 2 - 0.001
const ys = [p.y + 0.001, p.y + PLAYER_H * 0.5, p.y + PLAYER_H - 0.001]
const xs = [p.x - r, p.x + r]
const zs = [p.z - r, p.z + r]
for (const y of ys) for (const x of xs) for (const z of zs) if (solidAt(world, x, y, z)) return true
return false
}
// 单轴推进,遇到固体就贴住
function moveAxis(world, P, axis, delta) {
if (!delta) return false
const steps = Math.max(1, Math.ceil(Math.abs(delta) / 0.05))
const unit = delta / steps
for (let i = 0; i < steps; i++) {
const next = { x: P.pos.x, y: P.pos.y, z: P.pos.z }
next[axis] += unit
if (collides(world, next)) {
if (axis === 'y') {
if (unit < 0) P.onGround = true
P.vel.y = 0
} else {
P.vel[axis] = 0
}
return true
}
P.pos = next
}
return false
}
const GRAVITY = 26
const JUMP = 8.4
export const SPEED = 4.6
export const SPRINT_SPEED = 7.4
export const FLY_SPEED = 9
/**
* input: { fwd, side, jump, sprint, flyUp, flyDown }
*/
export function stepPlayer(world, P, input, dt) {
const sin = Math.sin(P.yaw), cos = Math.cos(P.yaw)
const fwd = Math.max(-1, Math.min(1, input.fwd || 0))
const side = Math.max(-1, Math.min(1, input.side || 0))
let mx = -sin * fwd + cos * side
let mz = -cos * fwd - sin * side
const len = Math.hypot(mx, mz)
if (len > 1) { mx /= len; mz /= len }
const speed = P.fly ? FLY_SPEED : input.sprint ? SPRINT_SPEED : SPEED
if (P.fly) {
P.vel.x = mx * speed
P.vel.z = mz * speed
P.vel.y = ((input.flyUp ? 1 : 0) - (input.flyDown ? 1 : 0)) * speed * 0.8
P.onGround = false
} else {
const accel = P.onGround ? 12 : 4
const k = Math.min(1, accel * dt)
P.vel.x += (mx * speed - P.vel.x) * k
P.vel.z += (mz * speed - P.vel.z) * k
P.vel.y -= GRAVITY * dt
if (input.jump && P.onGround) { P.vel.y = JUMP; P.onGround = false }
P.onGround = false
}
const wasGround = P.onGround
moveAxis(world, P, 'x', P.vel.x * dt)
moveAxis(world, P, 'z', P.vel.z * dt)
moveAxis(world, P, 'y', P.vel.y * dt)
const planar = Math.hypot(P.vel.x, P.vel.z)
if (planar > 0.6 && (P.onGround || P.fly)) P.walkPhase += dt * 9
if (P.fly && wasGround && Math.abs(P.vel.y) < 1e-6) P.onGround = false
return { moving: planar > 0.6, bob: Math.sin(P.walkPhase) * 0.045 }
}
/** 从 origin 沿 dir 找第一个非空气非水方块;返回 {x,y,z,id,prev} */
export function raycast(world, origin, dir, reach = REACH) {
let last = null
for (let t = 0; t <= reach; t += 0.04) {
const x = Math.floor(origin.x + dir.x * t)
const y = Math.floor(origin.y + dir.y * t)
const z = Math.floor(origin.z + dir.z * t)
if (!last || last.x !== x || last.y !== y || last.z !== z) {
const id = world.get(x, y, z)
if (id && id !== 5) return { x, y, z, id, prev: last }
last = { x, y, z }
}
}
return null
}
export function dirFromAngles(pitch, yaw) {
const cp = Math.cos(pitch)
return { x: -Math.sin(yaw) * cp, y: Math.sin(pitch), z: -Math.cos(yaw) * cp }
}
/** 能否在这里放方块(不与自己重叠) */
export function canPlaceAt(world, P, x, y, z) {
if (world.get(x, y, z)) return false
const r = PLAYER_W / 2
const overlapX = x + 1 > P.pos.x - r && x < P.pos.x + r
const overlapZ = z + 1 > P.pos.z - r && z < P.pos.z + r
const overlapY = y + 1 > P.pos.y && y < P.pos.y + PLAYER_H
return !(overlapX && overlapZ && overlapY)
}