followTarget
Permet à un objet de suivre un autre objet
require
sight()parameters
| Parameter | Default Value | Type | Description |
|---|---|---|---|
speed | number | 280 | |
stopDistance | number | 0 | |
retreat | boolean | false | |
tag | string | "player" | |
uTurnDelay | number | 0.2 | |
loosingSight | number | 400 | |
jumpForce | number | 800 | |
jumping | boolean | true | |
sight | number | 180 |
example
followTarget({ speed: 280, stopDistance: 0, retreat: false, tag: "player", uTurnDelay: 0.2, loosingSight: 400, jumpForce: 800, jumping: true, sight: 180}),function followTarget(p) { const param = { speed: 280, stopDistance: 0, retreat: false, tag: "player", uTurnDelay: 0.2, loosingSight: 400, jumpForce: 800, jumping: true, sight: 180, ...p } return { id: "followTarget", require: ["sight"], add() { let retreatPos let direction = vec2(0, 0) let target requestAnimationFrame(() => retreatPos = vec2(this.pos)) this.on("respawn", () => target = null) onUpdate(() => { const isOnSight = target != null && target.pos.dist(this.pos) < param.loosingSight const gravity = this.gravityScale != 0 if (!isOnSight) target = this.getClosestTarget(param) if (target) { const d = this.pos.dist(target.pos) let t if (d > param.stopDistance) t = target.pos.sub(this.pos) else t = vec2(0, 0) if (gravity && param.jumping && t.y < -1 && this.isGrounded()) this.jump(param.jumpForce) else if (param.retreat) { const d = this.pos.dist(retreatPos) if (d > param.stopDistance) t = retreatPos.sub(this.pos) else t = vec2(0, 0) } if (gravity) t.y = 0 tween(direction, t.unit(), param.uTurnDelay, (v) => (direction = v), easings.easeOutSin) this.trigger("facing", this.direction) this.move(direction.scale(param.speed)) if (direction.len() > 0.1) this.trigger("facing", direction.unit()) } }) }, }}