added Statemanager with Singleton, used to create and enter states, already with logging if entering state is un/successful

This commit is contained in:
Chauntalle Schüle 2020-05-29 01:08:25 +02:00
parent 767548cb82
commit 3bbae779af
3 changed files with 56 additions and 41 deletions

View File

@ -111,6 +111,8 @@
AB21D7D4246C748A00B09CBA /* MapFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapFactory.swift; sourceTree = "<group>"; }; AB21D7D4246C748A00B09CBA /* MapFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapFactory.swift; sourceTree = "<group>"; };
ABA03D9F244BD54F00A66916 /* Base.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Base.swift; sourceTree = "<group>"; }; ABA03D9F244BD54F00A66916 /* Base.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Base.swift; sourceTree = "<group>"; };
ABC0C3722481509300387B8F /* MapUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapUtils.swift; sourceTree = "<group>"; }; ABC0C3722481509300387B8F /* MapUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapUtils.swift; sourceTree = "<group>"; };
AE6BB1BF248030720063ECAE /* StateTypes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StateTypes.swift; sourceTree = "<group>"; };
AE6BB1C124807AC00063ECAE /* StateManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StateManager.swift; sourceTree = "<group>"; };
C04783ED2468583F004961FB /* intro-music.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = "intro-music.mp3"; sourceTree = "<group>"; }; C04783ED2468583F004961FB /* intro-music.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = "intro-music.mp3"; sourceTree = "<group>"; };
C04783EF24685995004961FB /* SettingsScene.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsScene.swift; sourceTree = "<group>"; }; C04783EF24685995004961FB /* SettingsScene.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsScene.swift; sourceTree = "<group>"; };
C05BB9C3247D890C00411249 /* SliderComponent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SliderComponent.swift; sourceTree = "<group>"; }; C05BB9C3247D890C00411249 /* SliderComponent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SliderComponent.swift; sourceTree = "<group>"; };
@ -182,6 +184,8 @@
9E0E459624796262009817A6 /* GameCenterManager.swift */, 9E0E459624796262009817A6 /* GameCenterManager.swift */,
C04783EF24685995004961FB /* SettingsScene.swift */, C04783EF24685995004961FB /* SettingsScene.swift */,
3EAD889424801B6A0048A10A /* RoundTimer.swift */, 3EAD889424801B6A0048A10A /* RoundTimer.swift */,
AE6BB1BF248030720063ECAE /* StateTypes.swift */,
AE6BB1C124807AC00063ECAE /* StateManager.swift */,
); );
path = GoldWars; path = GoldWars;
sourceTree = "<group>"; sourceTree = "<group>";
@ -406,6 +410,7 @@
9EC2FBA72476B1EC00ABF11F /* PlayerInfoComponent.swift in Sources */, 9EC2FBA72476B1EC00ABF11F /* PlayerInfoComponent.swift in Sources */,
AE6BB1C0248030720063ECAE /* StateTypes.swift in Sources */, AE6BB1C0248030720063ECAE /* StateTypes.swift in Sources */,
9EEDE02D246FCD770096C735 /* SpinningLogoEntity.swift in Sources */, 9EEDE02D246FCD770096C735 /* SpinningLogoEntity.swift in Sources */,
AE6BB1C224807AC00063ECAE /* StateManager.swift in Sources */,
9E174C86245DD91500209FF0 /* ButtonComponent.swift in Sources */, 9E174C86245DD91500209FF0 /* ButtonComponent.swift in Sources */,
11036113244B3E30008610AF /* MenuScene.swift in Sources */, 11036113244B3E30008610AF /* MenuScene.swift in Sources */,
C099579C246C5E5C0016AA22 /* DataService.swift in Sources */, C099579C246C5E5C0016AA22 /* DataService.swift in Sources */,

View File

@ -0,0 +1,51 @@
//
// StateManager.swift
// GoldWars
//
// Created by Chauntalle Schüle on 29.05.20.
// Copyright © 2020 SP2. All rights reserved.
//
import GameKit
import os
class StateManager{
static let stateManager = StateManager()
var stateMachine: GKStateMachine?
var allowedState: StateTypes?
let LOG = OSLog.init(subsystem: "StateManager", category: "StateManager")
func enterState(){
switch allowedState {
case .menuSt:
if stateMachine?.enter(MenuState.self) == false {
os_log("Failed entering Menu State", log: LOG, type: .info)
}
case .gameSt:
if stateMachine?.enter(GameState.self) == false {
os_log("Failed entering Game State", log: LOG, type: .info)
}
case .syncingSt:
if stateMachine?.enter(SyncingState.self) == false {
os_log("Failed entering Syncing State", log: LOG, type: .info)
}
case .endGameSt:
if stateMachine?.enter(EndGameState.self) == false {
os_log("Failed entering EndGame State", log: LOG, type: .info)
}
default:
os_log("Allowed State was found nil, can't enter any State", log: LOG, type: .info)
}
}
func createStates(){
let menuSt = MenuState()
let syncingSt = SyncingState()
let playingSt = GameState()
let endGameSt = EndGameState()
stateMachine = GKStateMachine(states: [menuSt, syncingSt, playingSt, endGameSt])
}
}

View File

@ -16,47 +16,6 @@ enum StateTypes {
case endGameSt case endGameSt
} }
class StateManager{
static let stateManager = StateManager()
var stateMachine: GKStateMachine?
var allowedState: StateTypes?
let LOG = OSLog.init(subsystem: "StateManager", category: "StateManager")
func enterState(){
switch allowedState {
case .menuSt:
if stateMachine?.enter(MenuState.self) == false {
os_log("Failed entering Menu State", log: LOG, type: .info)
}
case .gameSt:
if stateMachine?.enter(GameState.self) == false {
os_log("Failed entering Game State", log: LOG, type: .info)
}
case .syncingSt:
if stateMachine?.enter(SyncingState.self) == false {
os_log("Failed entering Syncing State", log: LOG, type: .info)
}
case .endGameSt:
if stateMachine?.enter(EndGameState.self) == false {
os_log("Failed entering EndGame State", log: LOG, type: .info)
}
default:
os_log("Allowed State was found nil, can't enter any State", log: LOG, type: .info)
}
}
func createStates(){
let menuSt = MenuState()
let syncingSt = SyncingState()
let playingSt = GameState()
let endGameSt = EndGameState()
stateMachine = GKStateMachine(states: [menuSt, syncingSt, playingSt, endGameSt])
}
}
class MenuState: GKState { class MenuState: GKState {
let LOG = OSLog.init(subsystem: "MenuState", category: "MenuState") let LOG = OSLog.init(subsystem: "MenuState", category: "MenuState")