42 lines
No EOL
3 KiB
HTML
42 lines
No EOL
3 KiB
HTML
<!doctype html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<title>Query.nearest — Ludic</title>
|
||
<meta name="description" content="The bearer of prop closest to a point, by squared distance.">
|
||
<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="ns-query.html">Query</a> <span>›</span> <span class="here">Query.nearest</span></div>
|
||
<div class="item-head">
|
||
<span class="kind-badge kind-method">method</span>
|
||
<h1 id="top">Query.nearest</h1>
|
||
</div>
|
||
<code class="sig">Query.nearest(prop, pos, x_field, y_field, x, y) -> int</code>
|
||
<div class="desc"><p>Returns the live entity carrying <code>prop</code> whose position is closest to <code>(x, y)</code>, or <code>-1</code> if none. Position comes from property <code>pos</code> at its integer fields <code>x_field</code> and <code>y_field</code> — pass the same id for <code>prop</code> and <code>pos</code> to query the coordinate component itself, or a separate tag for <code>prop</code> to mean "nearest <em>Enemy</em>". Distance is compared by exact squared integer distance (no square root), and ties go to the lower entity id, so the result is fully deterministic. The go-to for target acquisition, "interact with the closest thing", or nearest-neighbour AI.</p></div>
|
||
<div class="params"><h2>Parameters</h2><div class="param" id="param-prop"><code class="pname">prop</code><span class="pdesc">the property that filters which entities are considered</span></div><div class="param" id="param-pos"><code class="pname">pos</code><span class="pdesc">the property holding the coordinates</span></div></div>
|
||
<div class="examples"><h2>Example</h2><pre data-lang="ludic">program Demo {
|
||
property Position { col: int = 0, row: int = 0 }
|
||
property Enemy { hp: int = 0 }
|
||
model Slime { Position, Enemy }
|
||
entry {
|
||
spawn Slime { Position { col: 2, row: 0 }, Enemy { hp: 3 } }
|
||
spawn Slime { Position { col: 9, row: 0 }, Enemy { hp: 3 } }
|
||
let E = World.prop_id("Enemy")
|
||
let P = World.prop_id("Position")
|
||
let cx = World.field_id(P, "col")
|
||
let cy = World.field_id(P, "row")
|
||
let e = Query.nearest(E, P, cx, cy, 0, 0) # the enemy at col 2
|
||
print(World.get(e, P, cx)) # 2
|
||
}
|
||
}</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> |