From 888d194676286023fded8c0323ed0877fcad5af8 Mon Sep 17 00:00:00 2001 From: 127-Z3R0 <81heti1bif@hft-stuttgart.de> Date: Sun, 10 May 2020 18:10:36 +0200 Subject: [PATCH] add SettingsScene with background and music also boolean query for multiple audioplayer start --- GoldWars/GoldWars.xcodeproj/project.pbxproj | 4 +++ GoldWars/GoldWars/Scenes/MenuScene.swift | 5 ++- GoldWars/GoldWars/SettingsScene.swift | 40 +++++++++++++++++++++ GoldWars/GoldWars/SoundManager.swift | 7 ++++ 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 GoldWars/GoldWars/SettingsScene.swift diff --git a/GoldWars/GoldWars.xcodeproj/project.pbxproj b/GoldWars/GoldWars.xcodeproj/project.pbxproj index 3a7facc..89e886a 100644 --- a/GoldWars/GoldWars.xcodeproj/project.pbxproj +++ b/GoldWars/GoldWars.xcodeproj/project.pbxproj @@ -47,6 +47,7 @@ ABA03DA0244BD54F00A66916 /* Base.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABA03D9F244BD54F00A66916 /* Base.swift */; }; AE151589245F18EF001D363E /* MatchmakingHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE151588245F18EF001D363E /* MatchmakingHelper.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 */; }; C05FAED62468559D0006AF2E /* SoundManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = C05FAED52468559D0006AF2E /* SoundManager.swift */; }; /* End PBXBuildFile section */ @@ -106,6 +107,7 @@ ABA03D9F244BD54F00A66916 /* Base.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Base.swift; sourceTree = ""; }; AE151588245F18EF001D363E /* MatchmakingHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MatchmakingHelper.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 = ""; }; C05FAED52468559D0006AF2E /* SoundManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SoundManager.swift; sourceTree = ""; }; /* End PBXFileReference section */ @@ -166,6 +168,7 @@ 110360E4244B101B008610AF /* Info.plist */, AE151588245F18EF001D363E /* MatchmakingHelper.swift */, 3EBD242B245D8044003CECE7 /* GameCenterHelper.swift */, + C04783EF24685995004961FB /* SettingsScene.swift */, ); path = GoldWars; sourceTree = ""; @@ -379,6 +382,7 @@ 9EA3ABEB245C6DFA006BC61D /* BaseNode.swift in Sources */, 9E04AFAF245E2B73002D5CFC /* AttackActionComponent.swift in Sources */, 110360D9244B101A008610AF /* GameScene.swift in Sources */, + C04783F024685995004961FB /* SettingsScene.swift in Sources */, 116060F7245C57D2004E5A36 /* EntityManager.swift in Sources */, 3EBD242E245D9332003CECE7 /* Team.swift in Sources */, 9E174C88245DF1FF00209FF0 /* BackgroundComponent.swift in Sources */, diff --git a/GoldWars/GoldWars/Scenes/MenuScene.swift b/GoldWars/GoldWars/Scenes/MenuScene.swift index 946d478..c1b93de 100644 --- a/GoldWars/GoldWars/Scenes/MenuScene.swift +++ b/GoldWars/GoldWars/Scenes/MenuScene.swift @@ -35,10 +35,13 @@ class MenuScene: SKScene { position: CGPoint(x: midX, y: midY - 80 ), onButtonPress: { //TODO: create Settings Scene + self.loadScene(scene: SettingsScene(size: self.size)) })) entityManager.add(Background(size: self.size)) - soundManager.startMenuMusic() + if soundManager.isMusicPlaying == false { + soundManager.startMenuMusic() + } } func loadScene(scene: SKScene) { diff --git a/GoldWars/GoldWars/SettingsScene.swift b/GoldWars/GoldWars/SettingsScene.swift new file mode 100644 index 0000000..99f741b --- /dev/null +++ b/GoldWars/GoldWars/SettingsScene.swift @@ -0,0 +1,40 @@ +// +// SettingsScene.swift +// GoldWars +// +// Created by Tim Herbst on 10.05.20. +// Copyright © 2020 SP2. All rights reserved. +// + +import SpriteKit + +class SettingsScene: SKScene { + + var entityManager: EntityManager! + + override func sceneDidLoad() { + entityManager = EntityManager(scene: self) + let positionX = self.size.width * 0.1 + let positionY = self.size.height * 0.1 + + entityManager.add(Button(name: "backToMenuScene", + iconName: "", + text: "Back", + position: CGPoint(x: positionX, y: positionY), + onButtonPress: { + self.loadScene(scene: MenuScene(size: self.size)) + })) + + entityManager.add(Background(size: self.size)) + } + + func loadScene(scene: SKScene) { + let transition = SKTransition.flipVertical(withDuration: 0.5) + self.view?.presentScene(scene, transition: transition) + } + + override func update(_ currentTime: TimeInterval) { + entityManager.getBackground()!.update(deltaTime: currentTime) + } + +} diff --git a/GoldWars/GoldWars/SoundManager.swift b/GoldWars/GoldWars/SoundManager.swift index f882bf0..808c321 100644 --- a/GoldWars/GoldWars/SoundManager.swift +++ b/GoldWars/GoldWars/SoundManager.swift @@ -12,6 +12,7 @@ import AVFoundation class SoundManager { var audioPlayer = AVAudioPlayer() var backgroundMainMenuAudio: URL? + var isMusicPlaying: Bool = false func startMenuMusic() { backgroundMainMenuAudio = Bundle.main.url(forResource: "intro-music", withExtension: "mp3") @@ -23,5 +24,11 @@ class SoundManager { audioPlayer.numberOfLoops = -1 audioPlayer.prepareToPlay() audioPlayer.play() + isMusicPlaying = true + } + + func stopMenuMusic() { + audioPlayer.pause() + isMusicPlaying = false } }