mirror of
https://github.com/MartinKral/Slash-The-Hordes
synced 2026-02-14 12:22:42 +00:00
Audio player
This commit is contained in:
32
assets/Scripts/Game/Audio/GameAudioAdapter.ts
Normal file
32
assets/Scripts/Game/Audio/GameAudioAdapter.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user