ludic/docs/language/math/math-clamp.md

1.2 KiB

id name category kind tokens sig tip order ns member
math-clamp Math.clamp math namespace-method Math.clamp Math.clamp(v, lo, hi) -> int Constrain a value to the range [lo, hi]. 3 Math clamp

Returns v held inside the inclusive range lo..hi: below lo it returns lo, above hi it returns hi, otherwise v unchanged. It is the single-call form of Math.max(lo, Math.min(v, hi)) and works for int and fixed alike. Use it to keep a camera inside the world, a slider within its track, or a stat within its legal bounds. The bare form clamp(v, lo, hi) is kept as a convenience alias.

Parameters:

  • v — the value to constrain
  • lo — the lowest allowed result (inclusive)
  • hi — the highest allowed result (inclusive)
program CameraBounds {
  const WORLD_WIDTH: int = 320

  state CameraBoundsState {
    camera_x: int = 0
  }

  handler Follow(camera_bounds_st: mut CameraBoundsState) phase Update {
    camera_bounds_st.camera_x = Math.clamp(camera_bounds_st.camera_x, 0, WORLD_WIDTH - 1)
  }
}