Esempio n. 1
0
        private static void AddObjectLayerTiles(ReducedLayerInfo reducedLayerInfo, AbstractMapLayer tiledLayer, Tileset tileSet, uint?gid, int tileWidth, int tileHeight)
        {
            var asMapLayer = tiledLayer as mapObjectgroup;

            // early out
            if (asMapLayer.@object == null)
            {
                return;
            }

            foreach (var objectInstance in asMapLayer.@object)
            {
                if (objectInstance.gid > 0)
                {
                    ReducedQuadInfo quad = new DataTypes.ReducedQuadInfo();

                    quad.LeftQuadCoordinate   = (float)objectInstance.x;
                    quad.BottomQuadCoordinate = (float)-objectInstance.y;

                    quad.OverridingWidth  = objectInstance.width;
                    quad.OverridingHeight = objectInstance.height;

                    quad.RotationDegrees = (float)objectInstance.Rotation;

                    quad.FlipFlags = (byte)(gid.Value & 0xf0000000 >> 7);

                    var valueWithoutFlip = gid.Value & 0x0fffffff;

                    int leftPixelCoord;
                    int topPixelCoord;
                    int rightPixelCoord;
                    int bottomPixelCoord;
                    TiledMapSave.GetPixelCoordinatesFromGid(gid.Value, tileSet,
                                                            out leftPixelCoord, out topPixelCoord, out rightPixelCoord, out bottomPixelCoord);

                    quad.LeftTexturePixel = (ushort)Math.Min(leftPixelCoord, rightPixelCoord);
                    quad.TopTexturePixel  = (ushort)Math.Min(topPixelCoord, bottomPixelCoord);

                    quad.Name = objectInstance.Name;
                    if (string.IsNullOrEmpty(quad.Name))
                    {
                        var prop = quad.QuadSpecificProperties.FirstOrDefault(quadProp => quadProp.Name.ToLowerInvariant() == "name");
                        quad.Name = (string)prop.Value;
                    }

                    reducedLayerInfo?.Quads.Add(quad);
                }
            }
        }
        private static void CreateFromSpriteEditorScene(TiledMapSave tiledMapSave, float scale, float zOffset, FileReferenceType referenceType, ReducedTileMapInfo toReturn)
        {
            var ses = tiledMapSave.ToSceneSave(scale, referenceType);

            // This is not a stable sort!
            //ses.SpriteList.Sort((first, second) => first.Z.CompareTo(second.Z));
            ses.SpriteList = ses.SpriteList.OrderBy(item => item.Z).ToList();

            ReducedLayerInfo reducedLayerInfo = null;

            float z = float.NaN;


            int textureWidth  = 0;
            int textureHeight = 0;

            AbstractMapLayer currentLayer = null;
            int indexInLayer = 0;


            foreach (var spriteSave in ses.SpriteList)
            {
                if (spriteSave.Z != z)
                {
                    indexInLayer = 0;
                    z            = spriteSave.Z;


                    int layerIndex       = FlatRedBall.Math.MathFunctions.RoundToInt(z - zOffset);
                    var abstractMapLayer = tiledMapSave.MapLayers[layerIndex];
                    currentLayer = abstractMapLayer;

                    reducedLayerInfo = new ReducedLayerInfo
                    {
                        Z          = spriteSave.Z,
                        Texture    = spriteSave.Texture,
                        Name       = abstractMapLayer.Name,
                        TileWidth  = FlatRedBall.Math.MathFunctions.RoundToInt(spriteSave.ScaleX * 2),
                        TileHeight = FlatRedBall.Math.MathFunctions.RoundToInt(spriteSave.ScaleY * 2)
                    };

                    var mapLayer = abstractMapLayer as MapLayer;
                    // This should have data:
                    if (mapLayer != null)
                    {
                        var     idOfTexture  = mapLayer.data[0].tiles.FirstOrDefault(item => item != 0);
                        Tileset tileSet      = tiledMapSave.GetTilesetForGid(idOfTexture);
                        var     tilesetIndex = tiledMapSave.Tilesets.IndexOf(tileSet);

                        textureWidth  = tileSet.Images[0].width;
                        textureHeight = tileSet.Images[0].height;

                        reducedLayerInfo.TextureId = tilesetIndex;
                        toReturn.Layers.Add(reducedLayerInfo);
                    }


                    var objectGroup = tiledMapSave.MapLayers[layerIndex] as mapObjectgroup;

                    // This code only works based on the assumption that only one tileset will be used in any given object layer's image objects
                    var mapObjectgroupObject = [email protected](o => o.gid != null);

                    if (mapObjectgroupObject?.gid != null)
                    {
                        var     idOfTexture  = mapObjectgroupObject.gid.Value;
                        Tileset tileSet      = tiledMapSave.GetTilesetForGid(idOfTexture);
                        var     tilesetIndex = tiledMapSave.Tilesets.IndexOf(tileSet);

                        textureWidth  = tileSet.Images[0].width;
                        textureHeight = tileSet.Images[0].height;
                        reducedLayerInfo.TextureId = tilesetIndex;
                        toReturn.Layers.Add(reducedLayerInfo);
                    }
                }

                ReducedQuadInfo quad = ReducedQuadInfo.FromSpriteSave(spriteSave, textureWidth, textureHeight);

                if (currentLayer is mapObjectgroup)
                {
                    var asMapObjectGroup = currentLayer as mapObjectgroup;
                    var objectInstance   = asMapObjectGroup.@object[indexInLayer];

                    // skip over any non-sprite objects:
                    while (objectInstance.gid == null)
                    {
                        indexInLayer++;
                        if (indexInLayer >= [email protected])
                        {
                            objectInstance = null;
                            break;
                        }
                        else
                        {
                            objectInstance = asMapObjectGroup.@object[indexInLayer];
                        }
                    }

                    if (objectInstance != null && objectInstance.properties.Count != 0)
                    {
                        var nameProperty = objectInstance.properties.FirstOrDefault(item => item.StrippedNameLower == "name");
                        if (nameProperty != null)
                        {
                            quad.Name = nameProperty.value;
                        }
                        else
                        {
                            quad.Name = spriteSave.Name;

                            bool needsName = string.IsNullOrEmpty(spriteSave.Name);
                            if (needsName)
                            {
                                quad.Name = $"_{currentLayer.Name}runtime{indexInLayer}";
                            }
                        }

                        List <NamedValue> list = new List <NamedValue>();

                        foreach (var property in objectInstance.properties)
                        {
                            list.Add(
                                new NamedValue
                            {
                                Name  = property.StrippedName,
                                Value = property.value,
                                Type  = property.Type
                            }
                                );
                        }

                        quad.QuadSpecificProperties = list;
                    }
                }

                reducedLayerInfo?.Quads.Add(quad);

                indexInLayer++;
            }
        }
        private static void AddTileLayerTiles(TiledMapSave tiledMapSave, ReducedLayerInfo reducedLayerInfo, int i, AbstractMapLayer tiledLayer, Tileset tileSet, int tileWidth, int tileHeight)
        {
            var asMapLayer = tiledLayer as MapLayer;
            var count      = asMapLayer.data[0].tiles.Count;

            for (int dataId = 0; dataId < count; dataId++)
            {
                var dataAtIndex = asMapLayer.data[0].tiles[dataId];

                if (dataAtIndex != 0)
                {
                    ReducedQuadInfo quad = new DataTypes.ReducedQuadInfo();

                    float tileCenterX;
                    float tileCenterY;
                    float tileZ;

                    tiledMapSave.CalculateWorldCoordinates(i, dataId, tileWidth, tileHeight, asMapLayer.width,
                                                           out tileCenterX, out tileCenterY, out tileZ);

                    quad.LeftQuadCoordinate   = tileCenterX - tileWidth / 2.0f;
                    quad.BottomQuadCoordinate = tileCenterY - tileHeight / 2.0f;

                    var gid = dataAtIndex;

                    //quad.FlipFlags = (byte)((gid & 0xf0000000) >> 28);

                    var valueWithoutFlip = gid & 0x0fffffff;

                    spriteSaveForConversion.RotationZ      = 0;
                    spriteSaveForConversion.FlipHorizontal = false;
                    TiledMapSave.SetSpriteTextureCoordinates(gid, spriteSaveForConversion, tileSet, tiledMapSave.orientation);


                    bool isRotated = spriteSaveForConversion.RotationZ != 0;
                    if (isRotated)
                    {
                        quad.FlipFlags = (byte)(quad.FlipFlags | ReducedQuadInfo.FlippedDiagonallyFlag);
                    }

                    var leftTextureCoordinate = System.Math.Min(spriteSaveForConversion.LeftTextureCoordinate, spriteSaveForConversion.RightTextureCoordinate);
                    var topTextureCoordinate  = System.Math.Min(spriteSaveForConversion.TopTextureCoordinate, spriteSaveForConversion.BottomTextureCoordinate);

                    if (spriteSaveForConversion.LeftTextureCoordinate > spriteSaveForConversion.RightTextureCoordinate)
                    {
                        quad.FlipFlags = (byte)(quad.FlipFlags | ReducedQuadInfo.FlippedHorizontallyFlag);
                    }

                    if (spriteSaveForConversion.TopTextureCoordinate > spriteSaveForConversion.BottomTextureCoordinate)
                    {
                        quad.FlipFlags = (byte)(quad.FlipFlags | ReducedQuadInfo.FlippedVerticallyFlag);
                    }

                    quad.LeftTexturePixel = (ushort)FlatRedBall.Math.MathFunctions.RoundToInt(leftTextureCoordinate * tileSet.Images[0].width);
                    quad.TopTexturePixel  = (ushort)FlatRedBall.Math.MathFunctions.RoundToInt(topTextureCoordinate * tileSet.Images[0].height);


                    if (tileSet.TileDictionary.ContainsKey(valueWithoutFlip - tileSet.Firstgid))
                    {
                        var dictionary = tileSet.TileDictionary[valueWithoutFlip - tileSet.Firstgid].PropertyDictionary;
                        if (dictionary.ContainsKey("name"))
                        {
                            quad.Name = tileSet.TileDictionary[valueWithoutFlip - tileSet.Firstgid].PropertyDictionary["name"];
                        }
                        else if (dictionary.ContainsKey("Name"))
                        {
                            quad.Name = tileSet.TileDictionary[valueWithoutFlip - tileSet.Firstgid].PropertyDictionary["Name"];
                        }
                    }

                    reducedLayerInfo?.Quads.Add(quad);
                }
            }
        }