void ParseTileElement() { if (currentParentElement == CCTileMapProperty.Layer) { int layersCount = Layers != null ? Layers.Count : 0; CCTileLayerInfo layer = layersCount > 0 ? Layers[layersCount - 1] : null; uint gidAndFlags = uint.Parse(currentAttributeDict[TileElementGidAndFlags]); if (currentXmlTileIndex < layer.NumberOfTiles) { layer.TileGIDAndFlags[currentXmlTileIndex++] = CCTileMapFileEncodedTileFlags.DecodeGidAndFlags(gidAndFlags); } } else { int tilesetCount = Tilesets != null ? Tilesets.Count : 0; CCTileSetInfo info = tilesetCount > 0 ? Tilesets[tilesetCount - 1] : null; var dict = new Dictionary <string, string>(); ParentGID = (short)(info.FirstGid + short.Parse(currentAttributeDict[TileElementId])); TileProperties.Add(ParentGID, dict); currentParentElement = CCTileMapProperty.Tile; } }
CCTileMapInfo() { Tilesets = new List <CCTileSetInfo>(); Layers = new List <CCTileLayerInfo>(); ObjectGroups = new List <CCTileMapObjectGroup>(4); MapProperties = new Dictionary <string, string>(); TileProperties = new Dictionary <short, Dictionary <string, string> >(); TileAnimations = new Dictionary <short, List <CCTileAnimationKeyFrame> >(); tileDataCompressionType = CCTileDataCompressionType.None; currentParentElement = CCTileMapProperty.None; currentFirstGID = 0; // Associating xml elements of tmx file with parse actions mapFileElementActions = new Dictionary <string, Tuple <Action, Action> > { { "map", Tuple.Create <Action, Action>(ParseMapElement, ParseMapEndElement) }, { "tileset", Tuple.Create <Action, Action>(ParseTilesetElement, ParseTilesetEndElement) }, { "tile", Tuple.Create <Action, Action>(ParseTileElement, ParseTileEndElement) }, { "animation", Tuple.Create <Action, Action>(ParseTileAnimationElement, ParseTileAnimationEndElement) }, { "frame", Tuple.Create <Action, Action>(ParseTileAnimationKeyFrameElement, ParseTileAnimationKeyFrameEndElement) }, { "layer", Tuple.Create <Action, Action>(ParseLayerElement, ParseLayerEndElement) }, { "objectgroup", Tuple.Create <Action, Action>(ParseObjectGroupElement, ParseObjectGroupEndElement) }, { "image", Tuple.Create <Action, Action>(ParseImageElement, ParseImageEndElement) }, { "data", Tuple.Create <Action, Action>(ParseDataElement, ParseDataEndElement) }, { "object", Tuple.Create <Action, Action>(ParseObjectElement, ParseObjectEndElement) }, { "property", Tuple.Create <Action, Action>(ParsePropertyElement, ParsePropertyEndElement) }, { "polygon", Tuple.Create <Action, Action>(ParsePolygonElement, ParsePolygonEndElement) }, { "polyline", Tuple.Create <Action, Action>(ParsePolylineElement, ParsePolylineEndElement) }, { "ellipse", Tuple.Create <Action, Action>(ParseEllipseElement, ParseEllipseEndElement) }, }; }
void ParseTileAnimationElement() { if (currentParentElement == CCTileMapProperty.Tile) { currentParentElement = CCTileMapProperty.TileAnimation; currentTileAnimationKeyFrames = new List <CCTileAnimationKeyFrame>(); } }
void ParseTileAnimationEndElement() { if (currentParentElement == CCTileMapProperty.TileAnimation) { if (currentTileAnimationKeyFrames != null && currentTileAnimationKeyFrames.Count > 0) { TileAnimations.Add(ParentGID, currentTileAnimationKeyFrames); } currentTileAnimationKeyFrames = null; } currentParentElement = CCTileMapProperty.Tile; }
void ParseLayerElement() { var layerInfo = new CCTileLayerInfo(); layerInfo.Name = currentAttributeDict[LayerElementName]; CCTileMapCoordinates layerSize; layerSize.Column = (int)CCUtils.CCParseFloat(currentAttributeDict[LayerElementNumOfColumns]); layerSize.Row = (int)CCUtils.CCParseFloat(currentAttributeDict[LayerElementNumOfRows]); layerInfo.LayerDimensions = layerSize; layerInfo.TileGIDAndFlags = new CCTileGidAndFlags[layerSize.Column * layerSize.Row]; if (currentAttributeDict.ContainsKey(LayerElementVisible)) { string visible = currentAttributeDict[LayerElementVisible]; layerInfo.Visible = !(visible == "0"); } else { layerInfo.Visible = true; } if (currentAttributeDict.ContainsKey(LayerElementOpacity)) { string opacity = currentAttributeDict[LayerElementOpacity]; layerInfo.Opacity = (byte)(byte.MaxValue * CCUtils.CCParseFloat(opacity)); } else { layerInfo.Opacity = byte.MaxValue; } float x = currentAttributeDict.ContainsKey(LayerElementXOffset) ? CCUtils.CCParseFloat(currentAttributeDict[LayerElementXOffset]) : 0; float y = currentAttributeDict.ContainsKey(LayerElementYOffset) ? CCUtils.CCParseFloat(currentAttributeDict[LayerElementYOffset]) : 0; layerInfo.TileCoordOffset = new CCPoint(x, y); Layers.Add(layerInfo); currentParentElement = CCTileMapProperty.Layer; }
void ParseObjectGroupElement() { var objectGroup = new CCTileMapObjectGroup(); objectGroup.GroupName = currentAttributeDict[ObjectGrpElementName]; CCPoint positionOffset = CCPoint.Zero; if (currentAttributeDict.ContainsKey(ObjectGrpElementXOffset)) { positionOffset.X = CCUtils.CCParseFloat(currentAttributeDict[ObjectGrpElementXOffset]) * TileTexelSize.Width; } if (currentAttributeDict.ContainsKey(ObjectGrpElementYOffset)) { positionOffset.Y = CCUtils.CCParseFloat(currentAttributeDict[ObjectGrpElementYOffset]) * TileTexelSize.Height; } objectGroup.PositionOffset = positionOffset; ObjectGroups.Add(objectGroup); currentParentElement = CCTileMapProperty.ObjectGroup; }
void ParseMapElement() { // Version float version = CCUtils.CCParseFloat(currentAttributeDict[MapElementVersion]); if (version != 1.0f) { CCLog.Log("CocosSharp: CCTileMapInfo: Unsupported TMX version: {0}", version); } // Map type. Ortho, Iso, etc. string mapTypeStr = currentAttributeDict[MapElementMapType]; CCTileMapType mapType = CCTileMapType.None; if (mapTypeKeys.TryGetValue(mapTypeStr, out mapType)) { this.MapType = mapType; } else { CCLog.Log("CocosSharp: CCTileMapInfo: Unsupported TMX map type: {0}", mapTypeStr); } // Num of tile rows/columns in map CCTileMapCoordinates mapSize; mapSize.Column = (int)CCUtils.CCParseFloat(currentAttributeDict[MapElementNumOfColumns]); mapSize.Row = (int)CCUtils.CCParseFloat(currentAttributeDict[MapElementNumOfRows]); this.MapDimensions = mapSize; // Default tile texel dimensions CCSize tileTexelSize; tileTexelSize.Width = CCUtils.CCParseFloat(currentAttributeDict[MapElementTileTexelWidth]); tileTexelSize.Height = CCUtils.CCParseFloat(currentAttributeDict[MapElementTileTexelHeight]); this.TileTexelSize = tileTexelSize; this.currentParentElement = CCTileMapProperty.Map; }
void ParseObjectElement() { if (ObjectGroups == null || ObjectGroups.Count == 0) { return; } CCTileMapObjectGroup objectGroup = ObjectGroups[ObjectGroups.Count - 1]; // The value for "type" was blank or not a valid class name // Create an instance of TMXObjectInfo to store the object and its properties var dict = new Dictionary <string, string>(); var array = new[] { ObjectElementName, ObjectElementType, ObjectElementWidth, ObjectElementHeight, ObjectElementGid }; foreach (string key in array) { if (currentAttributeDict.ContainsKey(key)) { dict.Add(key, currentAttributeDict[key]); } } float x = float.Parse(currentAttributeDict[ObjectElementXPosition]) + objectGroup.PositionOffset.X; float y = float.Parse(currentAttributeDict[ObjectElementYPosition]) + objectGroup.PositionOffset.Y; // Correct y position. Tiled uses inverted y-coordinate system where top is y=0 y = (MapDimensions.Row * TileTexelSize.Height) - y - (currentAttributeDict.ContainsKey(ObjectElementHeight) ? float.Parse(currentAttributeDict[ObjectElementHeight]) : 0); dict.Add(ObjectElementXPosition, ToFloatString(x)); dict.Add(ObjectElementYPosition, ToFloatString(y)); objectGroup.Objects.Add(dict); currentParentElement = CCTileMapProperty.Object; }
void ParseObjectEndElement() { currentParentElement = CCTileMapProperty.None; }
void ParseTileElement() { if (currentParentElement == CCTileMapProperty.Layer) { int layersCount = Layers != null ? Layers.Count : 0; CCTileLayerInfo layer = layersCount > 0 ? Layers[layersCount - 1] : null; uint gidAndFlags = uint.Parse(currentAttributeDict[TileElementGidAndFlags]); if (currentXmlTileIndex < layer.NumberOfTiles) layer.TileGIDAndFlags[currentXmlTileIndex++] = CCTileMapFileEncodedTileFlags.DecodeGidAndFlags(gidAndFlags); } else { int tilesetCount = Tilesets != null ? Tilesets.Count : 0; CCTileSetInfo info = tilesetCount > 0 ? Tilesets[tilesetCount - 1] : null; var dict = new Dictionary<string, string>(); ParentGID = (short)(info.FirstGid + short.Parse(currentAttributeDict[TileElementId])); TileProperties.Add(ParentGID, dict); currentParentElement = CCTileMapProperty.Tile; } }
void ParseTileAnimationElement() { if(currentParentElement == CCTileMapProperty.Tile) { currentParentElement = CCTileMapProperty.TileAnimation; currentTileAnimationKeyFrames = new List<CCTileAnimationKeyFrame>(); } }
void ParseLayerEndElement() { currentParentElement = CCTileMapProperty.None; }
void ParseTileAnimationEndElement() { if(currentParentElement == CCTileMapProperty.TileAnimation) { if(currentTileAnimationKeyFrames != null && currentTileAnimationKeyFrames.Count > 0) TileAnimations.Add(ParentGID, currentTileAnimationKeyFrames); currentTileAnimationKeyFrames = null; } currentParentElement = CCTileMapProperty.Tile; }
void ParseObjectElement() { if (ObjectGroups == null || ObjectGroups.Count == 0) return; CCTileMapObjectGroup objectGroup = ObjectGroups[ObjectGroups.Count - 1]; // The value for "type" was blank or not a valid class name // Create an instance of TMXObjectInfo to store the object and its properties var dict = new Dictionary<string, string>(); var array = new[] { ObjectElementName, ObjectElementType, ObjectElementWidth, ObjectElementHeight, ObjectElementGid}; foreach (string key in array) { if (currentAttributeDict.ContainsKey(key)) { dict.Add(key, currentAttributeDict[key]); } } float x = float.Parse(currentAttributeDict[ObjectElementXPosition]) + objectGroup.PositionOffset.X; float y = float.Parse(currentAttributeDict[ObjectElementYPosition]) + objectGroup.PositionOffset.Y; // Correct y position. Tiled uses inverted y-coordinate system where top is y=0 y = (MapDimensions.Row * TileTexelSize.Height) - y - (currentAttributeDict.ContainsKey(ObjectElementHeight) ? float.Parse(currentAttributeDict[ObjectElementHeight]) : 0); dict.Add(ObjectElementXPosition, ToFloatString(x)); dict.Add(ObjectElementYPosition, ToFloatString(y)); objectGroup.Objects.Add(dict); currentParentElement = CCTileMapProperty.Object; }
void ParseObjectGroupElement() { var objectGroup = new CCTileMapObjectGroup(); objectGroup.GroupName = currentAttributeDict[ObjectGrpElementName]; CCPoint positionOffset = CCPoint.Zero; if (currentAttributeDict.ContainsKey(ObjectGrpElementXOffset)) positionOffset.X = CCUtils.CCParseFloat(currentAttributeDict[ObjectGrpElementXOffset]) * TileTexelSize.Width; if (currentAttributeDict.ContainsKey(ObjectGrpElementYOffset)) positionOffset.Y = CCUtils.CCParseFloat(currentAttributeDict[ObjectGrpElementYOffset]) * TileTexelSize.Height; objectGroup.PositionOffset = positionOffset; ObjectGroups.Add(objectGroup); currentParentElement = CCTileMapProperty.ObjectGroup; }
CCTileMapInfo() { Tilesets = new List<CCTileSetInfo>(); Layers = new List<CCTileLayerInfo>(); ObjectGroups = new List<CCTileMapObjectGroup>(4); MapProperties = new Dictionary<string, string>(); TileProperties = new Dictionary<short, Dictionary<string, string>>(); TileAnimations = new Dictionary<short, List<CCTileAnimationKeyFrame>>(); tileDataCompressionType = CCTileDataCompressionType.None; currentParentElement = CCTileMapProperty.None; currentFirstGID = 0; // Associating xml elements of tmx file with parse actions mapFileElementActions = new Dictionary<string, Tuple<Action,Action>> { { "map", Tuple.Create<Action,Action>(ParseMapElement, ParseMapEndElement) }, { "tileset", Tuple.Create<Action,Action>(ParseTilesetElement, ParseTilesetEndElement) }, { "tile", Tuple.Create<Action,Action>(ParseTileElement, ParseTileEndElement) }, { "animation", Tuple.Create<Action,Action>(ParseTileAnimationElement, ParseTileAnimationEndElement) }, { "frame", Tuple.Create<Action,Action>(ParseTileAnimationKeyFrameElement, ParseTileAnimationKeyFrameEndElement) }, { "layer", Tuple.Create<Action,Action>(ParseLayerElement, ParseLayerEndElement) }, { "objectgroup", Tuple.Create<Action,Action>(ParseObjectGroupElement, ParseObjectGroupEndElement) }, { "image", Tuple.Create<Action,Action>(ParseImageElement, ParseImageEndElement) }, { "data", Tuple.Create<Action,Action>(ParseDataElement, ParseDataEndElement) }, { "object", Tuple.Create<Action,Action>(ParseObjectElement, ParseObjectEndElement) }, { "property", Tuple.Create<Action,Action>(ParsePropertyElement, ParsePropertyEndElement) }, { "polygon", Tuple.Create<Action,Action>(ParsePolygonElement, ParsePolygonEndElement) }, { "polyline", Tuple.Create<Action,Action>(ParsePolylineElement, ParsePolylineEndElement) }, { "ellipse", Tuple.Create<Action,Action>(ParseEllipseElement, ParseEllipseEndElement) }, }; }