This repository was archived by the owner on Mar 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevelLoader.swift
More file actions
62 lines (49 loc) · 1.64 KB
/
LevelLoader.swift
File metadata and controls
62 lines (49 loc) · 1.64 KB
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
//
// LevelLoader.swift
// Bomberman
//
// Created by Wolfgang Schreurs on 26/04/16.
//
//
import Foundation
enum LevelLoaderError: Error {
case failedParsingLevelFile(file: String, inBundleSupportSubDirectory: String)
}
class LevelLoader: DataLoader {
static var levelCount: Int = LevelLoader.initLevelCount()
func loadLevel(_ levelIndex: Int) throws -> Level {
print("loading level: \(levelIndex)")
var level: Level
let file = "\(levelIndex).json"
let directory = "Levels"
if let json = try loadJson(fromFile: file, inBundleSupportSubDirectory: directory) {
level = try Level(game: game, levelIndex: levelIndex, json: json as NSDictionary)
print("\(json)")
} else {
throw LevelLoaderError.failedParsingLevelFile(file: file, inBundleSupportSubDirectory: directory)
}
return level
}
fileprivate static func initLevelCount() -> Int {
var count = 0
let directory = "Levels"
let fileManager = FileManager.default
var index = 0
while (true) {
do {
if let path = try fileManager.pathForFile("\(index).json", inBundleSupportSubDirectory: directory) {
if fileManager.fileExists(atPath: path) {
count += 1
index += 1
} else {
break
}
}
} catch let error {
print("error: \(error)")
break
}
}
return count
}
}