Copyright (c) 2011 Steve Revill and Shane Woolcock
Copyright (c) 2011 Steve Revill and Shane Woolcock
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#End
#End
Strict
Strict
Import xml
Import xml
Import base64
Import base64
Import mojo
Import mojo
Import brl.filepath
Import brl.filepath
' TileMapPropertyContainer
' TileMapPropertyContainer
'Summary: Classes that extend this will automatically instantiate a property container and expose it.
'Summary: Classes that extend this will automatically instantiate a property container and expose it.
Class TileMapPropertyContainer
Class TileMapPropertyContainer
Private
Private
Field properties:TileMapProperties = New TileMapProperties
Field properties:TileMapProperties = New TileMapProperties
Public
Public
Method Properties:TileMapProperties() Property
Method Properties:TileMapProperties() Property
Return properties
Return properties
End
End
End
End
' ITileMapPostLoad
' ITileMapPostLoad
'Summary: Classes that implement this interface will have PostLoad automatically called once the object has been created.
'Summary: Classes that implement this interface will have PostLoad automatically called once the object has been created.
Interface ITileMapPostLoad
Interface ITileMapPostLoad
Method PostLoad:Void()
Method PostLoad:Void()
End
End
' TileMapReader
' TileMapReader
'Summary: This can be extended to handle any map file format.
'Summary: This can be extended to handle any map file format.
Class TileMapReader Abstract
Class TileMapReader Abstract
Field tileMap:TileMap
Field tileMap:TileMap
Field graphicsPath:String
Field graphicsPath:String
Method LoadMap:TileMap(filename:String) Abstract
Method LoadMap:TileMap(filename:String) Abstract
' override this to create a custom tilemap class
' override this to create a custom tilemap class
Method CreateMap:TileMap()
Method CreateMap:TileMap()
Return New TileMap
Return New TileMap
End
End
End
End
' TiledTileMapReader
' TiledTileMapReader
'Summary: Extends TileMapReader to add support for the Tiled map editor.
'Summary: Extends TileMapReader to add support for the Tiled map editor.
Class TiledTileMapReader Extends TileMapReader
Class TiledTileMapReader Extends TileMapReader
Field doc:XMLDoc
Field doc:XMLDoc
' Overrides TileMapReader
' Overrides TileMapReader
Method LoadMap:TileMap(filename:String)
Method LoadMap:TileMap(filename:String)
' open file and get root node
' open file and get root node
Local xmlString:String = LoadString(filename)
Local xmlString:String = LoadString(filename)
' error if we couldnt load the file
' error if we couldnt load the file
If Not xmlString
If Not xmlString
Error("Cannot load tile map file " + filename + ". Ensure you have TMX in the in the allowed #TEXT_FILES")
Error("Cannot load tile map file " + filename + ". Ensure you have TMX in the in the allowed #TEXT_FILES")
End
End
' look for the data encoding, if we cant find it assume its RAW XML and thats just too slow!
' look for the data encoding, if we cant find it assume its RAW XML and thats just too slow!
Local findData:Int = xmlString.Find("<data encoding")
Local findData:Int = xmlString.Find("<data encoding")
If findData = -1
If findData = -1
Print("Tiled Raw XML is not supported!")
Print("Tiled Raw XML is not supported!")
End
End
'Set the root graphics path relative to this file's location.
graphicsPath = ExtractDir(filename) + "/"
doc = ParseXML(xmlString)
doc = ParseXML(xmlString)
Return ReadMap(doc)
Return ReadMap(doc)
End
End
Method ReadMap:TileMap(node:XMLNode)
Method ReadMap:TileMap(node:XMLNode)
tileMap = CreateMap()
tileMap = CreateMap()
ReadProperties(node, tileMap)
ReadProperties(node, tileMap)
' extract map properties
' extract map properties
If tileMap.properties.Has(PROP_MAP_WRAP_X) Then tileMap.wrapX = tileMap.properties.Get(PROP_MAP_WRAP_X).GetBool()
If tileMap.properties.Has(PROP_MAP_WRAP_X) Then tileMap.wrapX = tileMap.properties.Get(PROP_MAP_WRAP_X).GetBool()
If tileMap.properties.Has(PROP_MAP_WRAP_Y) Then tileMap.wrapY = tileMap.properties.Get(PROP_MAP_WRAP_Y).GetBool()
If tileMap.properties.Has(PROP_MAP_WRAP_Y) Then tileMap.wrapY = tileMap.properties.Get(PROP_MAP_WRAP_Y).GetBool()
' read root node's attributes
' read root node's attributes
If node.HasAttribute(ATTR_MAP_VERSION) Then tileMap.version = node.GetAttribute(ATTR_MAP_VERSION)
If node.HasAttribute(ATTR_MAP_VERSION) Then tileMap.version = node.GetAttribute(ATTR_MAP_VERSION)
If node.HasAttribute(ATTR_MAP_ORIENTATION) Then tileMap.orientation = node.GetAttribute(ATTR_MAP_ORIENTATION)
If node.HasAttribute(ATTR_MAP_ORIENTATION) Then tileMap.orientation = node.GetAttribute(ATTR_MAP_ORIENTATION)
If node.HasAttribute(ATTR_MAP_WIDTH) Then tileMap.width = Int(node.GetAttribute(ATTR_MAP_WIDTH))
If node.HasAttribute(ATTR_MAP_WIDTH) Then tileMap.width = Int(node.GetAttribute(ATTR_MAP_WIDTH))
If node.HasAttribute(ATTR_MAP_HEIGHT) Then tileMap.height = Int(node.GetAttribute(ATTR_MAP_HEIGHT))
If node.HasAttribute(ATTR_MAP_HEIGHT) Then tileMap.height = Int(node.GetAttribute(ATTR_MAP_HEIGHT))
If node.HasAttribute(ATTR_MAP_TILEWIDTH) Then tileMap.tileWidth = Int(node.GetAttribute(ATTR_MAP_TILEWIDTH))
If node.HasAttribute(ATTR_MAP_TILEWIDTH) Then tileMap.tileWidth = Int(node.GetAttribute(ATTR_MAP_TILEWIDTH))
If node.HasAttribute(ATTR_MAP_TILEHEIGHT) Then tileMap.tileHeight = Int(node.GetAttribute(ATTR_MAP_TILEHEIGHT))
If node.HasAttribute(ATTR_MAP_TILEHEIGHT) Then tileMap.tileHeight = Int(node.GetAttribute(ATTR_MAP_TILEHEIGHT))
tileMap.maxTileWidth = tileMap.tileWidth
tileMap.maxTileWidth = tileMap.tileWidth
tileMap.maxTileHeight = tileMap.tileHeight
tileMap.maxTileHeight = tileMap.tileHeight
' parse children
' parse children
If Not node.children.IsEmpty() Then
If Not node.children.IsEmpty() Then
For Local mapchild:XMLNode = Eachin node.children
For Local mapchild:XMLNode = Eachin node.children
' tileset
' tileset
If mapchild.name = NODE_TILESET Then
If mapchild.name = NODE_TILESET Then
Local ts:TileMapTileset = ReadTileset(mapchild)
Local ts:TileMapTileset = ReadTileset(mapchild)
tileMap.tilesets.Set(ts.name, ts)
tileMap.tilesets.Set(ts.name, ts)
' tile layer
' tile layer
Elseif mapchild.name = NODE_LAYER Then
Elseif mapchild.name = NODE_LAYER Then
Local layer:TileMapLayer = ReadTileLayer(mapchild)
Local layer:TileMapLayer = ReadTileLayer(mapchild)
tileMap.layers.Push(layer)
tileMap.layers.Push(layer)
' object layer
' object layer
Elseif mapchild.name = NODE_OBJECTGROUP Then
Elseif mapchild.name = NODE_OBJECTGROUP Then
Local layer:TileMapLayer = ReadObjectLayer(mapchild)
Local layer:TileMapLayer = ReadObjectLayer(mapchild)
tileMap.layers.Push(layer)
tileMap.layers.Push(layer)
Endif
Endif
Next
Next
Endif
Endif
DoPostLoad(tileMap)
DoPostLoad(tileMap)
Return tileMap
Return tileMap
End
End
Method DoPostLoad:Void(obj:Object)
Method DoPostLoad:Void(obj:Object)
If ITileMapPostLoad(obj) <> Null Then ITileMapPostLoad(obj).PostLoad()
If ITileMapPostLoad(obj) <> Null Then ITileMapPostLoad(obj).PostLoad()