add sharedInstance, small cleanup Code

This commit is contained in:
127-Z3R0 2020-05-13 12:59:55 +02:00
parent 284cf1381b
commit 2a42379610
4 changed files with 24 additions and 26 deletions

@ -12,7 +12,7 @@ class BackgroundComponent: GKComponent{
var nodes = [SKSpriteNode]() var nodes = [SKSpriteNode]()
let size: CGSize let size: CGSize
static var isMovingBackgroundEnabled: Bool = true static var isMovingBackgroundEnabled = true
init(size: CGSize) { init(size: CGSize) {
self.size = size self.size = size
@ -28,7 +28,7 @@ class BackgroundComponent: GKComponent{
} }
func update(){ func update(){
if BackgroundComponent.isMovingBackgroundEnabled == true { if BackgroundComponent.isMovingBackgroundEnabled {
for node in nodes{ for node in nodes{
node.position.x -= 2 node.position.x -= 2
if node.position.x < -(size.width) { if node.position.x < -(size.width) {

@ -23,7 +23,7 @@ class MenuScene: SKScene {
onButtonPress: { onButtonPress: {
if CommandLine.arguments.contains("--no-matchmaking") { if CommandLine.arguments.contains("--no-matchmaking") {
self.loadScene(scene: GameScene(size: self.size)) self.loadScene(scene: GameScene(size: self.size))
SoundManager.stopMenuMusic() SoundManager.sharedInstance.stopMenuMusic()
} else { } else {
MatchmakingHelper.sharedInstance.presentMatchmaker(scene: self) MatchmakingHelper.sharedInstance.presentMatchmaker(scene: self)
} }
@ -37,8 +37,8 @@ class MenuScene: SKScene {
})) }))
entityManager.add(Background(size: self.size)) entityManager.add(Background(size: self.size))
if SoundManager.isMusicPlaying == false && SoundManager.isMusicEnabled == true { if SoundManager.sharedInstance.isMusicPlaying == false && SoundManager.sharedInstance.isMusicEnabled == true {
SoundManager.startMenuMusic() SoundManager.sharedInstance.startMenuMusic()
} }
} }

@ -28,29 +28,25 @@ class SettingsScene: SKScene {
text: "ON/OFF", text: "ON/OFF",
position: CGPoint(x: self.size.width / 2, y: self.size.height / 2), position: CGPoint(x: self.size.width / 2, y: self.size.height / 2),
onButtonPress: { onButtonPress: {
if SoundManager.isMusicPlaying == true { if SoundManager.sharedInstance.isMusicPlaying {
SoundManager.stopMenuMusic() SoundManager.sharedInstance.stopMenuMusic()
SoundManager.isMusicEnabled = false SoundManager.sharedInstance.isMusicEnabled = false
} else { } else {
SoundManager.isMusicEnabled = true SoundManager.sharedInstance.isMusicEnabled = true
SoundManager.startMenuMusic() SoundManager.sharedInstance.startMenuMusic()
} }
})) }))
entityManager.add(Button(name: "StopMovingBackground", entityManager.add(Button(name: "StopMovingBackground",
iconName: "", iconName: "",
text: "MOVE/STOP", text: "MOVE/STOP",
position: CGPoint(x: self.size.width / 2, y: self.size.height / 2 - 100), position: CGPoint(x: self.size.width / 2, y: self.size.height / 2 - 100),
onButtonPress: { onButtonPress: {
if BackgroundComponent.isMovingBackgroundEnabled == true { if BackgroundComponent.isMovingBackgroundEnabled {
BackgroundComponent.isMovingBackgroundEnabled = false BackgroundComponent.isMovingBackgroundEnabled = false
} else { } else {
BackgroundComponent.isMovingBackgroundEnabled = true BackgroundComponent.isMovingBackgroundEnabled = true
} }
})) }))
entityManager.add(Background(size: self.size)) entityManager.add(Background(size: self.size))
} }

@ -10,13 +10,15 @@ import SpriteKit
import AVFoundation import AVFoundation
class SoundManager { class SoundManager {
static var audioPlayer = AVAudioPlayer() public static var sharedInstance = SoundManager()
static var backgroundMainMenuAudio: URL?
static var isMusicPlaying: Bool = false
static var isMusicEnabled: Bool = true
static func startMenuMusic() { var audioPlayer = AVAudioPlayer()
SoundManager.isMusicPlaying = true var backgroundMainMenuAudio: URL?
var isMusicPlaying = false
var isMusicEnabled = true
func startMenuMusic() {
self.isMusicPlaying = true
backgroundMainMenuAudio = Bundle.main.url(forResource: "intro-music", withExtension: "mp3") backgroundMainMenuAudio = Bundle.main.url(forResource: "intro-music", withExtension: "mp3")
do { do {
audioPlayer = try AVAudioPlayer(contentsOf: backgroundMainMenuAudio!) audioPlayer = try AVAudioPlayer(contentsOf: backgroundMainMenuAudio!)
@ -25,21 +27,21 @@ class SoundManager {
} }
audioPlayer.numberOfLoops = -1 audioPlayer.numberOfLoops = -1
audioPlayer.prepareToPlay() audioPlayer.prepareToPlay()
if SoundManager.isMusicEnabled == true { if self.isMusicEnabled == true {
audioPlayer.play() audioPlayer.play()
} }
} }
static func stopMenuMusic() { func stopMenuMusic() {
audioPlayer.pause() audioPlayer.pause()
SoundManager.isMusicPlaying = false self.isMusicPlaying = false
} }
static func setVolume(_ volume: Float) { func setVolume(_ volume: Float) {
audioPlayer.volume = volume audioPlayer.volume = volume
} }
static func getVolume() -> Float { func getVolume() -> Float {
return audioPlayer.volume return audioPlayer.volume
} }
} }