Pobtastic / Phaser Guide / Shot Too Soon

Created Wed, 19 Feb 2025 08:28:09 +0100 Modified Thu, 20 Feb 2025 15:27:57 +0000

Introduction

Should be fairly straight-forward to follow. This is the method which fires when the player shoots before the bandit has drawn their weapon:

triggerBandit(index) {
  const bandit = this.bandits[index];
  // Did the player shoot too soon?
  if (!bandit.hasDrawn) {
    this.shotTooSoon();
  }

Method: shotTooSoon()

shotTooSoon() {
  // Stop the current scene from executing, this will at least
  // mean the player only loses one life per "mistake".
  this.bandits.forEach(bandit => {
    // Stop any further click input.
    bandit.disableInteractive();
    // Clear the timer references.
    if (bandit.drawTimer) {
      bandit.drawTimer.remove();
      bandit.drawTimer = null;
    }
    if (bandit.fireTimer) {
      bandit.fireTimer.remove();
      bandit.fireTimer = null;
    }
  });
  // Remove any keyboard input.
  this.input.keyboard.removeAllListeners();
  // Grab the last life icon reference.
  const lastLife = this.lifeIcons[this.lifeIcons.length - 1];
  // Flash it 5 times, to show that you've lost it.
  this.tweens.add({
    targets: lastLife,
    alpha: 0,
    duration: 200,
    yoyo: true,
    repeat: 4,
    onComplete: () => {
      // Lose a life.
      this.registry.set('lives', this.registry.get('lives') - 1);
      // Are we out of lives?
      if (this.registry.get('lives') <= 0) {
        // Reset the score.
        this.registry.set('score', 0);
        // Go back to the intro screen.
        this.scene.start('SplashScreen');
      }
      // If not, just restart the scene.
      else {
        this.scene.start('MainScene');
      }
    }
  });
}

West Bank Disassembly