fly
Permet à un objet de descendre lorsqu'un autre objet est dessus
parameters
| Parameter | Default Value | Type | Description |
|---|---|---|---|
fallSpeed | number | 40 | |
magnetic | boolean | true | |
speed | number | 60 | |
wanderDistance | number | 0 | |
stopdistance | number | 6 | |
rotateDelay | number | 0.8 | |
collisionIgnore | boolean | true |
example
fly({ fallSpeed: 40, magnetic: true, speed: 60, wanderDistance: 0, stopdistance: 6, rotateDelay: 0.8, collisionIgnore: true}),function fly(p) { const param = { fallSpeed: 40, magnetic: true, speed: 60, wanderDistance: 0, stopdistance: 6, rotateDelay: 0.8, collisionIgnore: true, ...p, }; return { magneticPos: null, falling: false, id: 'fly', wanderTarget: null, wanderOrientation: vec2(0, 0), add() { this.onLand(() => (this.falling = true)); this.onCollideUpdate((obj, col) => this.checkFlyColission(obj, col)); this.onCollideEnd(() => (this.falling = false)); requestAnimationFrame(() => { this.magneticPos = vec2(this.pos); this.setWanderTarget(); }); if (param.collisionIgnore) this.collisionIgnore.push('fly'); }, checkFlyColission(obj, col) { if (obj.has('body') && !obj.has('fly') && !obj.isStatic && col.isTop()) { this.move(0, param.fallSpeed); obj.move(0, param.fallSpeed); this.falling = true; } }, update() { tween( this.wanderOrientation, Vec2.fromAngle(this.wanderTarget.sub(this.pos).angle()), param.rotateDelay, (v) => (this.wanderOrientation = v) ); if (param.magnetic) { if (!this.falling) { if (this.pos.dist(this.wanderTarget) > param.stopdistance) { this.move(this.wanderOrientation.scale(param.speed)); } else this.setWanderTarget(); } } if (this.falling) this.move(0, param.fallSpeed); }, setWanderTarget() { const direction = Vec2.fromAngle(rand(360)); const movement = direction.scale(rand(param.wanderDistance)); this.wanderTarget = this.magneticPos.add(movement); }, };}