sight
permet à un objet de repérer d'autres objet
parameters
| Parameter | Default Value | Type | Description |
|---|---|---|---|
sight | number | 600 | |
raycast | boolean | true | |
tag | string | "collectible" | |
pos | vec2 | vec2(0, 0) |
example
sight({ sight: 600, raycast: true, tag: "collectible", pos: vec2(0, 0)}),function sight(p) { const param = { sight: 600, raycast: true, tag: "collectible", pos: vec2(0, 0), ...p, } return { id: "sight", getClosestTarget(pr) { const targetParam = { ...param, ...pr } const targets = get(targetParam.tag, { recursive: true }) const raycastCheck = (target) => { this.area.scale = vec2(0, 0) const r = this.getLevel().raycast(this.pos.add(targetParam.pos), target.pos.sub(this.pos)) if (debug.inspect) drawLine({ p1: this.pos.add(targetParam.pos), p2: target.pos, width: 4, color: rgb(0, 0, 255), }); this.area.scale = vec2(1, 1) return (r != undefined && r.object && r.object.id == target.id) } let closestTarget, lastDistance for (const target of targets) { const distance = this.pos.dist(target.pos) const raycast = (targetParam.raycast) ? raycastCheck(target) : true const inreach = (distance < targetParam.sight) const closest = (lastDistance == null || lastDistance > distance) const notself = target != this const areaOn = !target.collisionIgnore.includes("*") if (notself && raycast && inreach && closest && areaOn) { lastDistance = distance closestTarget = target } } return closestTarget }, }}