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))
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 {

View File

@ -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
}

View File

@ -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]]] {

View File

@ -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 {

View File

@ -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
}

View File

@ -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
}

View File

@ -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,14 +39,6 @@ class MenuState: GKState {
os_log("Entered Menu State", log: LOG, type: .info)
if (previousState == nil){
presentFirstViewMenu()
}
}
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)
@ -50,3 +50,8 @@ class MenuState: GKState {
}
}
}
override func willExit(to nextState: GKState) {
}
}

View File

@ -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])
}*/
}

View File

@ -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
}