Last active
June 1, 2023 16:13
-
-
Save aheze/342adf1a2feb76e27cf14a0469c9331e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| enum WorldParser { | |
| /// process a world string, example: | |
| /* | |
| treasure | |
| 3x3 | |
| gold diamond gold | |
| diamond gold diamond | |
| gold diamond gold | |
| */ | |
| static func process(string: String) -> World { | |
| var world = World(width: 1, height: 1, blocks: []) | |
| var string = string | |
| string = string.condenseSpaces() | |
| string = string.condenseDoubleNewlines() | |
| let components = string.components(separatedBy: "\n\n") | |
| // MARK: - Get width and height | |
| if var first = components.first { | |
| first = first.trimmingCharacters(in: .whitespacesAndNewlines) | |
| let characters = first.components(separatedBy: "x") | |
| if let string = characters.first, let width = Int(string) { | |
| world.width = width | |
| } | |
| if let string = characters.last, let height = Int(string) { | |
| world.height = height | |
| } | |
| } | |
| let layers = Array(components.dropFirst().reversed()) | |
| for layerIndex in layers.indices { | |
| let layer = layers[layerIndex] | |
| let rows = layer.components(separatedBy: "\n") | |
| for rowIndex in rows.indices { | |
| var row = rows[rowIndex] | |
| row = row.trimmingCharacters(in: .whitespacesAndNewlines) | |
| let columns = row.components(separatedBy: " ") | |
| for columnIndex in columns.indices { | |
| let column = columns[columnIndex] | |
| if let blockKind = BlockKind(rawValue: column) { | |
| let coordinate = Coordinate(row: rowIndex, column: columnIndex, levitation: layerIndex) | |
| let block = Block(coordinate: coordinate, blockKind: blockKind) | |
| world.blocks.append(block) | |
| } | |
| } | |
| } | |
| } | |
| return world | |
| } | |
| } | |
| extension String { | |
| func condenseSpaces() -> String { | |
| return replacingOccurrences(of: #"[\h\t]{2,}"#, with: " ", options: .regularExpression, range: nil) | |
| } | |
| func condenseDoubleNewlines() -> String { | |
| return replacingOccurrences(of: #"\n{2,}"#, with: "\n\n", options: .regularExpression, range: nil) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct WorldPreset: Identifiable { | |
| var id: String { | |
| name | |
| } | |
| var name: String | |
| var world: World | |
| static var allPresets: [WorldPreset] = { | |
| let strings = WorldParser.getPresetStrings() | |
| let presets = strings.compactMap { WorldParser.getPreset(string: $0) } | |
| return presets | |
| }() | |
| } | |
| extension WorldParser { | |
| static func getPreset(string: String) -> WorldPreset? { | |
| let components = string.components(separatedBy: "\n") | |
| if let name = components.first { | |
| let worldString = components.dropFirst().joined(separator: "\n") | |
| let world = process(string: worldString) | |
| return WorldPreset(name: name, world: world) | |
| } | |
| return nil | |
| } | |
| static func getPresetStrings() -> [String] { | |
| /// presets string is split by --- | |
| /// stored in a local file right now, will host online later | |
| guard let string = getWorldPresetsString() else { return [] } | |
| var components = string.components(separatedBy: "---") | |
| components = components.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) } | |
| return components | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment