From 2f664712d0295430453ea73cebf9b86af516eefd Mon Sep 17 00:00:00 2001 From: 127-Z3R0 <81heti1bif@hft-stuttgart.de> Date: Thu, 25 Jun 2020 14:19:24 +0200 Subject: [PATCH 1/4] Add designated SettingsButton for ButtonNode-Visibility * modify EntityManager to add SettingsButton * implement Method to toggle Status on/off * in the ButtonNode make Label visible to change text after init --- GoldWars/GoldWars.xcodeproj/project.pbxproj | 4 ++++ GoldWars/GoldWars/Components/ButtonNode.swift | 11 ++++++----- GoldWars/GoldWars/Entities/EntityManager.swift | 9 +++++++++ GoldWars/GoldWars/Entities/SettingsButton.swift | 9 +++++++++ 4 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 GoldWars/GoldWars/Entities/SettingsButton.swift diff --git a/GoldWars/GoldWars.xcodeproj/project.pbxproj b/GoldWars/GoldWars.xcodeproj/project.pbxproj index bf2da7c..3bbb38a 100644 --- a/GoldWars/GoldWars.xcodeproj/project.pbxproj +++ b/GoldWars/GoldWars.xcodeproj/project.pbxproj @@ -50,6 +50,7 @@ AB671B252494ECF0003FBE8D /* EloHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB671B242494ECF0003FBE8D /* EloHelper.swift */; }; ABA03DA0244BD54F00A66916 /* Base.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABA03D9F244BD54F00A66916 /* Base.swift */; }; ABC0C3732481509300387B8F /* MapUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABC0C3722481509300387B8F /* MapUtils.swift */; }; + C00791EC24A4C1090063E413 /* SettingsButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = C00791EB24A4C1090063E413 /* SettingsButton.swift */; }; C04783EE2468583F004961FB /* intro-music.mp3 in Resources */ = {isa = PBXBuildFile; fileRef = C04783ED2468583F004961FB /* intro-music.mp3 */; }; C04783F024685995004961FB /* SettingsScene.swift in Sources */ = {isa = PBXBuildFile; fileRef = C04783EF24685995004961FB /* SettingsScene.swift */; }; C05BB9C4247D890C00411249 /* SliderComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = C05BB9C3247D890C00411249 /* SliderComponent.swift */; }; @@ -119,6 +120,7 @@ AB671B242494ECF0003FBE8D /* EloHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EloHelper.swift; sourceTree = ""; }; ABA03D9F244BD54F00A66916 /* Base.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Base.swift; sourceTree = ""; }; ABC0C3722481509300387B8F /* MapUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapUtils.swift; sourceTree = ""; }; + C00791EB24A4C1090063E413 /* SettingsButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsButton.swift; sourceTree = ""; }; C04783ED2468583F004961FB /* intro-music.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = "intro-music.mp3"; sourceTree = ""; }; C04783EF24685995004961FB /* SettingsScene.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsScene.swift; sourceTree = ""; }; C05BB9C3247D890C00411249 /* SliderComponent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SliderComponent.swift; sourceTree = ""; }; @@ -239,6 +241,7 @@ 9EEDE02C246FCD770096C735 /* SpinningLogoEntity.swift */, 3E6785412472CBEC007B9DE4 /* Way.swift */, C064E9AB246C151F0022B228 /* Label.swift */, + C00791EB24A4C1090063E413 /* SettingsButton.swift */, ); path = Entities; sourceTree = ""; @@ -418,6 +421,7 @@ C064E9AA246C114C0022B228 /* LabelComponent.swift in Sources */, 3EBD242E245D9332003CECE7 /* Team.swift in Sources */, 9E174C88245DF1FF00209FF0 /* BackgroundComponent.swift in Sources */, + C00791EC24A4C1090063E413 /* SettingsButton.swift in Sources */, 9E78ACBA245CBDAF00526FF7 /* HUD.swift in Sources */, 9EC2FBA72476B1EC00ABF11F /* PlayerInfoComponent.swift in Sources */, 9EEDE02D246FCD770096C735 /* SpinningLogoEntity.swift in Sources */, diff --git a/GoldWars/GoldWars/Components/ButtonNode.swift b/GoldWars/GoldWars/Components/ButtonNode.swift index b98164a..8129fa9 100644 --- a/GoldWars/GoldWars/Components/ButtonNode.swift +++ b/GoldWars/GoldWars/Components/ButtonNode.swift @@ -21,17 +21,15 @@ class ButtonNode: SKSpriteNode { } } } - + var label: SKLabelNode var onButtonPress: () -> () init(textureName: String, text: String, isEnabled: Bool, position: CGPoint, onButtonPress: @escaping () -> ()) { self.onButtonPress = onButtonPress self.isEnabled = isEnabled let texture = SKTexture(imageNamed: textureName) - super.init(texture: texture, color: SKColor.white, size: texture.size()) - self.position = position - - let label = SKLabelNode(fontNamed: "Courier-Bold") + + label = SKLabelNode(fontNamed: "Courier-Bold") label.fontSize = 30 label.fontColor = SKColor.black label.zPosition = 1 @@ -39,6 +37,9 @@ class ButtonNode: SKSpriteNode { label.text = text label.name = "label" + super.init(texture: texture, color: SKColor.white, size: texture.size()) + self.position = position + self.addChild(label) isUserInteractionEnabled = true } diff --git a/GoldWars/GoldWars/Entities/EntityManager.swift b/GoldWars/GoldWars/Entities/EntityManager.swift index 375bbb2..5856ccf 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -66,6 +66,10 @@ class EntityManager { scene.addChild(spinningLogoEntity.warsLetteringNode) } + if let settingsButtonEntity = entity as? SettingsButton { + scene.addChild(settingsButtonEntity.settingsButton) + } + if let wayEntity = entity as? Way { scene.addChild(wayEntity.localWayComponent) } @@ -293,6 +297,11 @@ class EntityManager { return entities.filter{$0 is Button && ($0 as! Button).name == buttonName }[0] as! Button } + func changeSettingsButtonText(buttonName: String, text: String) { + let button = entities.filter{$0 is SettingsButton && ($0 as! SettingsButton).name == buttonName }[0] as! SettingsButton + button.settingsButton.label.text = text + } + func getHUD() -> HUD? { return entities.filter{$0 is HUD}.first as? HUD } diff --git a/GoldWars/GoldWars/Entities/SettingsButton.swift b/GoldWars/GoldWars/Entities/SettingsButton.swift new file mode 100644 index 0000000..fd5e628 --- /dev/null +++ b/GoldWars/GoldWars/Entities/SettingsButton.swift @@ -0,0 +1,9 @@ +// +// SettingsButton.swift +// GoldWars +// +// Created by Tim Herbst on 25.06.20. +// Copyright © 2020 SP2. All rights reserved. +// + +import Foundation 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 2/4] 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() From bf50703b1c4fe04c528bf42443aa88289263c4bc Mon Sep 17 00:00:00 2001 From: 127-Z3R0 <81heti1bif@hft-stuttgart.de> Date: Thu, 25 Jun 2020 14:39:31 +0200 Subject: [PATCH 3/4] Ignore the last two Commits -> stupidy impact * Access to ButtonNode via Component * revert work and implement everything needed --- .../GoldWars/Entities/EntityManager.swift | 8 +-- GoldWars/GoldWars/SettingsScene.swift | 50 ++++++++++--------- 2 files changed, 28 insertions(+), 30 deletions(-) diff --git a/GoldWars/GoldWars/Entities/EntityManager.swift b/GoldWars/GoldWars/Entities/EntityManager.swift index 5856ccf..0621bf1 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -66,10 +66,6 @@ class EntityManager { scene.addChild(spinningLogoEntity.warsLetteringNode) } - if let settingsButtonEntity = entity as? SettingsButton { - scene.addChild(settingsButtonEntity.settingsButton) - } - if let wayEntity = entity as? Way { scene.addChild(wayEntity.localWayComponent) } @@ -298,8 +294,8 @@ class EntityManager { } func changeSettingsButtonText(buttonName: String, text: String) { - let button = entities.filter{$0 is SettingsButton && ($0 as! SettingsButton).name == buttonName }[0] as! SettingsButton - button.settingsButton.label.text = text + let button = entities.filter{$0 is Button && ($0 as! Button).name == buttonName }[0] as! Button + button.component(ofType: ButtonComponent.self)?.buttonNode.label.text = text } func getHUD() -> HUD? { diff --git a/GoldWars/GoldWars/SettingsScene.swift b/GoldWars/GoldWars/SettingsScene.swift index 2a96085..b408c84 100644 --- a/GoldWars/GoldWars/SettingsScene.swift +++ b/GoldWars/GoldWars/SettingsScene.swift @@ -27,31 +27,33 @@ class SettingsScene: SKScene { let scene = MenuScene(size: self.size) self.loadScene(scene: scene) })) - 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: "StopMenuMusic", + textureName: "yellow_button04", + 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(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(Button(name: "StopMovingBackground", + textureName: "yellow_button04", + 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", From 77ffe9a815ca8d3504aa818609e349fe2da6010a Mon Sep 17 00:00:00 2001 From: 127-Z3R0 <81heti1bif@hft-stuttgart.de> Date: Thu, 25 Jun 2020 14:42:23 +0200 Subject: [PATCH 4/4] delete unessecary files --- GoldWars/GoldWars.xcodeproj/project.pbxproj | 4 --- .../GoldWars/Entities/SettingsButton.swift | 29 ------------------- 2 files changed, 33 deletions(-) delete mode 100644 GoldWars/GoldWars/Entities/SettingsButton.swift diff --git a/GoldWars/GoldWars.xcodeproj/project.pbxproj b/GoldWars/GoldWars.xcodeproj/project.pbxproj index 3bbb38a..bf2da7c 100644 --- a/GoldWars/GoldWars.xcodeproj/project.pbxproj +++ b/GoldWars/GoldWars.xcodeproj/project.pbxproj @@ -50,7 +50,6 @@ AB671B252494ECF0003FBE8D /* EloHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB671B242494ECF0003FBE8D /* EloHelper.swift */; }; ABA03DA0244BD54F00A66916 /* Base.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABA03D9F244BD54F00A66916 /* Base.swift */; }; ABC0C3732481509300387B8F /* MapUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABC0C3722481509300387B8F /* MapUtils.swift */; }; - C00791EC24A4C1090063E413 /* SettingsButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = C00791EB24A4C1090063E413 /* SettingsButton.swift */; }; C04783EE2468583F004961FB /* intro-music.mp3 in Resources */ = {isa = PBXBuildFile; fileRef = C04783ED2468583F004961FB /* intro-music.mp3 */; }; C04783F024685995004961FB /* SettingsScene.swift in Sources */ = {isa = PBXBuildFile; fileRef = C04783EF24685995004961FB /* SettingsScene.swift */; }; C05BB9C4247D890C00411249 /* SliderComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = C05BB9C3247D890C00411249 /* SliderComponent.swift */; }; @@ -120,7 +119,6 @@ AB671B242494ECF0003FBE8D /* EloHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EloHelper.swift; sourceTree = ""; }; ABA03D9F244BD54F00A66916 /* Base.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Base.swift; sourceTree = ""; }; ABC0C3722481509300387B8F /* MapUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapUtils.swift; sourceTree = ""; }; - C00791EB24A4C1090063E413 /* SettingsButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsButton.swift; sourceTree = ""; }; C04783ED2468583F004961FB /* intro-music.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = "intro-music.mp3"; sourceTree = ""; }; C04783EF24685995004961FB /* SettingsScene.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsScene.swift; sourceTree = ""; }; C05BB9C3247D890C00411249 /* SliderComponent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SliderComponent.swift; sourceTree = ""; }; @@ -241,7 +239,6 @@ 9EEDE02C246FCD770096C735 /* SpinningLogoEntity.swift */, 3E6785412472CBEC007B9DE4 /* Way.swift */, C064E9AB246C151F0022B228 /* Label.swift */, - C00791EB24A4C1090063E413 /* SettingsButton.swift */, ); path = Entities; sourceTree = ""; @@ -421,7 +418,6 @@ C064E9AA246C114C0022B228 /* LabelComponent.swift in Sources */, 3EBD242E245D9332003CECE7 /* Team.swift in Sources */, 9E174C88245DF1FF00209FF0 /* BackgroundComponent.swift in Sources */, - C00791EC24A4C1090063E413 /* SettingsButton.swift in Sources */, 9E78ACBA245CBDAF00526FF7 /* HUD.swift in Sources */, 9EC2FBA72476B1EC00ABF11F /* PlayerInfoComponent.swift in Sources */, 9EEDE02D246FCD770096C735 /* SpinningLogoEntity.swift in Sources */, diff --git a/GoldWars/GoldWars/Entities/SettingsButton.swift b/GoldWars/GoldWars/Entities/SettingsButton.swift deleted file mode 100644 index 98b6c61..0000000 --- a/GoldWars/GoldWars/Entities/SettingsButton.swift +++ /dev/null @@ -1,29 +0,0 @@ -// -// SettingsButton.swift -// GoldWars -// -// Created by Tim Herbst on 25.06.20. -// Copyright © 2020 SP2. All rights reserved. -// - -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") - } -}