made smaller changes after review
This commit is contained in:
parent
c5867f12e3
commit
9eb06b54fd
@ -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")
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -15,9 +15,8 @@ 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
|
||||
}
|
||||
|
@ -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]]] {
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
@ -23,7 +22,7 @@ class GameState: GKState {
|
||||
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!)
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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()
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user