From 6970f27a61971ef88fef0281acb56dd98ffe9fc0 Mon Sep 17 00:00:00 2001 From: 127-Z3R0 <81heti1bif@hft-stuttgart.de> Date: Thu, 25 Jun 2020 14:20:16 +0200 Subject: [PATCH] Add new Buttons to SettingsScene * Due to UserDefaults change label by start * add logging in SoundManager finish work --- .../GoldWars/Entities/SettingsButton.swift | 22 +++++++- GoldWars/GoldWars/SettingsScene.swift | 53 +++++++++++-------- GoldWars/GoldWars/SoundManager.swift | 2 +- 3 files changed, 53 insertions(+), 24 deletions(-) diff --git a/GoldWars/GoldWars/Entities/SettingsButton.swift b/GoldWars/GoldWars/Entities/SettingsButton.swift index fd5e628..98b6c61 100644 --- a/GoldWars/GoldWars/Entities/SettingsButton.swift +++ b/GoldWars/GoldWars/Entities/SettingsButton.swift @@ -6,4 +6,24 @@ // Copyright © 2020 SP2. All rights reserved. // -import Foundation +import GameKit + +class SettingsButton: GKEntity { + + var name: String + var settingsButton: ButtonNode + + init(name: String, text: String, position: CGPoint, onButtonPress: @escaping () -> ()) { + self.name = name + settingsButton = ButtonNode(textureName: "yellow_button04", + text: text, + isEnabled: true, + position: position, + onButtonPress: onButtonPress) + super.init() + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } +} diff --git a/GoldWars/GoldWars/SettingsScene.swift b/GoldWars/GoldWars/SettingsScene.swift index 2e0760d..2a96085 100644 --- a/GoldWars/GoldWars/SettingsScene.swift +++ b/GoldWars/GoldWars/SettingsScene.swift @@ -11,11 +11,14 @@ import SpriteKit class SettingsScene: SKScene { var entityManager = EntityManager.settingsEMInstance + var musicButtonText = "" + var isMusicDeactivatedByUserDefault = UserDefaults.standard.bool(forKey: "noMusic") override func sceneDidLoad() { entityManager.setScene(scene: self) let positionX = self.size.width * 0.1 let positionY = self.size.height * 0.05 + isMusicDeactivatedByUserDefault ? setMusicButtonTextByUserDefault(text: "OFF") : setMusicButtonTextByUserDefault(text: "ON") entityManager.add(Button(name: "backToMenuScene", textureName: "yellow_button04", text: "Back", @@ -24,29 +27,31 @@ class SettingsScene: SKScene { let scene = MenuScene(size: self.size) self.loadScene(scene: scene) })) - entityManager.add(Button(name: "StopMenuMusic", - textureName: "yellow_button04", - text: "ON/OFF", - position: CGPoint(x: self.size.width * 0.6, y: self.size.height / 2), - onButtonPress: { - if SoundManager.sharedInstance.isMusicPlaying { - SoundManager.sharedInstance.stopMenuMusic() - SoundManager.sharedInstance.isMusicEnabled = false - } else { - SoundManager.sharedInstance.isMusicEnabled = true - SoundManager.sharedInstance.startMenuMusic() - } + entityManager.add(SettingsButton(name: "StopMenuMusic", + text: musicButtonText, + position: CGPoint(x: self.size.width * 0.6, y: self.size.height / 2), + onButtonPress: { + if SoundManager.sharedInstance.isMusicPlaying { + SoundManager.sharedInstance.stopMenuMusic() + SoundManager.sharedInstance.isMusicEnabled = false + self.entityManager.changeSettingsButtonText(buttonName: "StopMenuMusic", text: "OFF") + } else { + SoundManager.sharedInstance.isMusicEnabled = true + SoundManager.sharedInstance.startMenuMusic() + self.entityManager.changeSettingsButtonText(buttonName: "StopMenuMusic", text: "ON") + } })) - entityManager.add(Button(name: "StopMovingBackground", - textureName: "yellow_button04", - text: "MOVE/STOP", - position: CGPoint(x: self.size.width * 0.6, y: self.size.height / 2 - 100), - onButtonPress: { - if BackgroundComponent.isMovingBackgroundEnabled { - BackgroundComponent.isMovingBackgroundEnabled = false - } else { - BackgroundComponent.isMovingBackgroundEnabled = true - } + entityManager.add(SettingsButton(name: "StopMovingBackground", + text: "MOVE", + position: CGPoint(x: self.size.width * 0.6, y: self.size.height / 2 - 100), + onButtonPress: { + if BackgroundComponent.isMovingBackgroundEnabled { + BackgroundComponent.isMovingBackgroundEnabled = false + self.entityManager.changeSettingsButtonText(buttonName: "StopMovingBackground", text: "STOP") + } else { + BackgroundComponent.isMovingBackgroundEnabled = true + self.entityManager.changeSettingsButtonText(buttonName: "StopMovingBackground", text: "MOVE") + } })) entityManager.add(Label(fontnamed: "Courier-Bold", name: "SettingsLabel", @@ -83,6 +88,10 @@ class SettingsScene: SKScene { entityManager.add(Background(size: self.size)) } + func setMusicButtonTextByUserDefault(text: String) { + self.musicButtonText = text + } + func loadScene(scene: SKScene) { let transition = SKTransition.flipVertical(withDuration: 0.5) entityManager.entities.removeAll() diff --git a/GoldWars/GoldWars/SoundManager.swift b/GoldWars/GoldWars/SoundManager.swift index 55b7037..fc39933 100644 --- a/GoldWars/GoldWars/SoundManager.swift +++ b/GoldWars/GoldWars/SoundManager.swift @@ -27,7 +27,7 @@ class SoundManager { do { audioPlayer = try AVAudioPlayer(contentsOf: backgroundMainMenuAudio!) } catch { - //TODO: Add logging + os_log("backgroundMusic is broken", log: LOG, type: .error) } audioPlayer.numberOfLoops = -1 audioPlayer.prepareToPlay()