Пример #1
0
        public static string GetTypeAtCell(int cellX, int cellY, int cellZ)
        {
            Layer layer = CurrentMap.Layers[cellZ];

            if (!(layer is TileLayer))
            {
                return("nullType"); // Not an legal Tile Layer
            }

            TileLayer tileLayer = (TileLayer)layer;

            uint GID = tileLayer.GetTileGIDByCell(cellX, cellY);
            int  ID  = TiledHelperMethods.ConvertGIDToID(GID);

            (TiledSet tileSet, uint firstGID) = GetTileSetAndFirstGIDFromID(ID);

            if (tileSet == null)
            {
                return("nullType"); //Should not happen
            }

            return(tileSet.Tiles[ID - (int)firstGID].Type);
        }
Пример #2
0
        private static void DrawTileLayer(SpriteBatch spriteBatch, float z, Layer layer)
        {
            List <uint> firstGlobalIDs = CurrentMap.TiledSets.Keys.ToList <uint>();
            TileLayer   tileLayer      = layer as TileLayer;


            foreach (var chunk in tileLayer.MapChunks)
            {
                for (int tileY = 0; tileY < chunk.Height; tileY++)
                {
                    for (int tileX = 0; tileX < chunk.Width; tileX++)
                    {
                        uint GlobalID = chunk.Data[tileY, tileX];

                        if (GlobalID != 0)
                        {
                            int worldX = (tileX * CurrentMap.TileWidth) + (int)tileLayer.HorizontalOffset + (int)(chunk.Position.X * CurrentMap.TileWidth);
                            int worldY = (tileY * CurrentMap.TileHeight) + (int)tileLayer.VerticalOffset + (int)(chunk.Position.Y * CurrentMap.TileHeight);

                            Rectangle worldRect  = new Rectangle(worldX, worldY, CurrentMap.TileWidth, CurrentMap.TileHeight);
                            Rectangle screenRect = Camera.WorldToScreen(worldRect);

                            //Wrap the screen.
                            // the before and after changes to the position are done for smoothing purposes.
                            screenRect.X += CurrentMap.TileWidth;
                            screenRect.Y += CurrentMap.TileHeight;
                            screenRect.X  = MyMath.Mod(screenRect.X, Camera.WorldRectangle.Width);
                            screenRect.Y  = MyMath.Mod(screenRect.Y, Camera.WorldRectangle.Height);
                            screenRect.X -= CurrentMap.TileWidth;
                            screenRect.Y -= CurrentMap.TileHeight;



                            //Check bounds
                            if (screenRect.X + screenRect.Width > 0 &&
                                screenRect.X < Camera.ViewPortWidth &&
                                screenRect.Y + screenRect.Height > 0 &&
                                screenRect.Y < Camera.ViewPortHeight)
                            {
                                int           tilesetIndex = TiledHelperMethods.GetTileSetIndex(GlobalID, firstGlobalIDs);
                                int           localID      = TiledHelperMethods.ConvertGIDToID(GlobalID, firstGlobalIDs);
                                bool          hFlip        = TiledHelperMethods.isGIDHorizontallyFlipped(GlobalID);
                                bool          vFlip        = TiledHelperMethods.isGIDVerticallyFlipped(GlobalID);
                                bool          dFlip        = TiledHelperMethods.isGIDDiagonallyFlipped(GlobalID);
                                float         rotation     = 0.0f;
                                TiledSet      tiledSet     = (TiledSet)MapManager.TileSets[tilesetIndex];
                                Vector2       origin       = new Vector2(tiledSet.TileWidth / 2, tiledSet.TileHeight / 2);
                                SpriteEffects effects      = SpriteEffects.None;
                                Rectangle     sourceRect   = new Rectangle();

                                // Do we draw the default tile or it's animation tiles
                                if (tiledSet.Tiles[localID].IsAnimated)
                                {
                                    int currentAnimationTileIndex = tiledSet.Tiles[localID].CurrentFrameID;
                                    sourceRect = tiledSet.Tiles[currentAnimationTileIndex].Rectangle();
                                }
                                else
                                {
                                    sourceRect = tiledSet.Tiles[localID].Rectangle();
                                }

                                //Account for the new sprite origins
                                screenRect.X += (int)(origin.X);
                                screenRect.Y += (int)(origin.Y);

                                //Rotate and flip our sprites
                                effects  = CalculateEffects(hFlip, vFlip, dFlip);
                                rotation = CalculateRotation(hFlip, vFlip, dFlip);


                                spriteBatch.Draw(tiledSet.Texture,
                                                 screenRect,
                                                 sourceRect,
                                                 Color.White,
                                                 rotation,
                                                 origin,
                                                 effects,
                                                 z);
                            }
                        }
                    }
                }
            }
        }