diff --git a/GoldWars/GoldWars.xcodeproj/project.pbxproj b/GoldWars/GoldWars.xcodeproj/project.pbxproj index d815479..36a728c 100644 --- a/GoldWars/GoldWars.xcodeproj/project.pbxproj +++ b/GoldWars/GoldWars.xcodeproj/project.pbxproj @@ -404,7 +404,7 @@ 9E174C88245DF1FF00209FF0 /* BackgroundComponent.swift in Sources */, 9E78ACBA245CBDAF00526FF7 /* HUD.swift in Sources */, 9EC2FBA72476B1EC00ABF11F /* PlayerInfoComponent.swift in Sources */, - AE6BB1C0248030720063ECAE /* States.swift in Sources */, + AE6BB1C0248030720063ECAE /* StateTypes.swift in Sources */, 9EEDE02D246FCD770096C735 /* SpinningLogoEntity.swift in Sources */, 9E174C86245DD91500209FF0 /* ButtonComponent.swift in Sources */, 11036113244B3E30008610AF /* MenuScene.swift in Sources */, diff --git a/GoldWars/GoldWars/GameViewController.swift b/GoldWars/GoldWars/GameViewController.swift index de60002..2d3c89a 100644 --- a/GoldWars/GoldWars/GameViewController.swift +++ b/GoldWars/GoldWars/GameViewController.swift @@ -12,14 +12,12 @@ import GameplayKit class GameViewController: UIViewController { - var stateMachine: GKStateMachine? - override func viewDidLoad() { super.viewDidLoad() - createStates() - failedEnteringStates() - self.stateMachine?.enter(MenuState.self) + StateManager.stateManager.allowedState = StateTypes.menuSt + StateManager.stateManager.createStates() + StateManager.stateManager.enterState() if let view = self.view as! SKView? { let scene = MenuScene(size: self.view.bounds.size) @@ -31,9 +29,8 @@ class GameViewController: UIViewController { view.showsNodeCount = true } GameCenterManager.sharedInstance.viewController = self - - } + override var shouldAutorotate: Bool { return true } @@ -49,24 +46,4 @@ class GameViewController: UIViewController { override var prefersStatusBarHidden: Bool { return true } - - func createStates(){ - let menuSt = MenuState() - let syncingSt = SyncingState() - let playingSt = GameState() - - stateMachine = GKStateMachine(states: [menuSt, syncingSt, playingSt]) - } - - func failedEnteringStates(){ - if stateMachine?.enter(MenuState.self) == false { - print("failed to enter MenuState") - } - if stateMachine?.enter(SyncingState.self) == false { - print("failed to enter Syncing") - } - if stateMachine?.enter(GameState.self) == false { - print("failed to enter Game") - } - } } diff --git a/GoldWars/GoldWars/StateTypes.swift b/GoldWars/GoldWars/StateTypes.swift new file mode 100644 index 0000000..f9e9f8f --- /dev/null +++ b/GoldWars/GoldWars/StateTypes.swift @@ -0,0 +1,136 @@ +// +// States.swift +// GoldWars +// +// Created by Chauntalle Schüle on 28.05.20. +// Copyright © 2020 SP2. All rights reserved. +// + +import GameKit +import os + +enum StateTypes { + case menuSt + case syncingSt + case gameSt + case endGameSt +} + +class StateManager{ + + static let stateManager = StateManager() + + var stateMachine: GKStateMachine? + var allowedState: StateTypes? + let LOG = OSLog.init(subsystem: "StateManager", category: "StateManager") + + func enterState(){ + switch allowedState { + case .menuSt: + if stateMachine?.enter(MenuState.self) == false { + os_log("Failed entering Menu State", log: LOG, type: .info) + } + case .gameSt: + if stateMachine?.enter(GameState.self) == false { + os_log("Failed entering Game State", log: LOG, type: .info) + } + case .syncingSt: + if stateMachine?.enter(SyncingState.self) == false { + os_log("Failed entering Syncing State", log: LOG, type: .info) + } + case .endGameSt: + if stateMachine?.enter(EndGameState.self) == false { + os_log("Failed entering EndGame State", log: LOG, type: .info) + } + default: + os_log("Allowed State was found nil, can't enter any State", log: LOG, type: .info) + } + } + + func createStates(){ + let menuSt = MenuState() + let syncingSt = SyncingState() + let playingSt = GameState() + let endGameSt = EndGameState() + + stateMachine = GKStateMachine(states: [menuSt, syncingSt, playingSt, endGameSt]) + } +} + +class MenuState: GKState { + + let LOG = OSLog.init(subsystem: "MenuState", category: "MenuState") + + override func isValidNextState(_ stateClass: AnyClass) -> Bool { + /* guard GameViewController.allowedState != States.menuSt && GameViewController.allowedState == nil else { + return false + } + if (GameViewController.allowedState == States.syncingSt){ + return stateClass is SyncingState.Type + }else { + return false + }*/ + return stateClass is SyncingState.Type + } + + override func update(deltaTime seconds: TimeInterval) { + } + + override func didEnter(from previousState: GKState?) { + os_log("Entered Menu State", log: LOG, type: .info) + } + + override func willExit(to nextState: GKState) { + } +} + +class SyncingState: GKState { + let LOG = OSLog.init(subsystem: "SyncingState", category: "SyncingState") + + override func isValidNextState(_ stateClass: AnyClass) -> Bool { + return stateClass is GameState.Type || stateClass is EndGameState.Type + } + + override func didEnter(from previousState: GKState?) { + os_log("Entered Syncing State", log: LOG, type: .info) + } + override func update(deltaTime seconds: TimeInterval) { + } + override func willExit(to nextState: GKState) { + } +} + +class GameState: GKState { + let LOG = OSLog.init(subsystem: "GameState", category: "GameState") + + override func isValidNextState(_ stateClass: AnyClass) -> Bool { + return stateClass is SyncingState.Type + } + + override func didEnter(from previousState: GKState?) { + os_log("Entered Game State", log: LOG, type: .info) + } + override func update(deltaTime seconds: TimeInterval) { + } + override func willExit(to nextState: GKState) { + } +} + +class EndGameState: GKState { + + let LOG = OSLog.init(subsystem: "EndGameState", category: "EndGameState") + + override func isValidNextState(_ stateClass: AnyClass) -> Bool { + return stateClass is MenuState.Type + } + + override func update(deltaTime seconds: TimeInterval) { + } + + override func didEnter(from previousState: GKState?) { + os_log("Entered EndGame State", log: LOG, type: .info) + } + + override func willExit(to nextState: GKState) { + } +} diff --git a/GoldWars/GoldWars/States.swift b/GoldWars/GoldWars/States.swift deleted file mode 100644 index 5f34739..0000000 --- a/GoldWars/GoldWars/States.swift +++ /dev/null @@ -1,41 +0,0 @@ -// -// States.swift -// GoldWars -// -// Created by Chauntalle Schüle on 28.05.20. -// Copyright © 2020 SP2. All rights reserved. -// - -import GameKit - -class MenuState: GKState { - - override func isValidNextState(_ stateClass: AnyClass) -> Bool { - return stateClass is SyncingState.Type - } - - override func didEnter(from previousState: GKState?) { - print("Entered MenuState") - } -} - -class SyncingState: GKState { - override func isValidNextState(_ stateClass: AnyClass) -> Bool { - return stateClass is GameState.Type - } - - override func didEnter(from previousState: GKState?) { - print("Entered Syncing State") - } - -} - -class GameState: GKState { - override func isValidNextState(_ stateClass: AnyClass) -> Bool { - return stateClass is SyncingState.Type - } - - override func didEnter(from previousState: GKState?) { - print("Entered Game State") - } -}