Files

54 lines
1.8 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# npc.js — startLoop() (satır 374)
[← npc.js](index.md) · [← Master](../../wiki.md)
Parça 1/3 · [sonraki](startLoop-2.md)
```javascript
function startLoop(c)
{
// Zaten çalışan bir döngü varsa yenisini başlatma (hız stacklenmesini önler).
if (mappet.get("hector_loop_running")) return;
mappet.set("hector_loop_running", true);
var wander = {}; // uuid -> {x,z} dolaşma hedefi (closure'da kalıcı)
function loop(ctx)
{
// Sistem kapatıldıysa döngüyü temiz şekilde bitir.
if (!mappet.get("hector_follow_active"))
{
mappet.set("hector_loop_running", false);
return;
}
var mode = mappet.get("hector_mode");
if (!mode) mode = "follow";
var guards = ctx.getServer().getEntities("@e[type=minecraft:armor_stand,name=" + GUARD_NAME + "]");
// --- SERBEST DOLAŞMA (hedef seç -> oraya yürü -> yeni hedef) ---
if (mode === "wander")
{
var anchor = ctx.getServer().getPlayer(TARGET_NAME);
for (var w = 0; w < guards.size(); w++)
{
var gw = guards.get(w);
if (!gw) continue;
var id = gw.getUniqueId();
var p = gw.getPosition();
var st = wander[id];
// hedef yoksa ya da hedefe vardıysa yeni rasgele hedef seç
if (!st || (Math.abs(p.x - st.x) < 1.5 && Math.abs(p.z - st.z) < 1.5))
{
var ax = anchor ? anchor.getPosition().x : p.x;
var az = anchor ? anchor.getPosition().z : p.z;
st = {
x: ax + (Math.random() - 0.5) * 2 * WANDER_RADIUS,
z: az + (Math.random() - 0.5) * 2 * WANDER_RADIUS
```