This repository has been archived by the owner on Feb 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Launcher.swift
77 lines (66 loc) · 2.63 KB
/
Launcher.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//
// Launcher.swift
// Of Witches and Mazes
//
// Created by Gustavo C. Viegas on 9/11/18.
// Copyright © 2018 Gustavo C. Viegas. All rights reserved.
//
import SpriteKit
/// The struct responsible for launching the game.
///
struct Launcher {
/// An enum defining the possible states of the launcher.
///
private enum State {
case notLaunched, launching, launched
}
/// The current state of the launcher.
///
private static var state = State.notLaunched
/// The flag stating whether or not `Launcher.launch(window:view:completionHandler)` can be called.
///
static var canLaunch: Bool {
return state == .notLaunched
}
/// The flag stating whether or not `Launcher.launch(window:view:completionHandler)` has completed.
///
static var launchCompleted: Bool {
return state == .launched
}
private init() {}
/// Launches the game asynchronously.
///
/// - Parameters:
/// - window: The window object to use for the game.
/// - view: The view object to use for the game.
/// - completionHandler: A closure to be called when the launch process completes.
///
static func launch(window: NSWindow, view: SKView, completionHandler: @escaping () -> Void) {
guard state == .notLaunched else {
fatalError("Launcher.launch(window:view:completionHandler) called while in an invalid state")
}
state = .launching
Window.window = window
SceneManager.view = view
SceneManager.setScene(LoadingScene(), sceneKind: .loading)
let flag = SceneManager.switchToScene(ofKind: .loading); assert(flag)
DataFileManager.instance.prepare {
DataFileManager.instance.downloadConfigurationData { _ in
TexturePreloadManager.preloadInitialGameTextures {
SceneManager.setScene(MenuScene(menuType: MainMenu.self), sceneKind: .mainMenu)
SceneManager.setScene(MenuScene(menuType: NewGameMenu.self), sceneKind: .newGameMenu)
SceneManager.setScene(MenuScene(menuType: LoadGameMenu.self), sceneKind: .loadGameMenu)
SceneManager.setScene(MenuScene(menuType: SettingsMenu.self), sceneKind: .settingsMenu)
DispatchQueue.main.async {
let flag = SceneManager.switchToScene(ofKind: .mainMenu); assert(flag)
state = .launched
completionHandler()
}
}
}
}
#if DEBUG
INITDEBUG.run()
#endif
}
}