followTarget
Permet à un objet de suivre un autre objet
require
sight()triggers on self
"spotted"parameters
| Parameter | Default Value | Type | Description |
|---|---|---|---|
speed | number | 280 | |
stopDistance | number | 100 | |
retreat | boolean | false | |
tag | string | 'player' | |
uTurnDelay | number | 0.2 | |
jumpForce | number | 800 | |
jumping | boolean | true | |
chasingDelay | number | 0 | |
offScreenPaused | boolean | false |
example
followTarget({ speed: 280, stopDistance: 100, retreat: false, tag: 'player', uTurnDelay: 0.2, jumpForce: 800, jumping: true, chasingDelay: 0, offScreenPaused: false}),function followTarget(p) { const param = { speed: 280, stopDistance: 100, retreat: false, tag: 'player', uTurnDelay: 0.2, jumpForce: 800, jumping: true, chasingDelay: 0, offScreenPaused: false, ...p, }; return { id: 'followTarget', require: ['sight'], add() { const stopDistanceCheck = (t) => { return t.dist(this.pos) > param.stopDistance; }; const isAlive = (obj) => { return !obj.has('alive') || obj.isAlive == true; }; let chasing = false; let retreatPos; let direction = vec2(0, 0); let savedChasingTime = 0; requestAnimationFrame(() => (retreatPos = vec2(this.pos))); this.on('respawn', () => (this.currentTarget = null)); onUpdate(() => { if (isAlive(this)) { let newDirection = vec2(0, 0); const gravity = this.gravityScale != 0; if (this.currentTarget) { const chasingDelay = time() - savedChasingTime > param.chasingDelay; if (!chasing) { this.trigger('spotted'); chasing = true; savedChasingTime = time(); } if (chasingDelay && stopDistanceCheck(this.currentTarget.pos)) { newDirection = this.currentTarget.pos.sub(this.pos); if ( gravity && param.jumping && newDirection.y < -1 && this.isGrounded() ) this.jump(param.jumpForce); if (gravity) newDirection.y = 0; } } else { chasing = false; if (stopDistanceCheck(retreatPos)) newDirection = retreatPos.sub(this.pos); } tween( direction, newDirection.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()); } }); }, };}