diff --git a/GoldWars/GoldWars/GameCenterManager.swift b/GoldWars/GoldWars/GameCenterManager.swift index c2c14e1..dd073c1 100644 --- a/GoldWars/GoldWars/GameCenterManager.swift +++ b/GoldWars/GoldWars/GameCenterManager.swift @@ -51,7 +51,6 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG override init() { super.init() - // StateManager.stateManager.changeState(wantedState: .syncingSt, menuSc: nil, gameVC: nil) localPlayer.register(self) authUser(); localPlayerRandomNumber = RandomNumber() @@ -148,10 +147,12 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG os_log("State 2 erhalten", log: LOG, type: .info) sendStateToPeers(state: State(state: 3)) initIsFinish = true + StateManager.sharedInstance.changeState(wantedState: .gameSt) os_log("Spiel startet", log: LOG, type: .info) case 3: os_log("State 3 erhalten", log: LOG, type: .info) initIsFinish = true + StateManager.sharedInstance.changeState(wantedState: .gameSt) os_log("Spiel startet", log: LOG, type: .info) case 4: os_log("State 4 erhalten, Peer hat verloren", log: LOG, type: .info) @@ -181,6 +182,7 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG os_log("Map model wurde gesetzt", log: LOG, type: .info) GameCenterManager.sharedInstance.isMatchStarted = true self.gameScene = scene + StateManager.sharedInstance.gameSc = scene sendStateToPeers(state: State(state: 2)) os_log("State 2 wurde an Host gesendet", log: LOG, type: .info) // initIsFinish = true @@ -193,6 +195,7 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG self.gameScene = GameScene(size: self.menusc!.size) let mapModel = MapFactory(scene: self.gameScene!, entityManager: entityManager).load() os_log("Map wurde erstellt", log: LOG, type: .info) + StateManager.sharedInstance.gameSc = gameScene MultiplayerNetwork.sharedInstance.sendMapModelToPlayers(mapModel: mapModel) os_log("Map wurde an Peer gesendet", log: LOG, type: .info) DataService.sharedInstance.setSnapshotModel(snapshotModel: entityManager.getSnapshotModel()) @@ -233,14 +236,16 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG } func matchmakerViewControllerWasCancelled(_ viewController: GKMatchmakerViewController) { viewController.dismiss(animated: true, completion: nil) + StateManager.sharedInstance.changeState(wantedState: .menuSt) } func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFailWithError error: Error) { viewController.dismiss(animated: true, completion: nil) + StateManager.sharedInstance.changeState(wantedState: .menuSt) } func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFind match: GKMatch) { - StateManager.stateManager.changeState(wantedState: .syncingSt, menuSc: menusc, gameVC: nil) + StateManager.sharedInstance.changeState(wantedState: .syncingSt) viewController.dismiss(animated: true, completion: nil) myMatch = match if !isMatchStarted && match.expectedPlayerCount == 0 { diff --git a/GoldWars/GoldWars/GameViewController.swift b/GoldWars/GoldWars/GameViewController.swift index c36c21f..3d75655 100644 --- a/GoldWars/GoldWars/GameViewController.swift +++ b/GoldWars/GoldWars/GameViewController.swift @@ -15,8 +15,10 @@ class GameViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - StateManager.stateManager.createStates() - StateManager.stateManager.changeState(wantedState: .menuSt, menuSc: nil, gameVC: self) + StateManager.sharedInstance.createStates() + StateManager.sharedInstance.gameVC = self + StateManager.sharedInstance.changeState(wantedState: .menuSt) + GameCenterManager.sharedInstance.viewController = self } diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index a4c29a1..32dc733 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -72,7 +72,7 @@ class GameScene: SKScene{ } override func update(_ currentTime: TimeInterval) { - StateManager.stateManager.stateMachine!.update(deltaTime: currentTime) + StateManager.sharedInstance.stateMachine!.update(deltaTime: currentTime) /* entityManager.getBackground()?.update(deltaTime: currentTime) entityManager.getHUD()?.component(ofType: TimerComponent.self)?.update() */ diff --git a/GoldWars/GoldWars/Scenes/MenuScene.swift b/GoldWars/GoldWars/Scenes/MenuScene.swift index 6746c56..7e45358 100644 --- a/GoldWars/GoldWars/Scenes/MenuScene.swift +++ b/GoldWars/GoldWars/Scenes/MenuScene.swift @@ -15,6 +15,7 @@ class MenuScene: SKScene { override func sceneDidLoad() { GameCenterManager.sharedInstance.menusc = self + StateManager.sharedInstance.menuSc = self entityManager.setScene(scene: self) let midX = self.size.width / 2 let midY = self.size.height / 2 @@ -24,8 +25,8 @@ class MenuScene: SKScene { position: CGPoint(x: midX, y: midY), onButtonPress: { if CommandLine.arguments.contains("--no-matchmaking") { - StateManager.stateManager.changeState(wantedState: .gameSt, menuSc: self, gameVC: nil) - //self.loadScene(scene: GameScene(size: self.size)) + StateManager.sharedInstance.changeState(wantedState: .gameSt) + /*self.loadScene(scene: GameScene(size: self.size))*/ SoundManager.sharedInstance.stopMenuMusic() } else { if GameCenterManager.isAuthenticated { @@ -69,7 +70,7 @@ class MenuScene: SKScene { } override func update(_ currentTime: TimeInterval) { - StateManager.stateManager.stateMachine!.update(deltaTime: currentTime) + StateManager.sharedInstance.stateMachine!.update(deltaTime: currentTime) /* if entityManager.entities.count != 0 { entityManager.getBackground()!.update(deltaTime: currentTime) diff --git a/GoldWars/GoldWars/States/GameState.swift b/GoldWars/GoldWars/States/GameState.swift index 02c8c44..de2623f 100644 --- a/GoldWars/GoldWars/States/GameState.swift +++ b/GoldWars/GoldWars/States/GameState.swift @@ -20,10 +20,11 @@ class GameState: GKState { override func didEnter(from previousState: GKState?) { os_log("Entered Game State", log: LOG, type: .info) if CommandLine.arguments.contains("--no-matchmaking") { - StateManager.stateManager.menuSc!.loadScene(scene: GameScene(size: StateManager.stateManager.menuSc!.size)) + StateManager.sharedInstance.menuSc!.loadScene(scene: GameScene(size: StateManager.sharedInstance.menuSc!.size)) } + StateManager.sharedInstance.menuSc!.loadScene(scene: StateManager.sharedInstance.gameSc!) } - + override func update(deltaTime seconds: TimeInterval) { EntityManager.gameEMInstance.getBackground()?.update(deltaTime: seconds) EntityManager.gameEMInstance.getHUD()?.component(ofType: TimerComponent.self)?.update() diff --git a/GoldWars/GoldWars/States/MenuState.swift b/GoldWars/GoldWars/States/MenuState.swift index 289a5a8..fb8c4a4 100644 --- a/GoldWars/GoldWars/States/MenuState.swift +++ b/GoldWars/GoldWars/States/MenuState.swift @@ -27,9 +27,11 @@ class MenuState: GKState { menuEM.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterManager.isAuthenticated } - if GameCenterManager.sharedInstance.initIsFinish { - StateManager.stateManager.menuSc!.loadScene(scene: GameCenterManager.sharedInstance.gameScene!) - } + // if GameCenterManager.sharedInstance.initIsFinish { + // StateManager.sharedInstance.menuSc!.loadScene(scene: GameCenterManager.sharedInstance.gameScene!) + // } + + } override func didEnter(from previousState: GKState?) { @@ -44,8 +46,8 @@ class MenuState: GKState { } func presentFirstViewMenu(){ - if let view = StateManager.stateManager.gameVC!.view as! SKView? { - let scene = MenuScene(size: StateManager.stateManager.gameVC!.view.bounds.size) + if let view = StateManager.sharedInstance.gameVC!.view as! SKView? { + let scene = MenuScene(size: StateManager.sharedInstance.gameVC!.view.bounds.size) EntityManager.menuEMInstance.setScene(scene: scene) scene.scaleMode = .aspectFill view.presentScene(scene) diff --git a/GoldWars/GoldWars/States/StateManager.swift b/GoldWars/GoldWars/States/StateManager.swift index 8afc73b..b441f6e 100644 --- a/GoldWars/GoldWars/States/StateManager.swift +++ b/GoldWars/GoldWars/States/StateManager.swift @@ -11,7 +11,7 @@ import os class StateManager{ - static let stateManager = StateManager() + static let sharedInstance = StateManager() var stateMachine: GKStateMachine? var wantedState: StateTypes? @@ -32,8 +32,9 @@ class StateManager{ if stateMachine?.enter(MenuState.self) == false { if previousState is MenuState{ os_log("Already in Menu State", log: LOG, type: .info) + }else{ + os_log("Failed entering Menu State", log: LOG, type: .info) } - os_log("Failed entering Menu State", log: LOG, type: .info) }else{ currentState = stateMachine?.currentState } @@ -41,8 +42,9 @@ class StateManager{ if stateMachine?.enter(GameState.self) == false { if previousState is GameState{ os_log("Already in Game State", log: LOG, type: .info) + } else { + os_log("Failed entering Game State", log: LOG, type: .info) } - os_log("Failed entering Game State", log: LOG, type: .info) }else{ currentState = stateMachine?.currentState } @@ -50,8 +52,9 @@ class StateManager{ if stateMachine?.enter(SyncingState.self) == false { if previousState is SyncingState{ os_log("Already in Syncing State", log: LOG, type: .info) + }else{ + os_log("Failed entering Syncing State", log: LOG, type: .info) } - os_log("Failed entering Syncing State", log: LOG, type: .info) }else{ currentState = stateMachine?.currentState } @@ -59,8 +62,9 @@ class StateManager{ if stateMachine?.enter(EndGameState.self) == false { if previousState is EndGameState{ os_log("Already in EndGame State", log: LOG, type: .info) + } else{ + os_log("Failed entering EndGame State", log: LOG, type: .info) } - os_log("Failed entering EndGame State", log: LOG, type: .info) }else{ currentState = stateMachine?.currentState } @@ -78,10 +82,15 @@ class StateManager{ stateMachine = GKStateMachine(states: [menuSt, syncingSt, playingSt, endGameSt]) } - func changeState(wantedState: StateTypes, menuSc: MenuScene?, gameVC: GameViewController?){ + /* func changeState(wantedState: StateTypes, menuSc: MenuScene?, gameVC: GameViewController?){ + self.wantedState = wantedState + self.menuSc = menuSc + self.gameVC = gameVC + enterState() + }*/ + + func changeState(wantedState: StateTypes){ self.wantedState = wantedState - self.menuSc = menuSc - self.gameVC = gameVC enterState() } } diff --git a/GoldWars/GoldWars/States/SyncingState.swift b/GoldWars/GoldWars/States/SyncingState.swift index 3a20b0a..e2f6773 100644 --- a/GoldWars/GoldWars/States/SyncingState.swift +++ b/GoldWars/GoldWars/States/SyncingState.swift @@ -16,7 +16,7 @@ class SyncingState: GKState { var previousState: GKState? override func isValidNextState(_ stateClass: AnyClass) -> Bool { - return stateClass is GameState.Type || stateClass is EndGameState.Type + return stateClass is GameState.Type || stateClass is EndGameState.Type || stateClass is MenuState.Type } override func didEnter(from previousState: GKState?) { @@ -30,12 +30,9 @@ class SyncingState: GKState { menuEM.getBackground()!.update(deltaTime: seconds) menuEM.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterManager.isAuthenticated } - - if GameCenterManager.sharedInstance.initIsFinish { - StateManager.stateManager.changeState(wantedState: .gameSt, menuSc: self, gameVC: nil) - } } } + override func willExit(to nextState: GKState) { } }