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

107 lines
4.2 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-23 09:22:22 +01:00
import { ItemManager } from "../Items/ItemManager";
import { ItemType } from "../Items/ItemType";
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";
2023-01-09 11:09:06 +01:00
import { HaloProjectileLauncher } from "../Projectile/ProjectileLauncher/HaloProjectileLauncher";
import { WaveProjectileLauncher } from "../Projectile/ProjectileLauncher/WaveProjectileLauncher";
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-22 11:40:07 +01:00
@property(AudioClip) private playerHit: AudioClip;
@property(AudioClip) private playerDeath: 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;
2022-12-23 09:52:26 +01:00
@property(AudioClip) private magnetPickup: AudioClip;
@property(AudioClip) private chestPickup: AudioClip;
2022-12-21 14:56:23 +01:00
@property(AudioClip) private levelUp: AudioClip;
2022-12-22 12:08:00 +01:00
@property(AudioClip) private horizontalProjectileLaunch: AudioClip;
@property(AudioClip) private diagonalProjectileLaunch: AudioClip;
@property(AudioClip) private haloProjectileLaunch: AudioClip;
2022-12-20 10:00:47 +01:00
private audioPlayer: AudioPlayer;
2022-12-22 11:40:07 +01:00
private player: Player;
2022-12-20 10:00:47 +01:00
2022-12-22 12:08:00 +01:00
public init(
player: Player,
enemyManager: EnemyManager,
itemManager: ItemManager,
horizontalLauncher: WaveProjectileLauncher,
diagonalLauncher: WaveProjectileLauncher,
haloLauncher: HaloProjectileLauncher
): void {
2022-12-21 14:56:23 +01:00
AppRoot.Instance.AudioPlayer.playMusic(this.music);
2022-12-20 10:00:47 +01:00
this.audioPlayer = AppRoot.Instance.AudioPlayer;
2022-12-22 11:40:07 +01:00
this.player = player;
2022-12-20 10:00:47 +01:00
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);
2022-12-22 11:40:07 +01:00
player.Health.HealthPointsChangeEvent.on(this.tryPlayPlayerHitSound, this);
2022-12-21 14:56:23 +01:00
2022-12-20 10:00:47 +01:00
enemyManager.EnemyAddedEvent.on(this.addEnemyListeners, this);
enemyManager.EnemyRemovedEvent.on(this.removeEnemyListeners, this);
2022-12-22 12:08:00 +01:00
itemManager.PickupEvent.on(this.playPickupItemSound, this);
horizontalLauncher.ProjectileLaunchedEvent.on(() => this.audioPlayer.playSound(this.horizontalProjectileLaunch), this);
diagonalLauncher.ProjectileLaunchedEvent.on(() => this.audioPlayer.playSound(this.diagonalProjectileLaunch), this);
2023-01-02 09:17:11 +01:00
haloLauncher.ProjectilesLaunchedEvent.on(() => this.audioPlayer.playSound(this.haloProjectileLaunch), this);
2022-12-20 10:00:47 +01:00
}
private addEnemyListeners(enemy: Enemy): void {
enemy.Health.HealthPointsChangeEvent.on(this.playEnemyHitSound, this);
}
private removeEnemyListeners(enemy: Enemy): void {
enemy.Health.HealthPointsChangeEvent.off(this.playEnemyHitSound);
}
2022-12-22 11:40:07 +01:00
private tryPlayPlayerHitSound(healthChange: number): void {
if (healthChange < 0) {
this.audioPlayer.playSound(this.playerHit);
}
if (!this.player.Health.IsAlive) {
this.audioPlayer.playSound(this.playerDeath);
}
}
2022-12-20 10:00:47 +01:00
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;
2022-12-23 09:52:26 +01:00
case ItemType.Magnet:
clipToPlay = this.magnetPickup;
break;
case ItemType.Chest:
clipToPlay = this.chestPickup;
break;
2022-12-21 14:56:23 +01:00
default:
break;
}
this.audioPlayer.playSound(clipToPlay);
}
2022-12-20 10:00:47 +01:00
}