From aee6ec1ac1828256b792239a9253ff957d8fc341 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chauntalle=20Schu=CC=88le?= Date: Tue, 2 Jun 2020 21:38:23 +0200 Subject: [PATCH] adding bigger changes after review, deleted enum und methods enter&changeState --- GoldWars/GoldWars/GameCenterManager.swift | 14 ++--- GoldWars/GoldWars/GameViewController.swift | 2 +- .../GoldWars/RoundCalculatorService.swift | 4 +- GoldWars/GoldWars/Scenes/MenuScene.swift | 2 +- GoldWars/GoldWars/States/EndGameState.swift | 8 +++ GoldWars/GoldWars/States/GameState.swift | 8 +++ GoldWars/GoldWars/States/MenuState.swift | 29 +++++---- GoldWars/GoldWars/States/StateManager.swift | 62 ------------------- GoldWars/GoldWars/States/SyncingState.swift | 4 ++ 9 files changed, 48 insertions(+), 85 deletions(-) diff --git a/GoldWars/GoldWars/GameCenterManager.swift b/GoldWars/GoldWars/GameCenterManager.swift index 795aa88..a57edbc 100644 --- a/GoldWars/GoldWars/GameCenterManager.swift +++ b/GoldWars/GoldWars/GameCenterManager.swift @@ -148,13 +148,13 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG sendStateToPeers(state: State(state: 3)) initIsFinish = true print("entered gameSt in case 2 match") - StateManager.sharedInstance.changeState(wantedState: .gameSt) + StateManager.sharedInstance.stateMachine?.enter(GameState.self) os_log("Spiel startet", log: LOG, type: .info) case 3: os_log("State 3 erhalten", log: LOG, type: .info) initIsFinish = true print("entered gameSt in case 3 match") - StateManager.sharedInstance.changeState(wantedState: .gameSt) + StateManager.sharedInstance.stateMachine?.enter(GameState.self) os_log("Spiel startet", log: LOG, type: .info) case 4: os_log("State 4 erhalten, Peer hat verloren", log: LOG, type: .info) @@ -170,13 +170,13 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG DataService.sharedInstance.addRemotePlayerMoves(playerName: player.displayName, localRoundData: roundData) } if let snapshotModel = try? jsonDecoder.decode(SnapshotModel.self, from: data) { - StateManager.sharedInstance.enterState(wantedState: .syncingState) + StateManager.sharedInstance.stateMachine?.enter(SyncingState.self) DataService.sharedInstance.snapshotModel = snapshotModel RoundCalculatorService.sharedInstance.currentRound += 1 entityManager.getHUD()?.setCurrentRound(round: RoundCalculatorService.sharedInstance.currentRound) entityManager.updateSnapshotModel(snapshotModel: snapshotModel) entityManager.getHUD()?.startWithDuration() - StateManager.sharedInstance.enterState(wantedState: .gameState) + StateManager.sharedInstance.stateMachine?.enter(GameState.self) } if let mapModel = try? jsonDecoder.decode(MapGenerationModel.self, from: data) { os_log("Peer hat Map erhalten", log: LOG, type: .info) @@ -238,16 +238,16 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG } func matchmakerViewControllerWasCancelled(_ viewController: GKMatchmakerViewController) { viewController.dismiss(animated: true, completion: nil) - StateManager.sharedInstance.enterState(wantedState: .menuState) + StateManager.sharedInstance.stateMachine?.enter(MenuState.self) } func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFailWithError error: Error) { viewController.dismiss(animated: true, completion: nil) - StateManager.sharedInstance.enterState(wantedState: .menuState) + StateManager.sharedInstance.stateMachine?.enter(MenuState.self) } func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFind match: GKMatch) { - StateManager.sharedInstance.enterState(wantedState: .syncingState) + StateManager.sharedInstance.stateMachine?.enter(SyncingState.self) 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 cf1ebdc..3d3046e 100644 --- a/GoldWars/GoldWars/GameViewController.swift +++ b/GoldWars/GoldWars/GameViewController.swift @@ -16,7 +16,7 @@ class GameViewController: UIViewController { super.viewDidLoad() StateManager.sharedInstance.gameVC = self - StateManager.sharedInstance.enterState(wantedState: .menuState) + StateManager.sharedInstance.stateMachine?.enter(MenuState.self) GameCenterManager.sharedInstance.viewController = self } diff --git a/GoldWars/GoldWars/RoundCalculatorService.swift b/GoldWars/GoldWars/RoundCalculatorService.swift index 77a2688..1954403 100644 --- a/GoldWars/GoldWars/RoundCalculatorService.swift +++ b/GoldWars/GoldWars/RoundCalculatorService.swift @@ -29,7 +29,7 @@ class RoundCalculatorService { var roundNr = 1 func calculateRound() { - StateManager.sharedInstance.enterState(wantedState: .syncingState) + StateManager.sharedInstance.stateMachine?.enter(SyncingState.self) os_log("Started calculating Round", log: RoundCalculatorService.LOG, type: .info) roundNr += 1 @@ -173,7 +173,7 @@ class RoundCalculatorService { entityManager.updateSnapshotModel(snapshotModel: currentSnapshotModel!) entityManager.getHUD()?.startWithDuration() os_log("Finished calculating Round", log: RoundCalculatorService.LOG, type: .info) - StateManager.sharedInstance.enterState(wantedState: .gameState) + StateManager.sharedInstance.stateMachine?.enter(GameState.self) } func collectBaseSpecificMoves() -> [Int: [String: [PlayerMove]]] { diff --git a/GoldWars/GoldWars/Scenes/MenuScene.swift b/GoldWars/GoldWars/Scenes/MenuScene.swift index 91b969e..ce570f4 100644 --- a/GoldWars/GoldWars/Scenes/MenuScene.swift +++ b/GoldWars/GoldWars/Scenes/MenuScene.swift @@ -25,7 +25,7 @@ class MenuScene: SKScene { position: CGPoint(x: midX, y: midY), onButtonPress: { if CommandLine.arguments.contains("--no-matchmaking") { - StateManager.sharedInstance.enterState(wantedState: .gameState) + StateManager.sharedInstance.stateMachine?.enter(GameState.self) SoundManager.sharedInstance.stopMenuMusic() } else { if GameCenterManager.isAuthenticated { diff --git a/GoldWars/GoldWars/States/EndGameState.swift b/GoldWars/GoldWars/States/EndGameState.swift index da979c6..ed9933a 100644 --- a/GoldWars/GoldWars/States/EndGameState.swift +++ b/GoldWars/GoldWars/States/EndGameState.swift @@ -14,6 +14,14 @@ class EndGameState: GKState { let LOG = OSLog.init(subsystem: "EndGameState", category: "EndGameState") override func isValidNextState(_ stateClass: AnyClass) -> Bool { + if StateManager.sharedInstance.stateMachine?.currentState is EndGameState && stateClass is EndGameState.Type{ + os_log("Already in EndGame State", log: LOG, type: .info) + return false + } + if stateClass is SyncingState.Type || stateClass is GameState.Type{ + os_log("Failed: Entering SyncingState and GameState after EndGameState is not allowed", log: LOG, type: .info) + return false + } return stateClass is MenuState.Type } diff --git a/GoldWars/GoldWars/States/GameState.swift b/GoldWars/GoldWars/States/GameState.swift index f6ec921..320ed8c 100644 --- a/GoldWars/GoldWars/States/GameState.swift +++ b/GoldWars/GoldWars/States/GameState.swift @@ -14,6 +14,14 @@ class GameState: GKState { let LOG = OSLog.init(subsystem: "GameState", category: "GameState") override func isValidNextState(_ stateClass: AnyClass) -> Bool { + if StateManager.sharedInstance.stateMachine?.currentState is GameState && stateClass is GameState.Type{ + os_log("Already in Game State", log: LOG, type: .info) + return false + } + if stateClass is MenuState.Type || stateClass is EndGameState.Type{ + os_log("Failed: Entering MenuState and EndgameState after GameState is not allowed", log: LOG, type: .info) + return false + } return stateClass is SyncingState.Type } diff --git a/GoldWars/GoldWars/States/MenuState.swift b/GoldWars/GoldWars/States/MenuState.swift index 8dacf77..95d3b25 100644 --- a/GoldWars/GoldWars/States/MenuState.swift +++ b/GoldWars/GoldWars/States/MenuState.swift @@ -14,6 +14,14 @@ class MenuState: GKState { let LOG = OSLog.init(subsystem: "MenuState", category: "MenuState") override func isValidNextState(_ stateClass: AnyClass) -> Bool { + if StateManager.sharedInstance.stateMachine?.currentState is MenuState && stateClass is MenuState.Type{ + os_log("Already in Menu State", log: LOG, type: .info) + return false + } + if stateClass is EndGameState.Type{ + os_log("Entering EndGameState after MenuState is not allowed", log: LOG, type: .info) + return false + } if CommandLine.arguments.contains("--no-matchmaking") { return stateClass is GameState.Type } @@ -31,22 +39,19 @@ class MenuState: GKState { os_log("Entered Menu State", log: LOG, type: .info) if (previousState == nil){ - presentFirstViewMenu() + 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) + //TODO: create dev profile or remove on delivery + view.showsFPS = true + view.showsNodeCount = true + } } } override func willExit(to nextState: GKState) { } - func presentFirstViewMenu(){ - 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) - //TODO: create dev profile or remove on delivery - view.showsFPS = true - view.showsNodeCount = true - } - } } diff --git a/GoldWars/GoldWars/States/StateManager.swift b/GoldWars/GoldWars/States/StateManager.swift index 1d90eb9..f29b230 100644 --- a/GoldWars/GoldWars/States/StateManager.swift +++ b/GoldWars/GoldWars/States/StateManager.swift @@ -9,19 +9,11 @@ import GameKit import os -enum StateTypes { - case menuState - case syncingState - case gameState - case endGameState -} - class StateManager{ static let sharedInstance = StateManager() var stateMachine: GKStateMachine? - var currentState: GKState? var menuSc: MenuScene? var gameVC: GameViewController? @@ -37,58 +29,4 @@ class StateManager{ stateMachine = GKStateMachine(states: [menuState, syncingState, playingState, endGameState]) } - - func enterState(wantedState: StateTypes){ - switch wantedState { - case .menuState: - if stateMachine?.enter(MenuState.self) == false { - if stateMachine?.currentState is MenuState{ - os_log("Already in Menu State", log: LOG, type: .info) - }else{ - os_log("Failed entering Menu State", log: LOG, type: .info) - } - }else{ - currentState = stateMachine?.currentState - } - case .gameState: - if stateMachine?.enter(GameState.self) == false { - if stateMachine?.currentState is GameState{ - os_log("Already in Game State", log: LOG, type: .info) - } else { - os_log("Failed entering Game State", log: LOG, type: .info) - } - }else{ - currentState = stateMachine?.currentState - } - case .syncingState: - if stateMachine?.enter(SyncingState.self) == false { - if stateMachine?.currentState is SyncingState{ - os_log("Already in Syncing State", log: LOG, type: .info) - }else{ - os_log("Failed entering Syncing State", log: LOG, type: .info) - } - }else{ - currentState = stateMachine?.currentState - } - case .endGameState: - if stateMachine?.enter(EndGameState.self) == false { - if stateMachine?.currentState is EndGameState{ - os_log("Already in EndGame State", log: LOG, type: .info) - } else{ - os_log("Failed entering EndGame State", log: LOG, type: .info) - } - }else{ - currentState = stateMachine?.currentState - } - } - } - - /*func createStates(){ - let menuSt = MenuState() - let syncingSt = SyncingState() - let playingSt = GameState() - let endGameSt = EndGameState() - - stateMachine = GKStateMachine(states: [menuSt, syncingSt, playingSt, endGameSt]) - }*/ } diff --git a/GoldWars/GoldWars/States/SyncingState.swift b/GoldWars/GoldWars/States/SyncingState.swift index 81d0f53..5cf6f2e 100644 --- a/GoldWars/GoldWars/States/SyncingState.swift +++ b/GoldWars/GoldWars/States/SyncingState.swift @@ -15,6 +15,10 @@ class SyncingState: GKState { var previousState: GKState? override func isValidNextState(_ stateClass: AnyClass) -> Bool { + if StateManager.sharedInstance.stateMachine?.currentState is SyncingState && stateClass is SyncingState.Type{ + os_log("Already in Syncing State", log: LOG, type: .info) + return false + } return stateClass is GameState.Type || stateClass is EndGameState.Type || stateClass is MenuState.Type }