respawn
Permet à un objet de pouvoir réapparaître à sa position d'origine
triggers on self
"respawn""player death"parameters
| Parameter | Default Value | Type | Description |
|---|---|---|---|
onDeath | se réinitialise quand l'objet disparaît | boolean | false |
onPlayerDeath | se réinitialise quand les joueurs perdent | boolean | true |
appearAnimDuration | durée de l'animation d'apparition | number | 0.4 |
delay | délai avant la réapparition | number | 0.4 |
hiddenPosition | vec2 | vec2(-1000, -1000) |
example
respawn({ onDeath: false, onPlayerDeath: true, appearAnimDuration: 0.4, delay: 0.4, hiddenPosition: vec2(-1000, -1000)}),function respawn(p) { const param = { onDeath: false, // se réinitialise quand l'objet disparaît onPlayerDeath: true, // se réinitialise quand les joueurs perdent appearAnimDuration: 0.4, // durée de l'animation d'apparition delay: 0.4, // délai avant la réapparition hiddenPosition: vec2(-1000, -1000), ...p, }; return { id: 'respawn', resetPos: null, resetCol: null, resetScale: null, resetOpacity: null, resetStatic: null, resetCollisionIgnore: null, resetGravityScale: null, loadingForRespawn: false, add() { requestAnimationFrame(() => { if (this.has('pos')) this.resetPos = vec2(this.pos); if (this.has('color')) this.resetCol = this.color; if (this.has('scale') && this.scale) this.resetScale = this.scale; if (this.has('opacity')) this.resetOpacity = this.opacity; if (this.has('area')) this.resetCollisionIgnore = this.collisionIgnore; if (this.has('body')) this.resetGravityScale = this.gravityScale; this.resetStatic = this.isStatic; }); this.on('death', () => { wait(param.delay, () => { let alivePlayers = 0; for (const e of get('player', { recursive: true, })) { if (e.isAlive) { alivePlayers++; } } if (param.onDeath && alivePlayers > 0) this.trigger('respawn'); else if (alivePlayers == 0) { for (const e of get('*', { recursive: true })) { e.trigger('player death'); } for (const e of get('respawn', { recursive: true, })) { e.playerDeathRespawn(); } } else if (!this.has('player')) { if (this.has('body')) this.isStatic = true; if (this.has('area')) this.collisionIgnore = ['*']; if (this.pos) this.collisionIgnore = ['*']; this.pos = param.hiddenPosition; } }); }); this.on('respawn', () => { this.hiddenFromScene = false; this.hidden = false; this.loadingForRespawn = false; this.isStatic = this.resetStatic; if (this.has('pos')) this.pos = this.resetPos; if (this.has('scale') && this.scale) this.scaleTo(this.resetScale); if (this.has('body')) this.vel = vec2(0, 0); if (this.has('color')) this.color = this.resetCol; if (this.has('area')) this.collisionIgnore = this.resetCollisionIgnore; if (this.has('body')) this.gravityScale = this.resetGravityScale; if (this.has('opacity')) { this.opacity = this.resetOpacity; this.fadeIn(param.appearAnimDuration); } }); }, playerDeathRespawn() { if (param.onPlayerDeath) this.trigger('respawn'); }, setInitPos(p) { this.resetPos = p; }, };}