61 lines
No EOL
3.2 KiB
HTML
61 lines
No EOL
3.2 KiB
HTML
<!doctype html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<title>clamp — Ludic</title>
|
||
<meta name="description" content="Constrain a value to the inclusive range [lo, hi].">
|
||
<link rel="stylesheet" href="base.css">
|
||
<link rel="stylesheet" href="docs.css">
|
||
</head>
|
||
<body>
|
||
<header class="nav"><div class="wrap nav-in"><a class="brand" href="index.html"><span class="logo">L</span> Ludic</a><button class="nav-toggle" aria-label="Toggle menu" aria-expanded="false">☰</button><nav class="nav-links"><a href="index.html">Home</a><a href="api.html">API Reference</a><a class="nav-cta" href="https://git.workshopsoft.io/workshopsoft/ludic">Source ↗</a></nav></div></header>
|
||
<main class="wrap item">
|
||
<div class="crumbs"><a href="api.html">API Reference</a> <span>›</span> <a href="api.html#builtins">Builtin functions</a> <span>›</span> <span class="here">clamp</span></div>
|
||
<div class="item-head">
|
||
<span class="kind-badge kind-builtin">function</span>
|
||
<h1 id="top">clamp</h1>
|
||
</div>
|
||
<code class="sig">clamp(v, lo, hi) -> int</code>
|
||
<div class="desc"><p>Constrains a value to a range: it returns <code>lo</code> if <code>v</code> is below the range, <code>hi</code> if it is above, and <code>v</code> unchanged when it already lies within <code>[lo, hi]</code>. The classic use is keeping a moving object on screen or inside a grid, so its position can never run past the walls no matter how fast it moves. It saves you writing a pair of <code>if</code> checks by hand. Make sure <code>lo</code> is not greater than <code>hi</code>.</p></div>
|
||
<div class="params"><h2>Parameters</h2><div class="param" id="param-v"><code class="pname">v</code><span class="pdesc">the value to constrain</span></div><div class="param" id="param-lo"><code class="pname">lo</code><span class="pdesc">the lowest allowed value (inclusive)</span></div><div class="param" id="param-hi"><code class="pname">hi</code><span class="pdesc">the highest allowed value (inclusive)</span></div></div>
|
||
<div class="examples"><h2>Example</h2><pre data-lang="ludic">program ClampToGrid {
|
||
property Position { column: int = 0, row: int = 0 }
|
||
model Player { Position }
|
||
|
||
const GRID_WIDTH: int = 20
|
||
const GRID_HEIGHT: int = 15
|
||
const TILE_SIZE: int = 16
|
||
|
||
handler SpawnPlayer phase Start {
|
||
spawn Player { Position { column: 5, row: 5 } }
|
||
}
|
||
|
||
handler ReadInput phase Input {
|
||
let pressed = Input.key()
|
||
for (Position) in query [Position, {Player}] {
|
||
if pressed == 'd' { Position.column = Position.column + 1 }
|
||
if pressed == 'a' { Position.column = Position.column - 1 }
|
||
Position.column = clamp(Position.column, 0, GRID_WIDTH - 1)
|
||
}
|
||
}
|
||
|
||
handler DrawWorld phase Render {
|
||
Screen.clear(Color.MidnightBlue)
|
||
for (Position) in query [Position, {Player}] {
|
||
Screen.fill_rectangle(
|
||
x: Position.column * TILE_SIZE,
|
||
y: Position.row * TILE_SIZE,
|
||
width: TILE_SIZE,
|
||
height: TILE_SIZE,
|
||
color: Color.LimeGreen)
|
||
}
|
||
Screen.show()
|
||
}
|
||
}</pre></div>
|
||
|
||
<a class="back" href="api.html">← All symbols</a>
|
||
</main>
|
||
<script src="ludic-highlight.js"></script>
|
||
<script>Ludic.highlightAll(); Ludic.installCards(); Ludic.flashTarget();</script>
|
||
</body></html> |