adding bigger changes after review, deleted enum und methods enter&changeState

This commit is contained in:
Chauntalle Schüle 2020-06-02 21:38:23 +02:00
parent 9eb06b54fd
commit aee6ec1ac1
9 changed files with 48 additions and 85 deletions

View File

@ -148,13 +148,13 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG
sendStateToPeers(state: State(state: 3)) sendStateToPeers(state: State(state: 3))
initIsFinish = true initIsFinish = true
print("entered gameSt in case 2 match") 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) os_log("Spiel startet", log: LOG, type: .info)
case 3: case 3:
os_log("State 3 erhalten", log: LOG, type: .info) os_log("State 3 erhalten", log: LOG, type: .info)
initIsFinish = true initIsFinish = true
print("entered gameSt in case 3 match") 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) os_log("Spiel startet", log: LOG, type: .info)
case 4: case 4:
os_log("State 4 erhalten, Peer hat verloren", log: LOG, type: .info) 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) DataService.sharedInstance.addRemotePlayerMoves(playerName: player.displayName, localRoundData: roundData)
} }
if let snapshotModel = try? jsonDecoder.decode(SnapshotModel.self, from: data) { 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 DataService.sharedInstance.snapshotModel = snapshotModel
RoundCalculatorService.sharedInstance.currentRound += 1 RoundCalculatorService.sharedInstance.currentRound += 1
entityManager.getHUD()?.setCurrentRound(round: RoundCalculatorService.sharedInstance.currentRound) entityManager.getHUD()?.setCurrentRound(round: RoundCalculatorService.sharedInstance.currentRound)
entityManager.updateSnapshotModel(snapshotModel: snapshotModel) entityManager.updateSnapshotModel(snapshotModel: snapshotModel)
entityManager.getHUD()?.startWithDuration() entityManager.getHUD()?.startWithDuration()
StateManager.sharedInstance.enterState(wantedState: .gameState) StateManager.sharedInstance.stateMachine?.enter(GameState.self)
} }
if let mapModel = try? jsonDecoder.decode(MapGenerationModel.self, from: data) { if let mapModel = try? jsonDecoder.decode(MapGenerationModel.self, from: data) {
os_log("Peer hat Map erhalten", log: LOG, type: .info) os_log("Peer hat Map erhalten", log: LOG, type: .info)
@ -238,16 +238,16 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG
} }
func matchmakerViewControllerWasCancelled(_ viewController: GKMatchmakerViewController) { func matchmakerViewControllerWasCancelled(_ viewController: GKMatchmakerViewController) {
viewController.dismiss(animated: true, completion: nil) viewController.dismiss(animated: true, completion: nil)
StateManager.sharedInstance.enterState(wantedState: .menuState) StateManager.sharedInstance.stateMachine?.enter(MenuState.self)
} }
func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFailWithError error: Error) { func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFailWithError error: Error) {
viewController.dismiss(animated: true, completion: nil) viewController.dismiss(animated: true, completion: nil)
StateManager.sharedInstance.enterState(wantedState: .menuState) StateManager.sharedInstance.stateMachine?.enter(MenuState.self)
} }
func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFind match: GKMatch) { func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFind match: GKMatch) {
StateManager.sharedInstance.enterState(wantedState: .syncingState) StateManager.sharedInstance.stateMachine?.enter(SyncingState.self)
viewController.dismiss(animated: true, completion: nil) viewController.dismiss(animated: true, completion: nil)
myMatch = match myMatch = match
if !isMatchStarted && match.expectedPlayerCount == 0 { if !isMatchStarted && match.expectedPlayerCount == 0 {

View File

@ -16,7 +16,7 @@ class GameViewController: UIViewController {
super.viewDidLoad() super.viewDidLoad()
StateManager.sharedInstance.gameVC = self StateManager.sharedInstance.gameVC = self
StateManager.sharedInstance.enterState(wantedState: .menuState) StateManager.sharedInstance.stateMachine?.enter(MenuState.self)
GameCenterManager.sharedInstance.viewController = self GameCenterManager.sharedInstance.viewController = self
} }

View File

@ -29,7 +29,7 @@ class RoundCalculatorService {
var roundNr = 1 var roundNr = 1
func calculateRound() { func calculateRound() {
StateManager.sharedInstance.enterState(wantedState: .syncingState) StateManager.sharedInstance.stateMachine?.enter(SyncingState.self)
os_log("Started calculating Round", log: RoundCalculatorService.LOG, type: .info) os_log("Started calculating Round", log: RoundCalculatorService.LOG, type: .info)
roundNr += 1 roundNr += 1
@ -173,7 +173,7 @@ class RoundCalculatorService {
entityManager.updateSnapshotModel(snapshotModel: currentSnapshotModel!) entityManager.updateSnapshotModel(snapshotModel: currentSnapshotModel!)
entityManager.getHUD()?.startWithDuration() entityManager.getHUD()?.startWithDuration()
os_log("Finished calculating Round", log: RoundCalculatorService.LOG, type: .info) 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]]] { func collectBaseSpecificMoves() -> [Int: [String: [PlayerMove]]] {

View File

@ -25,7 +25,7 @@ class MenuScene: SKScene {
position: CGPoint(x: midX, y: midY), position: CGPoint(x: midX, y: midY),
onButtonPress: { onButtonPress: {
if CommandLine.arguments.contains("--no-matchmaking") { if CommandLine.arguments.contains("--no-matchmaking") {
StateManager.sharedInstance.enterState(wantedState: .gameState) StateManager.sharedInstance.stateMachine?.enter(GameState.self)
SoundManager.sharedInstance.stopMenuMusic() SoundManager.sharedInstance.stopMenuMusic()
} else { } else {
if GameCenterManager.isAuthenticated { if GameCenterManager.isAuthenticated {

View File

@ -14,6 +14,14 @@ class EndGameState: GKState {
let LOG = OSLog.init(subsystem: "EndGameState", category: "EndGameState") let LOG = OSLog.init(subsystem: "EndGameState", category: "EndGameState")
override func isValidNextState(_ stateClass: AnyClass) -> Bool { 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 return stateClass is MenuState.Type
} }

View File

@ -14,6 +14,14 @@ class GameState: GKState {
let LOG = OSLog.init(subsystem: "GameState", category: "GameState") let LOG = OSLog.init(subsystem: "GameState", category: "GameState")
override func isValidNextState(_ stateClass: AnyClass) -> Bool { 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 return stateClass is SyncingState.Type
} }

View File

@ -14,6 +14,14 @@ class MenuState: GKState {
let LOG = OSLog.init(subsystem: "MenuState", category: "MenuState") let LOG = OSLog.init(subsystem: "MenuState", category: "MenuState")
override func isValidNextState(_ stateClass: AnyClass) -> Bool { 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") { if CommandLine.arguments.contains("--no-matchmaking") {
return stateClass is GameState.Type return stateClass is GameState.Type
} }
@ -31,14 +39,6 @@ class MenuState: GKState {
os_log("Entered Menu State", log: LOG, type: .info) os_log("Entered Menu State", log: LOG, type: .info)
if (previousState == nil){ if (previousState == nil){
presentFirstViewMenu()
}
}
override func willExit(to nextState: GKState) {
}
func presentFirstViewMenu(){
if let view = StateManager.sharedInstance.gameVC!.view as! SKView? { if let view = StateManager.sharedInstance.gameVC!.view as! SKView? {
let scene = MenuScene(size: StateManager.sharedInstance.gameVC!.view.bounds.size) let scene = MenuScene(size: StateManager.sharedInstance.gameVC!.view.bounds.size)
EntityManager.menuEMInstance.setScene(scene: scene) EntityManager.menuEMInstance.setScene(scene: scene)
@ -49,4 +49,9 @@ class MenuState: GKState {
view.showsNodeCount = true view.showsNodeCount = true
} }
} }
}
override func willExit(to nextState: GKState) {
}
} }

View File

@ -9,19 +9,11 @@
import GameKit import GameKit
import os import os
enum StateTypes {
case menuState
case syncingState
case gameState
case endGameState
}
class StateManager{ class StateManager{
static let sharedInstance = StateManager() static let sharedInstance = StateManager()
var stateMachine: GKStateMachine? var stateMachine: GKStateMachine?
var currentState: GKState?
var menuSc: MenuScene? var menuSc: MenuScene?
var gameVC: GameViewController? var gameVC: GameViewController?
@ -37,58 +29,4 @@ class StateManager{
stateMachine = GKStateMachine(states: [menuState, syncingState, playingState, endGameState]) 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])
}*/
} }

View File

@ -15,6 +15,10 @@ class SyncingState: GKState {
var previousState: GKState? var previousState: GKState?
override func isValidNextState(_ stateClass: AnyClass) -> Bool { 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 return stateClass is GameState.Type || stateClass is EndGameState.Type || stateClass is MenuState.Type
} }