ludic/packages/ludic.character/sit.ludic
2026-09-25 14:02:21 +03:00

59 lines
2.9 KiB
Text

# sit.ludic - sitting: on a seat's point, or on the ground where the body stands. The pose either
# side is a still one, so the sit's own weight (body_sit_t) eases 0 -> 1 over a quarter second and
# the body walks the last step to the seat over the same time rather than jumping there.
export function char_sit(character_st: mut CharacterState, x: float, z: float, yaw: float, mode: int) -> void {
character_st.chr_sit = true
character_st.chr_sit_mode = mode
character_st.chr_speed = 0.0
character_st.chr_move = 0.0
character_st.chr_run = 0.0
character_st.chr_sit_x = x
character_st.chr_sit_z = z
character_st.chr_sit_yaw = yaw
character_st.chr_water = CharacterGround.water(character_st.chr_x, character_st.chr_z)
}
function chr_sit_tick(character_st: mut CharacterState, dt: float) -> void {
var want = 0.0
if character_st.chr_sit { want = 1.0 }
character_st.chr_sit_t = char_toward(character_st.chr_sit_t, want, 4.0 * dt)
if not character_st.chr_sit { return }
let k = Math.min(1.0, 8.0 * dt)
character_st.chr_x = Math.lerp(character_st.chr_x, character_st.chr_sit_x, k)
character_st.chr_z = Math.lerp(character_st.chr_z, character_st.chr_sit_z, k)
character_st.chr_yaw = character_st.chr_yaw + char_wrap(character_st.chr_sit_yaw - character_st.chr_yaw) * k
character_st.chr_y = CharacterGround.height(character_st.chr_x, character_st.chr_z)
character_st.chr_water = CharacterGround.water(character_st.chr_x, character_st.chr_z)
}
# Sitting down where the body stands: not astride something, not in the air, not on a slope,
# not in the water. Asked again while sitting, it stands up. The answer is a CHAR_* for the game
# to word.
export function char_sit_here(character_st: mut CharacterState) -> int {
if character_st.chr_sit {
char_stand(character_st)
return CHAR_STOOD
}
if CharacterBody.riding() { return CHAR_NOT_RIDING }
if character_st.chr_air { return CHAR_NOT_AIRBORNE }
if CharacterGround.slope(character_st.chr_x, character_st.chr_z) > 0.35 { return CHAR_NOT_STEEP }
if character_st.chr_y < character_st.chr_water + 0.9 { return CHAR_NOT_WET }
char_sit(character_st, character_st.chr_x, character_st.chr_z, character_st.chr_yaw, CHAR_SIT_GROUND)
return CHAR_SAT
}
# standing up out of a seat put down against a boulder or a bank must not leave the body inside it
export function char_stand(character_st: mut CharacterState) -> void {
character_st.chr_sit = false
if char_safe_spot(character_st, character_st.chr_x, character_st.chr_z, character_st.chr_y, 0.0) {
character_st.chr_x = character_st.chr_safe_x
character_st.chr_z = character_st.chr_safe_z
character_st.chr_y = CharacterGround.height(character_st.chr_x, character_st.chr_z)
}
}
# smoothstep, so a pose slows into the seat rather than arriving at full speed
export function char_ease(t: float) -> float {
let c = Math.clamp(t, 0.0, 1.0)
return c * c * (3.0 - 2.0 * c)
}