ludic/packages/ludic.aim/use.ludic

31 lines
1.4 KiB
Text

# use.ludic - the thing the use key should act on. The ray comes first and proximity is its fallback,
# never its override:
# under the crosshair and in reach -> that, whatever else is nearer
# under the crosshair and too far -> NOTHING (the prompt says why), within AIM_USE_MAX
# nothing of that kind aimed at -> the nearest thing in reach
# A player looking at something who presses the key has said which thing they mean.
export const AIM_USE_THIS: int = 0
export const AIM_USE_NOTHING: int = 1
export const AIM_USE_NEAREST: int = 2
export function aim_use(aim_st: AimState, kind: int) -> int {
if aim_st.am_kind == kind and aim_st.am_reach { return AIM_USE_THIS }
if aim_st.am_kind == kind and aim_st.am_dist < AIM_USE_MAX { return AIM_USE_NOTHING }
return AIM_USE_NEAREST
}
# whether (x, z) is before the view: within `cos_min` of the camera's heading, seen from the body.
# Anything at the feet counts, where a heading means nothing, and so does all of it when the view
# looks straight up or down
const AIM_FEET: float = 1.2
export function aim_ahead(x: float, z: float, cos_min: float) -> bool {
let dx = x - AimView.body_x()
let dz = z - AimView.body_z()
let d = Math.sqrt(dx * dx + dz * dz)
if d < AIM_FEET { return true }
let fx = AimView.fx()
let fz = AimView.fz()
let f = Math.sqrt(fx * fx + fz * fz)
if f < 0.0001 { return true }
return (dx * fx + dz * fz) / (d * f) >= cos_min
}