Пример #1
0
        internal static MapDrawableBatch FromReducedLayer(TMXGlueLib.DataTypes.ReducedLayerInfo reducedLayerInfo, string contentManagerName, int tileDimensionWidth, int tileDimensionHeight, float quadWidth, float quadHeight)
        {
            string textureName = reducedLayerInfo.Texture;

            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);
            foreach (var quad in reducedLayerInfo.Quads)
            {
                position.X = quad.LeftQuadCoordinate;
                position.Y = quad.BottomQuadCorodinate;
                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


                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
                }


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

            return(toReturn);
        }
Пример #2
0
        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.RotateTextureCoordinatesCounterclockwise(tileIndex);
                }

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

            return(toReturn);
        }