Slash-The-Hordes/assets/Scripts/Game/Audio/GameAudioAdapter.ts
2022-12-20 10:00:47 +01:00

33 lines
1.1 KiB
TypeScript

import { _decorator, Component, Node, AudioClip } from "cc";
import { AppRoot } from "../../AppRoot/AppRoot";
import { AudioPlayer } from "../../Services/AudioPlayer/AudioPlayer";
import { Enemy } from "../Unit/Enemy/Enemy";
import { EnemyManager } from "../Unit/Enemy/EnemyManager";
const { ccclass, property } = _decorator;
@ccclass("GameAudioAdapter")
export class GameAudioAdapter extends Component {
@property(AudioClip) private enemyHit: AudioClip;
private audioPlayer: AudioPlayer;
public init(enemyManager: EnemyManager): void {
this.audioPlayer = AppRoot.Instance.AudioPlayer;
enemyManager.EnemyAddedEvent.on(this.addEnemyListeners, this);
enemyManager.EnemyRemovedEvent.on(this.removeEnemyListeners, this);
}
private addEnemyListeners(enemy: Enemy): void {
enemy.Health.HealthPointsChangeEvent.on(this.playEnemyHitSound, this);
}
private removeEnemyListeners(enemy: Enemy): void {
enemy.Health.HealthPointsChangeEvent.off(this.playEnemyHitSound);
}
private playEnemyHitSound(): void {
this.audioPlayer.playSound(this.enemyHit);
}
}