Project
public class Project
Projects are the primary entry point for TiledKit. The project represents the root of all Maps and TileSets and the resources (such as images) that support them.
The default Project will use the main Bundle as its root, making it very easy to include in a Swift Package Manager or Xcode module.
let myMap = Project.default.get("mymap")
The above code will retrieve a file named ‘mymap.tmx’ from the root of the main Bundle. You may create instances of Projects that have a different root (such as an
actual Tiled project directory, or different Bundle) using the standard constructurs.
Projects are critical as they enable the relative processing of any resources referenced in the Tiled files and provide the key entry point for specializing TiledKit for a
a specific game engine. Specializations only require the registration of a specific ResourceLoader for the type of object required by the game engine. See
SKTiledKit for an example of a specialization.
Projects also provide resource caching capabilities, ensuring that the contents of any URL are loaded only once. If you add your own ResourceLoader for types
you can specify that if the created object should be cached or not.
-
The object types associated with the project
Declaration
Swift
public var objectTypes: ObjectTypes -
The location the project defines for storing the
ObjectTypesfileDeclaration
Swift
public var objectTypesUrl: URL -
The default
Projectwhich uses theBundle.mainas its resource rootDeclaration
Swift
public static let `default`: Project -
Creates a new instance of a
Projectthat uses a different bundle as a root.Declaration
Swift
public init(using bundle: Bundle, searchForProjectFile autodiscover: Bool = true)Parameters
bundleThe
Bundleto useautodiscoverWill do a deep search in the bundle for a
.tiled-projectfile and if found load it and set the base URL to the folder containing it. -
Creates a new instance of a
Projectthat uses the specied directory as its rootDeclaration
Swift
public init(at rootDirectory: URL, searchForProjectFile autodiscover: Bool = true)Parameters
rootDirectoryThe
URLof a directory on the local machineautodiscoverWill do a deep search in the folder for a
.tiled-projectfile and if found load it and set the base URL to the folder containing it. -
Creates a new instance of a
Projectthat uses the specified project file URL to provide both the root of the project for relative paths, and will load the contents of the file to provide the object types and foldersThrows
If the file does not exist or cannot be decodedDeclaration
Swift
public init(from projectFile: URL) throwsParameters
rootDirectoryThe
URLof the.tiled-projectfile -
Retrieves the
URLfor a resource within the projectDeclaration
Swift
public func url(for file: String, in subDirectory: String? = nil, of type: FileType? = nil) -> URL?Parameters
fileThe name of the file, excluding any extension
subDirectoryThe sub-directory path if it is not located at the root
typeThe type of file
Return Value
A
URLfor the file in theProjectornilif it could not be found -
Resources a url within the project, if it may be relative, then you should supply the URL it is relative to
Declaration
Swift
public func resolve(_ url: URL, relativeTo baseUrl: URL?) -> URL?Parameters
urlThe
URLto be resolved (it does not need to be in the project if it is a fully specified URL)relativeToIf the URL could be or is relative, then this will be used as the base
Return Value
A URL or nil if the the URL is not reachable
-
Loads a
Mapfrom the project with the specied name (and optionally subdirectory). Note that you do not have to include the file extension.tmxin the name (although if you do theMapwill still be loaded).Throws
Any errors finding or loading theMapwill be thrown hereDeclaration
Swift
public func retrieve(map name: String, in subdirectory: String? = nil) throws -> MapParameters
nameThe name of the map’s file. You can ommit the “.tmx ” extension, but it is acceptable to include it.
subdirectoryThe sub-directory path (from the root of the project)
Return Value
The desired
Map -
Loads a
TileSetfrom the project with the specied name (and optionally subdirectory). Note that you do not have to include the file extension.tsxin the name (although if you do theTileSetwill still be loaded).Throws
Any errors finding or loading theTileSetwill be thrown hereDeclaration
Swift
public func retrieve(tileset name: String, in subdirectory: String? = nil) throws -> TileSetParameters
nameThe name of the tileset’s file. You can ommit the “.tsx ” extension, but it is acceptable to include it.
subdirectoryThe sub-directory path (from the root of the project)
Return Value
The desired
TileSet -
Loads a URL for a resource (which can be relative to another resource in the project, very useful as Tiled often uses relative paths within projects, or across Maps and Tile Sets).
Throws
Any errors thrown while the resource is being retreived (for example, the resource can’t be found)Declaration
Swift
public func retrieve<R>(asType: R.Type, from resourceUrl: URL, relativeTo baseUrl: URL? = nil) throws -> R where R : LoadableParameters
asTypeThe desired type
resourceUrlThe
URLof the resource (can be relative)baseUrlThe
URLto use as the starting point ifresourceURLis relative. Otherwise the project root will be usedReturn Value
An instance of the resource
-
Stores an asset in the
Projects resource cache for later retreival via the suppliedURL. In this wayResourceLoaderscan exploit the ability to create common cached assetsDeclaration
Swift
public func store<R>(_ asset: R, as assetUrl: URL) where R : LoadableParameters
assetThe asset
assetUrlThe URL of the asset/resource
-
Retreive a specialized map for a specific game engine. Note that the exact specialization will be determined by the receiving variable, so you must ensure the type is explicit. For example:
let level : CoolEngineScene = try Project.default.retrieve(specializedMap: "Level 1")You may wish to wrap this in further extensions to project (e.g.
func retrieve(coolEngineMap:String))Throws
Errors from loadingDeclaration
Swift
func retrieve<EngineType>(_ engine: EngineType.Type, mapNamed name: String, in subdirectory: String? = nil) throws -> EngineType.MapType where EngineType : EngineParameters
nameThe name of the map which will be resolved to a specific tiled
Mapfile using the standardProjectmethodologysubdirectoryThe subdirectory the map is stored in relative to the
ProjectrootReturn Value
An instance of the specialized map
View on GitHub
Project Class Reference