From 33025262fd04a7905705ddb30cce8edc56493bc5 Mon Sep 17 00:00:00 2001 From: John Edvard Reiten Date: Tue, 13 Sep 2022 13:45:51 +0900 Subject: [PATCH] fix: Avoid adding extra listeners, avoid moving when has won, avoid respawning when has won --- src/Player.js | 4 ++++ src/PlayerControls.js | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Player.js b/src/Player.js index 7f9843e..a3dce96 100644 --- a/src/Player.js +++ b/src/Player.js @@ -25,6 +25,7 @@ export class Player { sprite = { render: () => {}, x: 0, y: 0 }; headSprite = { render: () => {}, x: 0, y: 0 }; // From Arcadian API hasWon = false; + hasSetListeners = false; headImg; headOffset = { x: 10, y: 38 }; isLeft = false; @@ -184,6 +185,7 @@ export class Player { } } handleDeadInAir() { + if (this.hasWon) return; if (!this.isRopeCut || this.playerState == PLAYER_DEAD) return; this.deadInAirTimer += 1; if (this.deadInAirTimer >= this.deadInAirLimit) { @@ -243,9 +245,11 @@ export class Player { } resetHearts() {} listenForGameEvents() { + if (this.hasSetListeners) return; on(GOAL_COLLISION, this.onGoalCollision); on(ARCADIAN_HEAD_SELECTED, this.onArcadianAdded); on(CUT_ROPE, this.onCutRope); + this.hasSetListeners = true; } onGoalCollision = () => { this.hasWon = true; diff --git a/src/PlayerControls.js b/src/PlayerControls.js index 3b9cfeb..6b0c4d7 100644 --- a/src/PlayerControls.js +++ b/src/PlayerControls.js @@ -9,7 +9,12 @@ export class PlayerControls { } updateControls() { - if (this.player.isRopeCut || this.player.rope.length <= 0) return; + if ( + this.player.hasWon || + this.player.isRopeCut || + this.player.rope.length <= 0 + ) + return; // TODO (johnedvard) add support for touch gesture and gamepad (if enough space) if (keyPressed('arrowleft')) { this.player.applyForce(-1.5, -1);