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

View File

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

View File

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

View File

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

View File

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