respawn
Permet à un objet de pouvoir réapparaître à sa position d'origine
triggers on self
"respawn"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, loadingForRespawn: false, add() { requestAnimationFrame(() => { if (this.is("pos")) this.resetPos = vec2(this.pos) if (this.is("color")) this.resetCol = this.color if (this.is("scale")) this.resetScale = this.scale if (this.is("opacity")) this.resetOpacity = this.opacity 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("respawn", { recursive: true })) e.playerDeathRespawn() }) }) this.on("respawn", () => { this.hiddenFromScene = false this.hidden = false this.loadingForRespawn = false this.isStatic = this.resetStatic; if (this.is("pos")) this.pos = this.resetPos if (this.is("scale")) this.scaleTo(this.resetScale) if (this.is("body")) this.vel = vec2(0, 0) if (this.is("color")) this.color = this.resetCol if (this.is("opacity")) { this.opacity = this.resetOpacity this.fadeIn(param.appearAnimDuration) } }) }, playerDeathRespawn() { if (param.onPlayerDeath) this.trigger("respawn") }, hideFromScene() { this.pos = param.hiddenPosition this.isStatic = true this.hidden = true }, setInitPos(p) { this.resetPos = p }, }}