From e53e3953dc4d370d3dd8915fb7fd1388fa076665 Mon Sep 17 00:00:00 2001 From: Daniel Steckert <71stda1bif@hft-stuttgart.de> Date: Thu, 11 Jun 2020 21:54:48 +0200 Subject: [PATCH] Trigger sound effects --- GoldWars/GoldWars/Entities/EntityManager.swift | 1 + GoldWars/GoldWars/Entities/HUD.swift | 4 ++++ GoldWars/GoldWars/RoundTimer.swift | 4 ++++ GoldWars/GoldWars/SoundManager.swift | 13 +++++++++++++ 4 files changed, 22 insertions(+) diff --git a/GoldWars/GoldWars/Entities/EntityManager.swift b/GoldWars/GoldWars/Entities/EntityManager.swift index 83e8b7e..161e1e8 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -132,6 +132,7 @@ class EntityManager { scene.addChild(spriteNode.unitcountLabel) scene.addChild(spriteNode.fire) } + SoundManager.sharedInstance.playSoundEffect(pathToFile: "attack_base",fileExtension: "wav",volumeLevel: 5.0) } GameCenterManager.sharedInstance.addAchievementProgress(identifier: "de.hft.stuttgart.ip2.goldwars.capture.fifty.bases", increasePercentComplete: 2) } diff --git a/GoldWars/GoldWars/Entities/HUD.swift b/GoldWars/GoldWars/Entities/HUD.swift index 35083f3..5a1704f 100644 --- a/GoldWars/GoldWars/Entities/HUD.swift +++ b/GoldWars/GoldWars/Entities/HUD.swift @@ -59,6 +59,7 @@ class HUD: GKEntity { EntityManager.gameEMInstance.getOpponentBases(for: EntityManager.gameEMInstance.getTeam()).forEach({base in base.component(ofType: TeamComponent.self)?.unitcountLabel.text = "\(base.unitCount)"}) GameCenterManager.sharedInstance.addAchievementProgress(identifier: "de.hft.stuttgart.ip2.goldwars.skill.first.time", increasePercentComplete: 100) GameCenterManager.sharedInstance.addAchievementProgress(identifier: "de.hft.stuttgart.ip2.goldwars.skill.spy.ten", increasePercentComplete: 10) + SoundManager.sharedInstance.playSoundEffect(pathToFile: "use_boost",fileExtension: "wav", volumeLevel: 10.0) } ) defSkill = SingeClickButtonNode( @@ -70,6 +71,7 @@ class HUD: GKEntity { DataService.sharedInstance.localRoundData.hasDefenceBoost = true GameCenterManager.sharedInstance.addAchievementProgress(identifier: "de.hft.stuttgart.ip2.goldwars.skill.first.time", increasePercentComplete: 100) GameCenterManager.sharedInstance.addAchievementProgress(identifier: "de.hft.stuttgart.ip2.goldwars.skill.def.ten", increasePercentComplete: 10) + SoundManager.sharedInstance.playSoundEffect(pathToFile: "use_boost",fileExtension: "wav",volumeLevel: 10.0) } ) atkSkill = SingeClickButtonNode( @@ -81,6 +83,7 @@ class HUD: GKEntity { DataService.sharedInstance.localRoundData.hasAttackBoost = true GameCenterManager.sharedInstance.addAchievementProgress(identifier: "de.hft.stuttgart.ip2.goldwars.skill.first.time", increasePercentComplete: 100) GameCenterManager.sharedInstance.addAchievementProgress(identifier: "de.hft.stuttgart.ip2.goldwars.skill.atk.ten", increasePercentComplete: 10) + SoundManager.sharedInstance.playSoundEffect(pathToFile: "use_boost",fileExtension: "wav",volumeLevel: 10.0) } ) @@ -186,5 +189,6 @@ class HUD: GKEntity { SKAction.scale(by: 0.5, duration: 1), ]) currentRoundLabel.run(newRoundAction) + SoundManager.sharedInstance.playSoundEffect(pathToFile: "new_round", fileExtension: "wav", volumeLevel: 5.0) } } diff --git a/GoldWars/GoldWars/RoundTimer.swift b/GoldWars/GoldWars/RoundTimer.swift index c8bc801..e1b0225 100644 --- a/GoldWars/GoldWars/RoundTimer.swift +++ b/GoldWars/GoldWars/RoundTimer.swift @@ -50,6 +50,10 @@ class RoundTimer: Timer { timeLeft -= 1 EntityManager.gameEMInstance.updateTime(time: (timeLeft > 0 ? String(timeLeft) : roundEnded)) + if 1 ... 5 ~= timeLeft { + SoundManager.sharedInstance.playSoundEffect(pathToFile: "countdown", fileExtension: "wav", volumeLevel: 5.0) + } + if timeLeft == 0 { EntityManager.gameEMInstance.removeModal() RoundCalculatorService.sharedInstance.resetNumberOfAttacksAndFormats() diff --git a/GoldWars/GoldWars/SoundManager.swift b/GoldWars/GoldWars/SoundManager.swift index 9f7bb03..c8d12da 100644 --- a/GoldWars/GoldWars/SoundManager.swift +++ b/GoldWars/GoldWars/SoundManager.swift @@ -13,7 +13,9 @@ class SoundManager { public static var sharedInstance = SoundManager() var audioPlayer = AVAudioPlayer() + var effectPlayer = AVAudioPlayer() var backgroundMainMenuAudio: URL? + var soundEffect: URL? var isMusicPlaying = false var isMusicEnabled = true @@ -39,6 +41,17 @@ class SoundManager { UserDefaults.standard.set(true, forKey: "noMusic") } + func playSoundEffect(pathToFile: String, fileExtension: String, volumeLevel: Float){ + soundEffect = Bundle.main.url(forResource: pathToFile, withExtension: fileExtension) + do { + effectPlayer = try AVAudioPlayer(contentsOf: soundEffect!) + } catch { + //TODO: Add logging + } + effectPlayer.volume += volumeLevel + effectPlayer.play() + } + func setVolume(_ volume: Float) { audioPlayer.volume = volume }