From 3bbae779afb2940d345290d3c4d4d65e4f6dcea6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chauntalle=20Schu=CC=88le?= Date: Fri, 29 May 2020 01:08:25 +0200 Subject: [PATCH] added Statemanager with Singleton, used to create and enter states, already with logging if entering state is un/successful --- GoldWars/GoldWars.xcodeproj/project.pbxproj | 5 ++ GoldWars/GoldWars/StateManager.swift | 51 +++++++++++++++++++++ GoldWars/GoldWars/StateTypes.swift | 41 ----------------- 3 files changed, 56 insertions(+), 41 deletions(-) create mode 100644 GoldWars/GoldWars/StateManager.swift diff --git a/GoldWars/GoldWars.xcodeproj/project.pbxproj b/GoldWars/GoldWars.xcodeproj/project.pbxproj index 36a728c..bae179d 100644 --- a/GoldWars/GoldWars.xcodeproj/project.pbxproj +++ b/GoldWars/GoldWars.xcodeproj/project.pbxproj @@ -111,6 +111,8 @@ AB21D7D4246C748A00B09CBA /* MapFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapFactory.swift; sourceTree = ""; }; ABA03D9F244BD54F00A66916 /* Base.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Base.swift; sourceTree = ""; }; ABC0C3722481509300387B8F /* MapUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapUtils.swift; sourceTree = ""; }; + AE6BB1BF248030720063ECAE /* StateTypes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StateTypes.swift; sourceTree = ""; }; + AE6BB1C124807AC00063ECAE /* StateManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StateManager.swift; sourceTree = ""; }; C04783ED2468583F004961FB /* intro-music.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = "intro-music.mp3"; sourceTree = ""; }; C04783EF24685995004961FB /* SettingsScene.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsScene.swift; sourceTree = ""; }; C05BB9C3247D890C00411249 /* SliderComponent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SliderComponent.swift; sourceTree = ""; }; @@ -182,6 +184,8 @@ 9E0E459624796262009817A6 /* GameCenterManager.swift */, C04783EF24685995004961FB /* SettingsScene.swift */, 3EAD889424801B6A0048A10A /* RoundTimer.swift */, + AE6BB1BF248030720063ECAE /* StateTypes.swift */, + AE6BB1C124807AC00063ECAE /* StateManager.swift */, ); path = GoldWars; sourceTree = ""; @@ -406,6 +410,7 @@ 9EC2FBA72476B1EC00ABF11F /* PlayerInfoComponent.swift in Sources */, AE6BB1C0248030720063ECAE /* StateTypes.swift in Sources */, 9EEDE02D246FCD770096C735 /* SpinningLogoEntity.swift in Sources */, + AE6BB1C224807AC00063ECAE /* StateManager.swift in Sources */, 9E174C86245DD91500209FF0 /* ButtonComponent.swift in Sources */, 11036113244B3E30008610AF /* MenuScene.swift in Sources */, C099579C246C5E5C0016AA22 /* DataService.swift in Sources */, diff --git a/GoldWars/GoldWars/StateManager.swift b/GoldWars/GoldWars/StateManager.swift new file mode 100644 index 0000000..3441ecb --- /dev/null +++ b/GoldWars/GoldWars/StateManager.swift @@ -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]) + } +} diff --git a/GoldWars/GoldWars/StateTypes.swift b/GoldWars/GoldWars/StateTypes.swift index f9e9f8f..4d47c3e 100644 --- a/GoldWars/GoldWars/StateTypes.swift +++ b/GoldWars/GoldWars/StateTypes.swift @@ -16,47 +16,6 @@ enum StateTypes { 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 { let LOG = OSLog.init(subsystem: "MenuState", category: "MenuState")