{"size": 3203, "ext": "js", "max_stars_repo_path": "src/component/AudioLibrary/AudioLibrary.js", "max_stars_repo_name": "pigeonfresh/GlobalGameJame", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/component/AudioLibrary/AudioLibrary.js", "max_issues_repo_name": "pigeonfresh/GlobalGameJame", "max_issues_repo_issues_event_min_datetime": "2019-03-01T13:26:06.000Z", "max_issues_repo_issues_event_max_datetime": "2019-03-01T13:42:16.000Z", "max_forks_repo_path": "src/component/AudioLibrary/AudioLibrary.js", "max_forks_repo_name": "pigeonfresh/GlobalGameJame", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 5, "max_forks_count": null, "avg_line_length": 33.0206185567, "max_line_length": 92, "alphanum_fraction": 0.6022478926}
import { AbstractTransitionComponent } from 'vue-transition-component';
import { Howl, Howler } from 'howler';
import { mapActions } from 'vuex';
import { STOP_CAT } from '../../store/module/cat/cat';
import AudioLibraryTransitionController from './AudioLibraryTransitionController';
import eventBus from '../../util/eventBus';
import SOUND_FX from '../../data/enum/SoundFX';
import { RouteNames } from '../../router/routes';
import { ROOM_STOP_DESTRUCTION } from '../../store/module/rooms/rooms';

// @vue/component
export default {
  name: 'AudioLibrary',
  extends: AbstractTransitionComponent,
  data() {
    return {
      soundFX: null,
    };
  },
  created() {
    eventBus.$on('play-sound-fx', this.playSoundFX);
    eventBus.$on('start-game', this.startSoundTrack);
    this.hanldeUpdate();
    this.soundFX = new Howl({
      src: [`${this.$staticRoot}audio/sfx.mp3`],
      sprite: {
        [SOUND_FX.MEOW_0]: [0, 1000, false],
        [SOUND_FX.MEOW_1]: [50000, 1000, false],
        [SOUND_FX.MEOW_2]: [51000, 1000, false],
        [SOUND_FX.PURR]: [2000, 4000],
        [SOUND_FX.NOM]: [6000, 1000],
        [SOUND_FX.PETTING]: [8000, 1000, false],
        [SOUND_FX.DOORBELL]: [9000, 1000],
        [SOUND_FX.PHONERINGING]: [11000, 1000],
        [SOUND_FX.PHONEHANGUP]: [12000, 1000],
        [SOUND_FX.FOOTSTEP]: [13000, 1000],
        [SOUND_FX.LITTER]: [14000, 2000],
        [SOUND_FX.SCRATCHING]: [16000, 2000],
        [SOUND_FX.FIRE]: [18000, 3000],
        [SOUND_FX.VASEPUT]: [23000, 1000],
        [SOUND_FX.VASEBREAKING]: [24000, 2000],
        [SOUND_FX.FAUCET]: [26000, 1000],
        [SOUND_FX.SINKWATERRUNNING]: [27000, 2000],
        [SOUND_FX.BATHTUB]: [29000, 5000],
        [SOUND_FX.BOXESFALLING]: [34000, 3000],
        [SOUND_FX.BOXESWITHGLASSES]: [37000, 3000],
        [SOUND_FX.ROOSTER]: [40000, 2000],
        [SOUND_FX.OWL]: [42000, 2000],
        [SOUND_FX.CLOCK]: [44000, 3000],
        [SOUND_FX.CLOCKFAST]: [47000, 9000],
      },
    });
  },
  destroyed() {
    eventBus.$off('play-sound-fx', this.playSoundFX);
    eventBus.$off('start-game', this.startSoundTrack);
    cancelAnimationFrame(this.raf);
  },
  methods: {
    ...mapActions({
      stopCat: STOP_CAT,
      stopDestruction: ROOM_STOP_DESTRUCTION,
    }),
    hanldeUpdate() {
      this.gameMusic &&
        eventBus.$emit('music-position', this.gameMusic.seek() / this.gameMusic.duration());
      this.raf = requestAnimationFrame(this.hanldeUpdate);
    },
    playSoundFX(id) {
      this.soundFX.play(id);
    },
    startSoundTrack() {
      this.gameMusic = new Howl({
        src: [`${this.$staticRoot}audio/theme-song.mp3`],
        autoplay: true,
        onend: () => {
          this.$router.push({ name: RouteNames.GAME_OVER });
          this.startGameOver();
        },
      });
      this.gameMusic.play();
    },
    startGameOver() {
      this.gameOver = new Howl({
        src: [`${this.$staticRoot}audio/game-over.mp3`],
      });
      this.gameOver.play();
      this.stopCat();
      this.stopDestruction();
    },
    handleAllComponentsReady() {
      this.transitionController = new AudioLibraryTransitionController(this);
      this.isReady();
    },
  },
};
