menuSt, syncSt and gameSt now correctly triggered even in MM, menuSc and gameSc loaded in states, still need to find trigger for syncSt after finishing one timer cycle / round

This commit is contained in:
Chauntalle Schüle 2020-06-01 02:32:38 +02:00
parent f4847e79d7
commit b23071bb08
8 changed files with 45 additions and 28 deletions

View File

@ -51,7 +51,6 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG
override init() { override init() {
super.init() super.init()
// StateManager.stateManager.changeState(wantedState: .syncingSt, menuSc: nil, gameVC: nil)
localPlayer.register(self) localPlayer.register(self)
authUser(); authUser();
localPlayerRandomNumber = RandomNumber() localPlayerRandomNumber = RandomNumber()
@ -148,10 +147,12 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG
os_log("State 2 erhalten", log: LOG, type: .info) os_log("State 2 erhalten", log: LOG, type: .info)
sendStateToPeers(state: State(state: 3)) sendStateToPeers(state: State(state: 3))
initIsFinish = true initIsFinish = true
StateManager.sharedInstance.changeState(wantedState: .gameSt)
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
StateManager.sharedInstance.changeState(wantedState: .gameSt)
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)
@ -181,6 +182,7 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG
os_log("Map model wurde gesetzt", log: LOG, type: .info) os_log("Map model wurde gesetzt", log: LOG, type: .info)
GameCenterManager.sharedInstance.isMatchStarted = true GameCenterManager.sharedInstance.isMatchStarted = true
self.gameScene = scene self.gameScene = scene
StateManager.sharedInstance.gameSc = scene
sendStateToPeers(state: State(state: 2)) sendStateToPeers(state: State(state: 2))
os_log("State 2 wurde an Host gesendet", log: LOG, type: .info) os_log("State 2 wurde an Host gesendet", log: LOG, type: .info)
// initIsFinish = true // initIsFinish = true
@ -193,6 +195,7 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG
self.gameScene = GameScene(size: self.menusc!.size) self.gameScene = GameScene(size: self.menusc!.size)
let mapModel = MapFactory(scene: self.gameScene!, entityManager: entityManager).load() let mapModel = MapFactory(scene: self.gameScene!, entityManager: entityManager).load()
os_log("Map wurde erstellt", log: LOG, type: .info) os_log("Map wurde erstellt", log: LOG, type: .info)
StateManager.sharedInstance.gameSc = gameScene
MultiplayerNetwork.sharedInstance.sendMapModelToPlayers(mapModel: mapModel) MultiplayerNetwork.sharedInstance.sendMapModelToPlayers(mapModel: mapModel)
os_log("Map wurde an Peer gesendet", log: LOG, type: .info) os_log("Map wurde an Peer gesendet", log: LOG, type: .info)
DataService.sharedInstance.setSnapshotModel(snapshotModel: entityManager.getSnapshotModel()) DataService.sharedInstance.setSnapshotModel(snapshotModel: entityManager.getSnapshotModel())
@ -233,14 +236,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.changeState(wantedState: .menuSt)
} }
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.changeState(wantedState: .menuSt)
} }
func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFind match: GKMatch) { 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) viewController.dismiss(animated: true, completion: nil)
myMatch = match myMatch = match
if !isMatchStarted && match.expectedPlayerCount == 0 { if !isMatchStarted && match.expectedPlayerCount == 0 {

View File

@ -15,8 +15,10 @@ class GameViewController: UIViewController {
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
StateManager.stateManager.createStates() StateManager.sharedInstance.createStates()
StateManager.stateManager.changeState(wantedState: .menuSt, menuSc: nil, gameVC: self) StateManager.sharedInstance.gameVC = self
StateManager.sharedInstance.changeState(wantedState: .menuSt)
GameCenterManager.sharedInstance.viewController = self GameCenterManager.sharedInstance.viewController = self
} }

View File

@ -72,7 +72,7 @@ class GameScene: SKScene{
} }
override func update(_ currentTime: TimeInterval) { override func update(_ currentTime: TimeInterval) {
StateManager.stateManager.stateMachine!.update(deltaTime: currentTime) StateManager.sharedInstance.stateMachine!.update(deltaTime: currentTime)
/* entityManager.getBackground()?.update(deltaTime: currentTime) /* entityManager.getBackground()?.update(deltaTime: currentTime)
entityManager.getHUD()?.component(ofType: TimerComponent.self)?.update() entityManager.getHUD()?.component(ofType: TimerComponent.self)?.update()
*/ */

View File

@ -15,6 +15,7 @@ class MenuScene: SKScene {
override func sceneDidLoad() { override func sceneDidLoad() {
GameCenterManager.sharedInstance.menusc = self GameCenterManager.sharedInstance.menusc = self
StateManager.sharedInstance.menuSc = self
entityManager.setScene(scene: self) entityManager.setScene(scene: self)
let midX = self.size.width / 2 let midX = self.size.width / 2
let midY = self.size.height / 2 let midY = self.size.height / 2
@ -24,8 +25,8 @@ 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.stateManager.changeState(wantedState: .gameSt, menuSc: self, gameVC: nil) StateManager.sharedInstance.changeState(wantedState: .gameSt)
//self.loadScene(scene: GameScene(size: self.size)) /*self.loadScene(scene: GameScene(size: self.size))*/
SoundManager.sharedInstance.stopMenuMusic() SoundManager.sharedInstance.stopMenuMusic()
} else { } else {
if GameCenterManager.isAuthenticated { if GameCenterManager.isAuthenticated {
@ -69,7 +70,7 @@ class MenuScene: SKScene {
} }
override func update(_ currentTime: TimeInterval) { override func update(_ currentTime: TimeInterval) {
StateManager.stateManager.stateMachine!.update(deltaTime: currentTime) StateManager.sharedInstance.stateMachine!.update(deltaTime: currentTime)
/* /*
if entityManager.entities.count != 0 { if entityManager.entities.count != 0 {
entityManager.getBackground()!.update(deltaTime: currentTime) entityManager.getBackground()!.update(deltaTime: currentTime)

View File

@ -20,10 +20,11 @@ class GameState: GKState {
override func didEnter(from previousState: GKState?) { override func didEnter(from previousState: GKState?) {
os_log("Entered Game State", log: LOG, type: .info) os_log("Entered Game State", log: LOG, type: .info)
if CommandLine.arguments.contains("--no-matchmaking") { 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) { override func update(deltaTime seconds: TimeInterval) {
EntityManager.gameEMInstance.getBackground()?.update(deltaTime: seconds) EntityManager.gameEMInstance.getBackground()?.update(deltaTime: seconds)
EntityManager.gameEMInstance.getHUD()?.component(ofType: TimerComponent.self)?.update() EntityManager.gameEMInstance.getHUD()?.component(ofType: TimerComponent.self)?.update()

View File

@ -27,9 +27,11 @@ class MenuState: GKState {
menuEM.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterManager.isAuthenticated menuEM.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterManager.isAuthenticated
} }
if GameCenterManager.sharedInstance.initIsFinish { // if GameCenterManager.sharedInstance.initIsFinish {
StateManager.stateManager.menuSc!.loadScene(scene: GameCenterManager.sharedInstance.gameScene!) // StateManager.sharedInstance.menuSc!.loadScene(scene: GameCenterManager.sharedInstance.gameScene!)
} // }
} }
override func didEnter(from previousState: GKState?) { override func didEnter(from previousState: GKState?) {
@ -44,8 +46,8 @@ class MenuState: GKState {
} }
func presentFirstViewMenu(){ func presentFirstViewMenu(){
if let view = StateManager.stateManager.gameVC!.view as! SKView? { if let view = StateManager.sharedInstance.gameVC!.view as! SKView? {
let scene = MenuScene(size: StateManager.stateManager.gameVC!.view.bounds.size) let scene = MenuScene(size: StateManager.sharedInstance.gameVC!.view.bounds.size)
EntityManager.menuEMInstance.setScene(scene: scene) EntityManager.menuEMInstance.setScene(scene: scene)
scene.scaleMode = .aspectFill scene.scaleMode = .aspectFill
view.presentScene(scene) view.presentScene(scene)

View File

@ -11,7 +11,7 @@ import os
class StateManager{ class StateManager{
static let stateManager = StateManager() static let sharedInstance = StateManager()
var stateMachine: GKStateMachine? var stateMachine: GKStateMachine?
var wantedState: StateTypes? var wantedState: StateTypes?
@ -32,8 +32,9 @@ class StateManager{
if stateMachine?.enter(MenuState.self) == false { if stateMachine?.enter(MenuState.self) == false {
if previousState is MenuState{ if previousState is MenuState{
os_log("Already in Menu State", log: LOG, type: .info) 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{ }else{
currentState = stateMachine?.currentState currentState = stateMachine?.currentState
} }
@ -41,8 +42,9 @@ class StateManager{
if stateMachine?.enter(GameState.self) == false { if stateMachine?.enter(GameState.self) == false {
if previousState is GameState{ if previousState is GameState{
os_log("Already in Game State", log: LOG, type: .info) 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{ }else{
currentState = stateMachine?.currentState currentState = stateMachine?.currentState
} }
@ -50,8 +52,9 @@ class StateManager{
if stateMachine?.enter(SyncingState.self) == false { if stateMachine?.enter(SyncingState.self) == false {
if previousState is SyncingState{ if previousState is SyncingState{
os_log("Already in Syncing State", log: LOG, type: .info) 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{ }else{
currentState = stateMachine?.currentState currentState = stateMachine?.currentState
} }
@ -59,8 +62,9 @@ class StateManager{
if stateMachine?.enter(EndGameState.self) == false { if stateMachine?.enter(EndGameState.self) == false {
if previousState is EndGameState{ if previousState is EndGameState{
os_log("Already in EndGame State", log: LOG, type: .info) 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{ }else{
currentState = stateMachine?.currentState currentState = stateMachine?.currentState
} }
@ -78,10 +82,15 @@ class StateManager{
stateMachine = GKStateMachine(states: [menuSt, syncingSt, playingSt, endGameSt]) 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.wantedState = wantedState
self.menuSc = menuSc
self.gameVC = gameVC
enterState() enterState()
} }
} }

View File

@ -16,7 +16,7 @@ class SyncingState: GKState {
var previousState: GKState? var previousState: GKState?
override func isValidNextState(_ stateClass: AnyClass) -> Bool { 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?) { override func didEnter(from previousState: GKState?) {
@ -30,12 +30,9 @@ class SyncingState: GKState {
menuEM.getBackground()!.update(deltaTime: seconds) menuEM.getBackground()!.update(deltaTime: seconds)
menuEM.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterManager.isAuthenticated 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) { override func willExit(to nextState: GKState) {
} }
} }