ApplyDiagonalFlip() public method

public ApplyDiagonalFlip ( int orderedTileIndex ) : void
orderedTileIndex int
return void
示例#1
0
        internal static MapDrawableBatch FromReducedLayer(TMXGlueLib.DataTypes.ReducedLayerInfo reducedLayerInfo, LayeredTileMap owner, TMXGlueLib.DataTypes.ReducedTileMapInfo rtmi, string contentManagerName)
        {
            int   tileDimensionWidth  = reducedLayerInfo.TileWidth;
            int   tileDimensionHeight = reducedLayerInfo.TileHeight;
            float quadWidth           = reducedLayerInfo.TileWidth;
            float quadHeight          = reducedLayerInfo.TileHeight;

            string textureName = reducedLayerInfo.Texture;


#if IOS || ANDROID
            textureName = textureName.ToLowerInvariant();
#endif

            Texture2D texture = FlatRedBallServices.Load <Texture2D>(textureName, contentManagerName);

            MapDrawableBatch toReturn = new MapDrawableBatch(reducedLayerInfo.Quads.Count, tileDimensionWidth, tileDimensionHeight, texture);

            toReturn.Name = reducedLayerInfo.Name;

            Vector3 position       = new Vector3();
            Vector2 tileDimensions = new Vector2(quadWidth, quadHeight);


            IEnumerable <TMXGlueLib.DataTypes.ReducedQuadInfo> quads = null;

            if (rtmi.NumberCellsWide > rtmi.NumberCellsTall)
            {
                quads = reducedLayerInfo.Quads.OrderBy(item => item.LeftQuadCoordinate).ToList();
                toReturn.mSortAxis = SortAxis.X;
            }
            else
            {
                quads = reducedLayerInfo.Quads.OrderBy(item => item.BottomQuadCoordinate).ToList();
                toReturn.mSortAxis = SortAxis.Y;
            }

            foreach (var quad in quads)
            {
                position.X = quad.LeftQuadCoordinate;
                position.Y = quad.BottomQuadCoordinate;

                // The Z of the quad should be relative to this layer, not absolute Z values.
                // A multi-layer map will offset the individual layer Z values, the quads should have a Z of 0.
                // position.Z = reducedLayerInfo.Z;


                var textureValues = new Vector4();

                // The purpose of CoordinateAdjustment is to bring the texture values "in", to reduce the chance of adjacent
                // tiles drawing on a given tile quad. If we don't do this, we can get slivers of adjacent colors appearing, causing
                // lines or grid patterns.
                // To bring the values "in" we have to consider rotated quads.
                textureValues.X = CoordinateAdjustment + (float)quad.LeftTexturePixel / (float)texture.Width;                          // Left
                textureValues.Y = -CoordinateAdjustment + (float)(quad.LeftTexturePixel + tileDimensionWidth) / (float)texture.Width;  // Right
                textureValues.Z = CoordinateAdjustment + (float)quad.TopTexturePixel / (float)texture.Height;                          // Top
                textureValues.W = -CoordinateAdjustment + (float)(quad.TopTexturePixel + tileDimensionHeight) / (float)texture.Height; // Bottom

                // pad before doing any rotations/flipping
                const bool pad = true;
                if (pad)
                {
                    const float amountToAdd = .0000001f;
                    textureValues.X += amountToAdd; // Left
                    textureValues.Y -= amountToAdd; // Right
                    textureValues.Z += amountToAdd; // Top
                    textureValues.W -= amountToAdd; // Bottom
                }

                if ((quad.FlipFlags & TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedHorizontallyFlag) == TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedHorizontallyFlag)
                {
                    var temp = textureValues.Y;
                    textureValues.Y = textureValues.X;
                    textureValues.X = temp;
                }

                if ((quad.FlipFlags & TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedVerticallyFlag) == TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedVerticallyFlag)
                {
                    var temp = textureValues.Z;
                    textureValues.Z = textureValues.W;
                    textureValues.W = temp;
                }

                int tileIndex = toReturn.AddTile(position, tileDimensions,
                                                 //quad.LeftTexturePixel, quad.TopTexturePixel, quad.LeftTexturePixel + tileDimensionWidth, quad.TopTexturePixel + tileDimensionHeight);
                                                 textureValues);

                if ((quad.FlipFlags & TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedDiagonallyFlag) == TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedDiagonallyFlag)
                {
                    toReturn.ApplyDiagonalFlip(tileIndex);
                }

                // This was moved to outside of this conversion, to support shaps
                //if (quad.QuadSpecificProperties != null)
                //{
                //    var listToAdd = quad.QuadSpecificProperties.ToList();
                //    listToAdd.Add(new NamedValue { Name = "Name", Value = quad.Name });
                //    owner.Properties.Add(quad.Name, listToAdd);
                //}
                if (quad.RotationDegrees != 0)
                {
                    // Tiled rotates clockwise :(
                    var rotationRadians = -MathHelper.ToRadians(quad.RotationDegrees);

                    Vector3 bottomLeftPos = toReturn.Vertices[tileIndex * 4].Position;

                    Vector3 vertPos = toReturn.Vertices[tileIndex * 4 + 1].Position;
                    MathFunctions.RotatePointAroundPoint(bottomLeftPos, ref vertPos, rotationRadians);
                    toReturn.Vertices[tileIndex * 4 + 1].Position = vertPos;

                    vertPos = toReturn.Vertices[tileIndex * 4 + 2].Position;
                    MathFunctions.RotatePointAroundPoint(bottomLeftPos, ref vertPos, rotationRadians);
                    toReturn.Vertices[tileIndex * 4 + 2].Position = vertPos;

                    vertPos = toReturn.Vertices[tileIndex * 4 + 3].Position;
                    MathFunctions.RotatePointAroundPoint(bottomLeftPos, ref vertPos, rotationRadians);
                    toReturn.Vertices[tileIndex * 4 + 3].Position = vertPos;
                }

                toReturn.RegisterName(quad.Name, tileIndex);
            }

            return(toReturn);
        }
        internal static MapDrawableBatch FromReducedLayer(TMXGlueLib.DataTypes.ReducedLayerInfo reducedLayerInfo, TMXGlueLib.DataTypes.ReducedTileMapInfo rtmi, string contentManagerName)
        {
            int tileDimensionWidth = rtmi.CellWidthInPixels;
            int tileDimensionHeight = rtmi.CellHeightInPixels;
            float quadWidth = rtmi.QuadWidth;
            float quadHeight = rtmi.QuadHeight;

            string textureName = reducedLayerInfo.Texture;


#if IOS || ANDROID

			textureName = textureName.ToLowerInvariant();

#endif

            Texture2D texture = FlatRedBallServices.Load<Texture2D>(textureName, contentManagerName);
#if DEBUG
            if (!MathFunctions.IsPowerOfTwo(texture.Width) || !MathFunctions.IsPowerOfTwo(texture.Height))
            {
                throw new Exception("The dimensions of the texture file " + texture.Name + " are not power of 2!");
            }
#endif
            MapDrawableBatch toReturn = new MapDrawableBatch(reducedLayerInfo.Quads.Count, tileDimensionWidth, tileDimensionHeight, texture);

            toReturn.Name = reducedLayerInfo.Name;

            Vector3 position = new Vector3();
            Vector2 tileDimensions = new Vector2(quadWidth, quadHeight);


            IEnumerable<TMXGlueLib.DataTypes.ReducedQuadInfo> quads = null;

            if (rtmi.NumberCellsWide > rtmi.NumberCellsTall)
            {
                quads = reducedLayerInfo.Quads.OrderBy(item => item.LeftQuadCoordinate);
                toReturn.mSortAxis = SortAxis.X;
            }
            else
            {
                quads = reducedLayerInfo.Quads.OrderBy(item => item.BottomQuadCoordinate);
                toReturn.mSortAxis = SortAxis.Y;
            }

            foreach (var quad in quads)
            {
                position.X = quad.LeftQuadCoordinate;
                position.Y = quad.BottomQuadCoordinate;

                // The Z of the quad should be relative to this layer, not absolute Z values.
                // A multi-layer map will offset the individual layer Z values, the quads should have a Z of 0.
                // position.Z = reducedLayerInfo.Z;

                var textureValues = new Vector4();
                textureValues.X = (float)quad.LeftTexturePixel / (float)texture.Width; // Left
                textureValues.Y = (float)(quad.LeftTexturePixel + tileDimensionWidth) / (float)texture.Width; // Right
                textureValues.Z = (float)quad.TopTexturePixel / (float)texture.Height; // Top
                textureValues.W = (float)(quad.TopTexturePixel + tileDimensionHeight) / (float)texture.Height; // Bottom

                // pad before doing any rotations/flipping
                const bool pad = true;
                if (pad)
                {
                    const float amountToAdd = .0000001f;
                    textureValues.X += amountToAdd; // Left
                    textureValues.Y -= amountToAdd; // Right
                    textureValues.Z += amountToAdd; // Top
                    textureValues.W -= amountToAdd; // Bottom
                }

                if ((quad.FlipFlags & TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedHorizontallyFlag) == TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedHorizontallyFlag)
                {
                    var temp = textureValues.Y;
                    textureValues.Y = textureValues.X;
                    textureValues.X = temp;
                }

                if ((quad.FlipFlags & TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedVerticallyFlag) == TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedVerticallyFlag)
                {
                    var temp = textureValues.Z;
                    textureValues.Z = textureValues.W;
                    textureValues.W = temp;
                }

                int tileIndex = toReturn.AddTile(position, tileDimensions,
                    //quad.LeftTexturePixel, quad.TopTexturePixel, quad.LeftTexturePixel + tileDimensionWidth, quad.TopTexturePixel + tileDimensionHeight);
                    textureValues);

                if ((quad.FlipFlags & TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedDiagonallyFlag) == TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedDiagonallyFlag)
                {
                    toReturn.ApplyDiagonalFlip(tileIndex);
                }

                toReturn.RegisterName(quad.Name, tileIndex);
            }

            return toReturn;
        }
        internal static MapDrawableBatch FromReducedLayer(TMXGlueLib.DataTypes.ReducedLayerInfo reducedLayerInfo, TMXGlueLib.DataTypes.ReducedTileMapInfo rtmi, string contentManagerName)
        {
            int   tileDimensionWidth  = rtmi.CellWidthInPixels;
            int   tileDimensionHeight = rtmi.CellHeightInPixels;
            float quadWidth           = rtmi.QuadWidth;
            float quadHeight          = rtmi.QuadHeight;

            string textureName = reducedLayerInfo.Texture;


#if IOS || ANDROID
            textureName = textureName.ToLowerInvariant();
#endif

            Texture2D texture = FlatRedBallServices.Load <Texture2D>(textureName, contentManagerName);
#if DEBUG
            if (!MathFunctions.IsPowerOfTwo(texture.Width) || !MathFunctions.IsPowerOfTwo(texture.Height))
            {
                throw new Exception("The dimensions of the texture file " + texture.Name + " are not power of 2!");
            }
#endif
            MapDrawableBatch toReturn = new MapDrawableBatch(reducedLayerInfo.Quads.Count, tileDimensionWidth, tileDimensionHeight, texture);

            toReturn.Name = reducedLayerInfo.Name;

            Vector3 position       = new Vector3();
            Vector2 tileDimensions = new Vector2(quadWidth, quadHeight);


            IEnumerable <TMXGlueLib.DataTypes.ReducedQuadInfo> quads = null;

            if (rtmi.NumberCellsWide > rtmi.NumberCellsTall)
            {
                quads = reducedLayerInfo.Quads.OrderBy(item => item.LeftQuadCoordinate);
                toReturn.mSortAxis = SortAxis.X;
            }
            else
            {
                quads = reducedLayerInfo.Quads.OrderBy(item => item.BottomQuadCoordinate);
                toReturn.mSortAxis = SortAxis.Y;
            }

            foreach (var quad in quads)
            {
                position.X = quad.LeftQuadCoordinate;
                position.Y = quad.BottomQuadCoordinate;

                // The Z of the quad should be relative to this layer, not absolute Z values.
                // A multi-layer map will offset the individual layer Z values, the quads should have a Z of 0.
                // position.Z = reducedLayerInfo.Z;

                var textureValues = new Vector4();
                textureValues.X = (float)quad.LeftTexturePixel / (float)texture.Width;                         // Left
                textureValues.Y = (float)(quad.LeftTexturePixel + tileDimensionWidth) / (float)texture.Width;  // Right
                textureValues.Z = (float)quad.TopTexturePixel / (float)texture.Height;                         // Top
                textureValues.W = (float)(quad.TopTexturePixel + tileDimensionHeight) / (float)texture.Height; // Bottom

                // pad before doing any rotations/flipping
                const bool pad = true;
                if (pad)
                {
                    const float amountToAdd = .0000001f;
                    textureValues.X += amountToAdd; // Left
                    textureValues.Y -= amountToAdd; // Right
                    textureValues.Z += amountToAdd; // Top
                    textureValues.W -= amountToAdd; // Bottom
                }

                if ((quad.FlipFlags & TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedHorizontallyFlag) == TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedHorizontallyFlag)
                {
                    var temp = textureValues.Y;
                    textureValues.Y = textureValues.X;
                    textureValues.X = temp;
                }

                if ((quad.FlipFlags & TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedVerticallyFlag) == TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedVerticallyFlag)
                {
                    var temp = textureValues.Z;
                    textureValues.Z = textureValues.W;
                    textureValues.W = temp;
                }

                int tileIndex = toReturn.AddTile(position, tileDimensions,
                                                 //quad.LeftTexturePixel, quad.TopTexturePixel, quad.LeftTexturePixel + tileDimensionWidth, quad.TopTexturePixel + tileDimensionHeight);
                                                 textureValues);

                if ((quad.FlipFlags & TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedDiagonallyFlag) == TMXGlueLib.DataTypes.ReducedQuadInfo.FlippedDiagonallyFlag)
                {
                    toReturn.ApplyDiagonalFlip(tileIndex);
                }

                toReturn.RegisterName(quad.Name, tileIndex);
            }

            return(toReturn);
        }