Add new Buttons to SettingsScene

* Due to UserDefaults change label by start
* add logging in SoundManager

finish work
This commit is contained in:
127-Z3R0 2020-06-25 14:20:16 +02:00
parent 2f664712d0
commit 6970f27a61
3 changed files with 53 additions and 24 deletions

View File

@ -6,4 +6,24 @@
// Copyright © 2020 SP2. All rights reserved. // 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")
}
}

View File

@ -11,11 +11,14 @@ import SpriteKit
class SettingsScene: SKScene { class SettingsScene: SKScene {
var entityManager = EntityManager.settingsEMInstance var entityManager = EntityManager.settingsEMInstance
var musicButtonText = ""
var isMusicDeactivatedByUserDefault = UserDefaults.standard.bool(forKey: "noMusic")
override func sceneDidLoad() { override func sceneDidLoad() {
entityManager.setScene(scene: self) entityManager.setScene(scene: self)
let positionX = self.size.width * 0.1 let positionX = self.size.width * 0.1
let positionY = self.size.height * 0.05 let positionY = self.size.height * 0.05
isMusicDeactivatedByUserDefault ? setMusicButtonTextByUserDefault(text: "OFF") : setMusicButtonTextByUserDefault(text: "ON")
entityManager.add(Button(name: "backToMenuScene", entityManager.add(Button(name: "backToMenuScene",
textureName: "yellow_button04", textureName: "yellow_button04",
text: "Back", text: "Back",
@ -24,28 +27,30 @@ class SettingsScene: SKScene {
let scene = MenuScene(size: self.size) let scene = MenuScene(size: self.size)
self.loadScene(scene: scene) self.loadScene(scene: scene)
})) }))
entityManager.add(Button(name: "StopMenuMusic", entityManager.add(SettingsButton(name: "StopMenuMusic",
textureName: "yellow_button04", text: musicButtonText,
text: "ON/OFF",
position: CGPoint(x: self.size.width * 0.6, y: self.size.height / 2), position: CGPoint(x: self.size.width * 0.6, y: self.size.height / 2),
onButtonPress: { onButtonPress: {
if SoundManager.sharedInstance.isMusicPlaying { if SoundManager.sharedInstance.isMusicPlaying {
SoundManager.sharedInstance.stopMenuMusic() SoundManager.sharedInstance.stopMenuMusic()
SoundManager.sharedInstance.isMusicEnabled = false SoundManager.sharedInstance.isMusicEnabled = false
self.entityManager.changeSettingsButtonText(buttonName: "StopMenuMusic", text: "OFF")
} else { } else {
SoundManager.sharedInstance.isMusicEnabled = true SoundManager.sharedInstance.isMusicEnabled = true
SoundManager.sharedInstance.startMenuMusic() SoundManager.sharedInstance.startMenuMusic()
self.entityManager.changeSettingsButtonText(buttonName: "StopMenuMusic", text: "ON")
} }
})) }))
entityManager.add(Button(name: "StopMovingBackground", entityManager.add(SettingsButton(name: "StopMovingBackground",
textureName: "yellow_button04", text: "MOVE",
text: "MOVE/STOP",
position: CGPoint(x: self.size.width * 0.6, y: self.size.height / 2 - 100), position: CGPoint(x: self.size.width * 0.6, y: self.size.height / 2 - 100),
onButtonPress: { onButtonPress: {
if BackgroundComponent.isMovingBackgroundEnabled { if BackgroundComponent.isMovingBackgroundEnabled {
BackgroundComponent.isMovingBackgroundEnabled = false BackgroundComponent.isMovingBackgroundEnabled = false
self.entityManager.changeSettingsButtonText(buttonName: "StopMovingBackground", text: "STOP")
} else { } else {
BackgroundComponent.isMovingBackgroundEnabled = true BackgroundComponent.isMovingBackgroundEnabled = true
self.entityManager.changeSettingsButtonText(buttonName: "StopMovingBackground", text: "MOVE")
} }
})) }))
entityManager.add(Label(fontnamed: "Courier-Bold", entityManager.add(Label(fontnamed: "Courier-Bold",
@ -83,6 +88,10 @@ class SettingsScene: SKScene {
entityManager.add(Background(size: self.size)) entityManager.add(Background(size: self.size))
} }
func setMusicButtonTextByUserDefault(text: String) {
self.musicButtonText = text
}
func loadScene(scene: SKScene) { func loadScene(scene: SKScene) {
let transition = SKTransition.flipVertical(withDuration: 0.5) let transition = SKTransition.flipVertical(withDuration: 0.5)
entityManager.entities.removeAll() entityManager.entities.removeAll()

View File

@ -27,7 +27,7 @@ class SoundManager {
do { do {
audioPlayer = try AVAudioPlayer(contentsOf: backgroundMainMenuAudio!) audioPlayer = try AVAudioPlayer(contentsOf: backgroundMainMenuAudio!)
} catch { } catch {
//TODO: Add logging os_log("backgroundMusic is broken", log: LOG, type: .error)
} }
audioPlayer.numberOfLoops = -1 audioPlayer.numberOfLoops = -1
audioPlayer.prepareToPlay() audioPlayer.prepareToPlay()