-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
192 additions
and
13 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
Sources/GateEngine/Resources/Importers/Formats/TiledTMJImporter.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright © 2023 Dustin Collins (Strega's Gate) | ||
* All Rights Reserved. | ||
* | ||
* http://stregasgate.com | ||
*/ | ||
|
||
import Foundation | ||
|
||
fileprivate struct TMJFile: Decodable { | ||
let type: String | ||
let compressionlevel: Int | ||
let orientation: String | ||
let renderorder: String | ||
let infinite: Bool | ||
let tileheight: Int | ||
let tilewidth: Int | ||
let width: Int | ||
let height: Int | ||
let layers: [Layer] | ||
struct Layer: Decodable { | ||
let id: Int | ||
let name: String | ||
let type: String | ||
let visible: Bool | ||
let opacity: Float | ||
let x: Int | ||
let y: Int | ||
let width: Int | ||
let height: Int | ||
let data: [Int32] | ||
} | ||
let nextlayerid: Int | ||
let nextobjectid: Int | ||
let version: String | ||
let tiledversion: String | ||
} | ||
|
||
public class TiledTMJImporter: TileMapImporter { | ||
public required init() {} | ||
|
||
public func process(data: Data, baseURL: URL, options: TileMapImporterOptions) async throws -> TileMap { | ||
let file = try JSONDecoder().decode(TMJFile.self, from: data) | ||
|
||
var layers: [TileMap.Layer] = [] | ||
layers.reserveCapacity(file.layers.count) | ||
for fileLayer in file.layers { | ||
var tiles: [[TileMap.Tile]] = [] | ||
let count = fileLayer.width * fileLayer.height | ||
for start in stride(from: 0, through: count, by: fileLayer.width) { | ||
let subset = fileLayer.data[start ..< start + fileLayer.width] | ||
tiles.append(subset.map({ rawID in | ||
var options: TileMap.Tile.Options = [] | ||
var flags = TileMap.Tile.Options(rawValue: UInt(rawID)) | ||
if flags.contains(.flippedHorizontal) { | ||
options.insert(.flippedHorizontal) | ||
} | ||
if flags.contains(.flippedVertical) { | ||
options.insert(.flippedVertical) | ||
} | ||
if flags.contains(.flippedDiagonal) { | ||
options.insert(.flippedDiagonal) | ||
} | ||
if flags.contains(.rotatedHexagonal120) { | ||
options.insert(.rotatedHexagonal120) | ||
} | ||
flags.remove([.flippedHorizontal, .flippedVertical, .flippedDiagonal, .rotatedHexagonal120]) | ||
return TileMap.Tile(id: Int(flags.rawValue), options: options) | ||
})) | ||
} | ||
let layer = TileMap.Layer(name: fileLayer.name, | ||
size: Size2(Float(fileLayer.width), Float(fileLayer.height)), | ||
tileSize: Size2(Float(file.tilewidth), Float(file.tileheight)), | ||
tiles: tiles) | ||
layers.append(layer) | ||
} | ||
|
||
return TileMap(layers: layers) | ||
} | ||
|
||
static public func supportedFileExtensions() -> [String] { | ||
return ["tmj"] | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
Sources/GateEngine/Resources/Importers/Formats/TiledTSJImporter.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright © 2023 Dustin Collins (Strega's Gate) | ||
* All Rights Reserved. | ||
* | ||
* http://stregasgate.com | ||
*/ | ||
|
||
import Foundation | ||
|
||
fileprivate struct TSJFile: Decodable { | ||
let columns: Int | ||
let image: String | ||
let imageheight: Int | ||
let imagewidth: Int | ||
let margin: Int | ||
let name: String | ||
let spacing: Int | ||
let tilecount: Int | ||
let tiledversion: String | ||
let tileheight: Int | ||
let tilewidth: Int | ||
let type: String | ||
let version: String | ||
let tiles: [Tile]? | ||
|
||
struct Tile: Decodable { | ||
let id: Int | ||
let properties: [Property]? | ||
|
||
struct Property: Decodable { | ||
let name: String | ||
let type: String | ||
let value: String | ||
} | ||
} | ||
} | ||
|
||
public class TiledTSJImporter: TileSetImporter { | ||
public required init() {} | ||
|
||
public func process(data: Data, baseURL: URL, options: TileSetImporterOptions) async throws -> TileSet { | ||
let file = try JSONDecoder().decode(TSJFile.self, from: data) | ||
|
||
let tiles: [Tile] = (0 ..< file.tilecount).map({ id in | ||
var properties: [String: String] = [:] | ||
if let fileTiles = file.tiles { | ||
if let fileTile = fileTiles.first(where: {$0.id == id}) { | ||
if let fileTileProperties = fileTile.properties { | ||
for property in fileTileProperties { | ||
properties[property.name] = property.value | ||
} | ||
} | ||
} | ||
} | ||
return Tile(id: id, properties: properties, colliders: nil) | ||
}) | ||
|
||
return TileSet(textureName: file.image, | ||
textureSize: Size2(Float(file.imagewidth), Float(file.imageheight)), | ||
count: file.tilecount, | ||
columns: file.columns, | ||
tileSize: Size2(Float(file.tilewidth), Float(file.tileheight)), | ||
tiles: tiles) | ||
} | ||
|
||
static public func supportedFileExtensions() -> [String] { | ||
return ["tsj"] | ||
} | ||
} |
This file contains 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
This file contains 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