Slash-The-Hordes/assets/Scripts/Game/Audio/GameAudioAdapter.ts

67 lines
2.4 KiB
TypeScript
Raw Normal View History

2022-12-20 10:00:47 +01:00
import { _decorator, Component, Node, AudioClip } from "cc";
import { AppRoot } from "../../AppRoot/AppRoot";
import { AudioPlayer } from "../../Services/AudioPlayer/AudioPlayer";
2022-12-21 14:56:23 +01:00
import { ItemManager, ItemType } from "../Items/ItemManager";
2022-12-20 10:00:47 +01:00
import { Enemy } from "../Unit/Enemy/Enemy";
import { EnemyManager } from "../Unit/Enemy/EnemyManager";
2022-12-21 14:56:23 +01:00
import { Player } from "../Unit/Player/Player";
2022-12-20 10:00:47 +01:00
const { ccclass, property } = _decorator;
@ccclass("GameAudioAdapter")
export class GameAudioAdapter extends Component {
2022-12-21 14:56:23 +01:00
@property(AudioClip) private music: AudioClip;
2022-12-20 10:00:47 +01:00
@property(AudioClip) private enemyHit: AudioClip;
2022-12-21 14:56:23 +01:00
@property(AudioClip) private weaponSwing: AudioClip;
@property(AudioClip) private xpPickup: AudioClip;
@property(AudioClip) private goldPickup: AudioClip;
@property(AudioClip) private healthPotionPickup: AudioClip;
@property(AudioClip) private levelUp: AudioClip;
2022-12-20 10:00:47 +01:00
private audioPlayer: AudioPlayer;
2022-12-21 14:56:23 +01:00
public init(player: Player, enemyManager: EnemyManager, itemManager: ItemManager): void {
AppRoot.Instance.AudioPlayer.playMusic(this.music);
2022-12-20 10:00:47 +01:00
this.audioPlayer = AppRoot.Instance.AudioPlayer;
2022-12-21 14:56:23 +01:00
player.Weapon.WeaponStrikeEvent.on(() => this.audioPlayer.playSound(this.weaponSwing), this);
player.Level.LevelUpEvent.on(() => this.audioPlayer.playSound(this.levelUp), this);
itemManager.PickupEvent.on(this.playPickupItemSound, this);
2022-12-20 10:00:47 +01:00
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);
}
2022-12-21 14:56:23 +01:00
private playPickupItemSound(itemType: ItemType): void {
let clipToPlay: AudioClip;
switch (itemType) {
case ItemType.XP:
clipToPlay = this.xpPickup;
break;
case ItemType.Gold:
clipToPlay = this.goldPickup;
break;
case ItemType.HealthPotion:
clipToPlay = this.healthPotionPickup;
break;
default:
break;
}
this.audioPlayer.playSound(clipToPlay);
}
2022-12-20 10:00:47 +01:00
}