From 9eb06b54fdc7ad0d72ec58dc33d79405e8a8165b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chauntalle=20Schu=CC=88le?= Date: Tue, 2 Jun 2020 17:25:00 +0200 Subject: [PATCH] made smaller changes after review --- .../GoldWars/Components/TimerComponent.swift | 83 ------------------- GoldWars/GoldWars/GameCenterManager.swift | 12 ++- GoldWars/GoldWars/GameViewController.swift | 5 +- .../GoldWars/RoundCalculatorService.swift | 6 +- GoldWars/GoldWars/Scenes/MenuScene.swift | 2 +- GoldWars/GoldWars/States/GameState.swift | 5 +- GoldWars/GoldWars/States/MenuState.swift | 7 +- GoldWars/GoldWars/States/StateManager.swift | 48 +++++------ GoldWars/GoldWars/States/SyncingState.swift | 7 +- 9 files changed, 43 insertions(+), 132 deletions(-) delete mode 100644 GoldWars/GoldWars/Components/TimerComponent.swift diff --git a/GoldWars/GoldWars/Components/TimerComponent.swift b/GoldWars/GoldWars/Components/TimerComponent.swift deleted file mode 100644 index 69db2eb..0000000 --- a/GoldWars/GoldWars/Components/TimerComponent.swift +++ /dev/null @@ -1,83 +0,0 @@ -// -// TimerComponent.swift -// GoldWars -// -// Created by Daniel Steckert on 05.05.20. -// Copyright © 2020 SP2. All rights reserved. -// - -import GameplayKit - -class TimerComponent: GKComponent { - - let labelNode :SKLabelNode - var endTime :Date! - var duration :Double - var isRunning = false - - init(text: String, anchorPoint: CGPoint, duration: TimeInterval) { - self.labelNode = SKLabelNode(text: text) - self.labelNode.fontColor = UIColor.black - self.labelNode.fontSize = CGFloat(45) - self.labelNode.position = CGPoint(x: anchorPoint.x, y: anchorPoint.y - 15) - self.duration = duration + 1 - super.init() - startWithDuration(duration: self.duration) - } - - func startWithDuration(duration: TimeInterval){ - if RoundCalculatorService.sharedInstance.roundNr != 1{ - print("entered gameState in RCS calculate Round") - StateManager.sharedInstance.changeState(wantedState: .gameSt) - } - isRunning = true - endTime = Date().addingTimeInterval(duration) - RoundCalculatorService.sharedInstance.isCalculating = false - } - - func timeLeft() -> Int { - if isRunning { - let remainingSeconds = Int(endTime.timeIntervalSince(Date())) - if(remainingSeconds == 0) { - isRunning = false - } - return remainingSeconds - } - - // if(remainingSeconds < 0){ - // startWithDuration(duration: duration) - // } - return 0 - } - - func isFinished() -> Bool { - return timeLeft() == 0 - } - - func update() { - self.labelNode.text = String(timeLeft()) - - if(isFinished()){ - self.labelNode.text = "Synching" - print("entered syncingSt in Timer") - StateManager.sharedInstance.changeState(wantedState: .syncingSt) - RoundCalculatorService.sharedInstance.resetNumberOfAttacksAndFormats() - if !MultiplayerNetwork.sharedInstance.isSending { - print("timer update 1") - MultiplayerNetwork.sharedInstance.sendPlayerMoves(playerMoves: DataService.sharedInstance.localPlayerMoves) - } - if !RoundCalculatorService.sharedInstance.isCalculating - && DataService.sharedInstance.didReceiveAllData() - && GameCenterManager.sharedInstance.isServer { - print("timer update 2") - RoundCalculatorService.sharedInstance.calculateRound() - } - } - } - - required init?(coder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - -} - diff --git a/GoldWars/GoldWars/GameCenterManager.swift b/GoldWars/GoldWars/GameCenterManager.swift index 09833ab..795aa88 100644 --- a/GoldWars/GoldWars/GameCenterManager.swift +++ b/GoldWars/GoldWars/GameCenterManager.swift @@ -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.changeState(wantedState: .syncingSt) + StateManager.sharedInstance.enterState(wantedState: .syncingState) DataService.sharedInstance.snapshotModel = snapshotModel RoundCalculatorService.sharedInstance.currentRound += 1 entityManager.getHUD()?.setCurrentRound(round: RoundCalculatorService.sharedInstance.currentRound) entityManager.updateSnapshotModel(snapshotModel: snapshotModel) entityManager.getHUD()?.startWithDuration() - StateManager.sharedInstance.changeState(wantedState: .gameSt) + StateManager.sharedInstance.enterState(wantedState: .gameState) } if let mapModel = try? jsonDecoder.decode(MapGenerationModel.self, from: data) { os_log("Peer hat Map erhalten", log: LOG, type: .info) @@ -189,8 +189,6 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG StateManager.sharedInstance.gameSc = scene sendStateToPeers(state: State(state: 2)) os_log("State 2 wurde an Host gesendet", log: LOG, type: .info) - // initIsFinish = true - // os_log("Peer startet Spiel", log: LOG, type: .info) } MultiplayerNetwork.sharedInstance.isSending = false } @@ -240,16 +238,16 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG } func matchmakerViewControllerWasCancelled(_ viewController: GKMatchmakerViewController) { viewController.dismiss(animated: true, completion: nil) - StateManager.sharedInstance.changeState(wantedState: .menuSt) + StateManager.sharedInstance.enterState(wantedState: .menuState) } func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFailWithError error: Error) { viewController.dismiss(animated: true, completion: nil) - StateManager.sharedInstance.changeState(wantedState: .menuSt) + StateManager.sharedInstance.enterState(wantedState: .menuState) } func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFind match: GKMatch) { - StateManager.sharedInstance.changeState(wantedState: .syncingSt) + StateManager.sharedInstance.enterState(wantedState: .syncingState) 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 3d75655..cf1ebdc 100644 --- a/GoldWars/GoldWars/GameViewController.swift +++ b/GoldWars/GoldWars/GameViewController.swift @@ -14,10 +14,9 @@ class GameViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - - StateManager.sharedInstance.createStates() + StateManager.sharedInstance.gameVC = self - StateManager.sharedInstance.changeState(wantedState: .menuSt) + StateManager.sharedInstance.enterState(wantedState: .menuState) GameCenterManager.sharedInstance.viewController = self } diff --git a/GoldWars/GoldWars/RoundCalculatorService.swift b/GoldWars/GoldWars/RoundCalculatorService.swift index adcb5d5..77a2688 100644 --- a/GoldWars/GoldWars/RoundCalculatorService.swift +++ b/GoldWars/GoldWars/RoundCalculatorService.swift @@ -23,13 +23,15 @@ class RoundCalculatorService { let MAX_ROUNDS = 20 var currentRound = 1 var isCalculating = false + var numberOfAttacks = 0 var numberOfOwnUnitMoves = 0 var roundNr = 1 func calculateRound() { - StateManager.sharedInstance.changeState(wantedState: .syncingSt) + StateManager.sharedInstance.enterState(wantedState: .syncingState) os_log("Started calculating Round", log: RoundCalculatorService.LOG, type: .info) + roundNr += 1 isCalculating = true let currentSnapshotModel = DataService.sharedInstance.snapshotModel @@ -171,7 +173,7 @@ class RoundCalculatorService { entityManager.updateSnapshotModel(snapshotModel: currentSnapshotModel!) entityManager.getHUD()?.startWithDuration() os_log("Finished calculating Round", log: RoundCalculatorService.LOG, type: .info) - StateManager.sharedInstance.changeState(wantedState: .gameSt) + StateManager.sharedInstance.enterState(wantedState: .gameState) } func collectBaseSpecificMoves() -> [Int: [String: [PlayerMove]]] { diff --git a/GoldWars/GoldWars/Scenes/MenuScene.swift b/GoldWars/GoldWars/Scenes/MenuScene.swift index 0e7c00d..91b969e 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.changeState(wantedState: .gameSt) + StateManager.sharedInstance.enterState(wantedState: .gameState) SoundManager.sharedInstance.stopMenuMusic() } else { if GameCenterManager.isAuthenticated { diff --git a/GoldWars/GoldWars/States/GameState.swift b/GoldWars/GoldWars/States/GameState.swift index f453cbc..f6ec921 100644 --- a/GoldWars/GoldWars/States/GameState.swift +++ b/GoldWars/GoldWars/States/GameState.swift @@ -12,7 +12,6 @@ import os class GameState: GKState { let LOG = OSLog.init(subsystem: "GameState", category: "GameState") - var roundNr = RoundCalculatorService.sharedInstance.roundNr override func isValidNextState(_ stateClass: AnyClass) -> Bool { return stateClass is SyncingState.Type @@ -20,10 +19,10 @@ class GameState: GKState { override func didEnter(from previousState: GKState?) { os_log("Entered Game State", log: LOG, type: .info) - if CommandLine.arguments.contains("--no-matchmaking"){ + if CommandLine.arguments.contains("--no-matchmaking") { StateManager.sharedInstance.menuSc!.loadScene(scene: GameScene(size: StateManager.sharedInstance.menuSc!.size)) } - if roundNr == 1{ + if RoundCalculatorService.sharedInstance.roundNr == 1 { StateManager.sharedInstance.menuSc!.loadScene(scene: StateManager.sharedInstance.gameSc!) } } diff --git a/GoldWars/GoldWars/States/MenuState.swift b/GoldWars/GoldWars/States/MenuState.swift index 16fb004..8dacf77 100644 --- a/GoldWars/GoldWars/States/MenuState.swift +++ b/GoldWars/GoldWars/States/MenuState.swift @@ -12,7 +12,6 @@ import os class MenuState: GKState { let LOG = OSLog.init(subsystem: "MenuState", category: "MenuState") - var menuEM = EntityManager.menuEMInstance override func isValidNextState(_ stateClass: AnyClass) -> Bool { if CommandLine.arguments.contains("--no-matchmaking") { @@ -22,9 +21,9 @@ class MenuState: GKState { } override func update(deltaTime seconds: TimeInterval) { - if menuEM.entities.count != 0 { - menuEM.getBackground()!.update(deltaTime: seconds) - menuEM.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterManager.isAuthenticated + if EntityManager.menuEMInstance.entities.count != 0 { + EntityManager.menuEMInstance.getBackground()!.update(deltaTime: seconds) + EntityManager.menuEMInstance.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterManager.isAuthenticated } } diff --git a/GoldWars/GoldWars/States/StateManager.swift b/GoldWars/GoldWars/States/StateManager.swift index c505e9d..1d90eb9 100644 --- a/GoldWars/GoldWars/States/StateManager.swift +++ b/GoldWars/GoldWars/States/StateManager.swift @@ -10,10 +10,10 @@ import GameKit import os enum StateTypes { - case menuSt - case syncingSt - case gameSt - case endGameSt + case menuState + case syncingState + case gameState + case endGameState } class StateManager{ @@ -21,9 +21,7 @@ class StateManager{ static let sharedInstance = StateManager() var stateMachine: GKStateMachine? - var wantedState: StateTypes? var currentState: GKState? - var previousState: GKState? var menuSc: MenuScene? var gameVC: GameViewController? @@ -31,13 +29,20 @@ class StateManager{ let LOG = OSLog.init(subsystem: "StateManager", category: "StateManager") - func enterState(){ - previousState = stateMachine?.currentState + init() { + let menuState = MenuState() + let syncingState = SyncingState() + let playingState = GameState() + let endGameState = EndGameState() + stateMachine = GKStateMachine(states: [menuState, syncingState, playingState, endGameState]) + } + + func enterState(wantedState: StateTypes){ switch wantedState { - case .menuSt: + case .menuState: if stateMachine?.enter(MenuState.self) == false { - if previousState is MenuState{ + 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) @@ -45,9 +50,9 @@ class StateManager{ }else{ currentState = stateMachine?.currentState } - case .gameSt: + case .gameState: if stateMachine?.enter(GameState.self) == false { - if previousState is GameState{ + 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) @@ -55,9 +60,9 @@ class StateManager{ }else{ currentState = stateMachine?.currentState } - case .syncingSt: + case .syncingState: if stateMachine?.enter(SyncingState.self) == false { - if previousState is SyncingState{ + 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) @@ -65,9 +70,9 @@ class StateManager{ }else{ currentState = stateMachine?.currentState } - case .endGameSt: + case .endGameState: if stateMachine?.enter(EndGameState.self) == false { - if previousState is EndGameState{ + 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) @@ -75,22 +80,15 @@ class StateManager{ }else{ currentState = stateMachine?.currentState } - default: - os_log("Allowed State was found nil, can't enter any State", log: LOG, type: .info) } } - func createStates(){ + /*func createStates(){ let menuSt = MenuState() let syncingSt = SyncingState() let playingSt = GameState() let endGameSt = EndGameState() stateMachine = GKStateMachine(states: [menuSt, syncingSt, playingSt, endGameSt]) - } - - func changeState(wantedState: StateTypes){ - self.wantedState = wantedState - enterState() - } + }*/ } diff --git a/GoldWars/GoldWars/States/SyncingState.swift b/GoldWars/GoldWars/States/SyncingState.swift index e2f6773..81d0f53 100644 --- a/GoldWars/GoldWars/States/SyncingState.swift +++ b/GoldWars/GoldWars/States/SyncingState.swift @@ -12,7 +12,6 @@ import os class SyncingState: GKState { let LOG = OSLog.init(subsystem: "SyncingState", category: "SyncingState") - var menuEM = EntityManager.menuEMInstance var previousState: GKState? override func isValidNextState(_ stateClass: AnyClass) -> Bool { @@ -26,9 +25,9 @@ class SyncingState: GKState { override func update(deltaTime seconds: TimeInterval) { if previousState is MenuState { - if menuEM.entities.count != 0 { - menuEM.getBackground()!.update(deltaTime: seconds) - menuEM.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterManager.isAuthenticated + if EntityManager.menuEMInstance.entities.count != 0 { + EntityManager.menuEMInstance.getBackground()!.update(deltaTime: seconds) + EntityManager.menuEMInstance.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterManager.isAuthenticated } } }