made smaller changes after review

This commit is contained in:
Chauntalle Schüle 2020-06-02 17:25:00 +02:00
parent c5867f12e3
commit 9eb06b54fd
9 changed files with 43 additions and 132 deletions

View File

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

View File

@ -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.changeState(wantedState: .syncingSt) StateManager.sharedInstance.enterState(wantedState: .syncingState)
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.changeState(wantedState: .gameSt) StateManager.sharedInstance.enterState(wantedState: .gameState)
} }
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)
@ -189,8 +189,6 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG
StateManager.sharedInstance.gameSc = 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
// os_log("Peer startet Spiel", log: LOG, type: .info)
} }
MultiplayerNetwork.sharedInstance.isSending = false MultiplayerNetwork.sharedInstance.isSending = false
} }
@ -240,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.changeState(wantedState: .menuSt) StateManager.sharedInstance.enterState(wantedState: .menuState)
} }
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) StateManager.sharedInstance.enterState(wantedState: .menuState)
} }
func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFind match: GKMatch) { func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFind match: GKMatch) {
StateManager.sharedInstance.changeState(wantedState: .syncingSt) StateManager.sharedInstance.enterState(wantedState: .syncingState)
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,9 +15,8 @@ class GameViewController: UIViewController {
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
StateManager.sharedInstance.createStates()
StateManager.sharedInstance.gameVC = self StateManager.sharedInstance.gameVC = self
StateManager.sharedInstance.changeState(wantedState: .menuSt) StateManager.sharedInstance.enterState(wantedState: .menuState)
GameCenterManager.sharedInstance.viewController = self GameCenterManager.sharedInstance.viewController = self
} }

View File

@ -23,13 +23,15 @@ class RoundCalculatorService {
let MAX_ROUNDS = 20 let MAX_ROUNDS = 20
var currentRound = 1 var currentRound = 1
var isCalculating = false var isCalculating = false
var numberOfAttacks = 0 var numberOfAttacks = 0
var numberOfOwnUnitMoves = 0 var numberOfOwnUnitMoves = 0
var roundNr = 1 var roundNr = 1
func calculateRound() { func calculateRound() {
StateManager.sharedInstance.changeState(wantedState: .syncingSt) StateManager.sharedInstance.enterState(wantedState: .syncingState)
os_log("Started calculating Round", log: RoundCalculatorService.LOG, type: .info) os_log("Started calculating Round", log: RoundCalculatorService.LOG, type: .info)
roundNr += 1 roundNr += 1
isCalculating = true isCalculating = true
let currentSnapshotModel = DataService.sharedInstance.snapshotModel let currentSnapshotModel = DataService.sharedInstance.snapshotModel
@ -171,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.changeState(wantedState: .gameSt) StateManager.sharedInstance.enterState(wantedState: .gameState)
} }
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.changeState(wantedState: .gameSt) StateManager.sharedInstance.enterState(wantedState: .gameState)
SoundManager.sharedInstance.stopMenuMusic() SoundManager.sharedInstance.stopMenuMusic()
} else { } else {
if GameCenterManager.isAuthenticated { if GameCenterManager.isAuthenticated {

View File

@ -12,7 +12,6 @@ import os
class GameState: GKState { class GameState: GKState {
let LOG = OSLog.init(subsystem: "GameState", category: "GameState") let LOG = OSLog.init(subsystem: "GameState", category: "GameState")
var roundNr = RoundCalculatorService.sharedInstance.roundNr
override func isValidNextState(_ stateClass: AnyClass) -> Bool { override func isValidNextState(_ stateClass: AnyClass) -> Bool {
return stateClass is SyncingState.Type return stateClass is SyncingState.Type
@ -23,7 +22,7 @@ class GameState: GKState {
if CommandLine.arguments.contains("--no-matchmaking") { if CommandLine.arguments.contains("--no-matchmaking") {
StateManager.sharedInstance.menuSc!.loadScene(scene: GameScene(size: StateManager.sharedInstance.menuSc!.size)) 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!) StateManager.sharedInstance.menuSc!.loadScene(scene: StateManager.sharedInstance.gameSc!)
} }
} }

View File

@ -12,7 +12,6 @@ import os
class MenuState: GKState { class MenuState: GKState {
let LOG = OSLog.init(subsystem: "MenuState", category: "MenuState") let LOG = OSLog.init(subsystem: "MenuState", category: "MenuState")
var menuEM = EntityManager.menuEMInstance
override func isValidNextState(_ stateClass: AnyClass) -> Bool { override func isValidNextState(_ stateClass: AnyClass) -> Bool {
if CommandLine.arguments.contains("--no-matchmaking") { if CommandLine.arguments.contains("--no-matchmaking") {
@ -22,9 +21,9 @@ class MenuState: GKState {
} }
override func update(deltaTime seconds: TimeInterval) { override func update(deltaTime seconds: TimeInterval) {
if menuEM.entities.count != 0 { if EntityManager.menuEMInstance.entities.count != 0 {
menuEM.getBackground()!.update(deltaTime: seconds) EntityManager.menuEMInstance.getBackground()!.update(deltaTime: seconds)
menuEM.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterManager.isAuthenticated EntityManager.menuEMInstance.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterManager.isAuthenticated
} }
} }

View File

@ -10,10 +10,10 @@ import GameKit
import os import os
enum StateTypes { enum StateTypes {
case menuSt case menuState
case syncingSt case syncingState
case gameSt case gameState
case endGameSt case endGameState
} }
class StateManager{ class StateManager{
@ -21,9 +21,7 @@ class StateManager{
static let sharedInstance = StateManager() static let sharedInstance = StateManager()
var stateMachine: GKStateMachine? var stateMachine: GKStateMachine?
var wantedState: StateTypes?
var currentState: GKState? var currentState: GKState?
var previousState: GKState?
var menuSc: MenuScene? var menuSc: MenuScene?
var gameVC: GameViewController? var gameVC: GameViewController?
@ -31,13 +29,20 @@ class StateManager{
let LOG = OSLog.init(subsystem: "StateManager", category: "StateManager") let LOG = OSLog.init(subsystem: "StateManager", category: "StateManager")
func enterState(){ init() {
previousState = stateMachine?.currentState 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 { switch wantedState {
case .menuSt: case .menuState:
if stateMachine?.enter(MenuState.self) == false { 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) os_log("Already in Menu State", log: LOG, type: .info)
}else{ }else{
os_log("Failed entering Menu State", log: LOG, type: .info) os_log("Failed entering Menu State", log: LOG, type: .info)
@ -45,9 +50,9 @@ class StateManager{
}else{ }else{
currentState = stateMachine?.currentState currentState = stateMachine?.currentState
} }
case .gameSt: case .gameState:
if stateMachine?.enter(GameState.self) == false { 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) os_log("Already in Game State", log: LOG, type: .info)
} else { } else {
os_log("Failed entering Game State", log: LOG, type: .info) os_log("Failed entering Game State", log: LOG, type: .info)
@ -55,9 +60,9 @@ class StateManager{
}else{ }else{
currentState = stateMachine?.currentState currentState = stateMachine?.currentState
} }
case .syncingSt: case .syncingState:
if stateMachine?.enter(SyncingState.self) == false { 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) os_log("Already in Syncing State", log: LOG, type: .info)
}else{ }else{
os_log("Failed entering Syncing State", log: LOG, type: .info) os_log("Failed entering Syncing State", log: LOG, type: .info)
@ -65,9 +70,9 @@ class StateManager{
}else{ }else{
currentState = stateMachine?.currentState currentState = stateMachine?.currentState
} }
case .endGameSt: case .endGameState:
if stateMachine?.enter(EndGameState.self) == false { 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) os_log("Already in EndGame State", log: LOG, type: .info)
} else{ } else{
os_log("Failed entering EndGame State", log: LOG, type: .info) os_log("Failed entering EndGame State", log: LOG, type: .info)
@ -75,22 +80,15 @@ class StateManager{
}else{ }else{
currentState = stateMachine?.currentState 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 menuSt = MenuState()
let syncingSt = SyncingState() let syncingSt = SyncingState()
let playingSt = GameState() let playingSt = GameState()
let endGameSt = EndGameState() let endGameSt = EndGameState()
stateMachine = GKStateMachine(states: [menuSt, syncingSt, playingSt, endGameSt]) stateMachine = GKStateMachine(states: [menuSt, syncingSt, playingSt, endGameSt])
} }*/
func changeState(wantedState: StateTypes){
self.wantedState = wantedState
enterState()
}
} }

View File

@ -12,7 +12,6 @@ import os
class SyncingState: GKState { class SyncingState: GKState {
let LOG = OSLog.init(subsystem: "SyncingState", category: "SyncingState") let LOG = OSLog.init(subsystem: "SyncingState", category: "SyncingState")
var menuEM = EntityManager.menuEMInstance
var previousState: GKState? var previousState: GKState?
override func isValidNextState(_ stateClass: AnyClass) -> Bool { override func isValidNextState(_ stateClass: AnyClass) -> Bool {
@ -26,9 +25,9 @@ class SyncingState: GKState {
override func update(deltaTime seconds: TimeInterval) { override func update(deltaTime seconds: TimeInterval) {
if previousState is MenuState { if previousState is MenuState {
if menuEM.entities.count != 0 { if EntityManager.menuEMInstance.entities.count != 0 {
menuEM.getBackground()!.update(deltaTime: seconds) EntityManager.menuEMInstance.getBackground()!.update(deltaTime: seconds)
menuEM.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterManager.isAuthenticated EntityManager.menuEMInstance.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterManager.isAuthenticated
} }
} }
} }