Example #1
0
    void LoadMapPreview(string _path)
    {
        m_haveLoadedImage = false;

        string jsonString = System.IO.File.ReadAllText(_path);
        Dictionary <string, object> json = (Dictionary <string, object>)MiniJSON.Json.Deserialize(jsonString);

        m_mapTexture = null;

        m_openMapName = System.IO.Path.GetFileNameWithoutExtension(_path);
        if (json.ContainsKey("tilesets") == false)
        {
            return;
        }

        //
        // Load tile bank (only one tile bank for each map for now)
        //
        List <object> tilesetsJson = (List <object>)json["tilesets"];
        Dictionary <string, object> tilesetJson = (Dictionary <string, object>)tilesetsJson[0];
        //Debug.Log ("we have tilesets" );
        //Debug.Log ("tilesets=" + tilesetsJson );
        //Debug.Log ("tileset=" + tilesetJson );
        string imageFileName = (string)tilesetJson["image"];
        string imageFullPath = System.IO.Path.GetDirectoryName(_path) + System.IO.Path.DirectorySeparatorChar + imageFileName;

        //Debug.Log ("image file name=" + imageFileName + ", imagefullpath=" + imageFullPath );
        LoadBMP(imageFullPath);

        // Create map texture
        int map_tiles_w  = ((int)(long)json["width"]);
        int map_tiles_h  = ((int)(long)json["height"]);
        int map_pixels_w = map_tiles_w * 8;
        int map_pixels_h = map_tiles_h * 8;

        m_mapTexture                  = new Texture2D(map_pixels_w, map_pixels_h, TextureFormat.ARGB32, false);
        m_mapTexture.filterMode       = FilterMode.Point;
        m_collisionTexture            = new Texture2D(map_pixels_w, map_pixels_h, TextureFormat.ARGB32, false);
        m_collisionTexture.filterMode = FilterMode.Point;
        m_mapWindowRect               = new Rect(m_tileBankWindowRect.x, m_tileBankWindowRect.y, 10 + map_pixels_w, 25 + map_pixels_h + 25);
        float add = m_mapWindowRect.x + m_mapWindowRect.width - m_projectWindowWidth - m_windowPadding;

        m_tileBankWindowRect.x += add;
        m_paletteRemapRect.x   += add;
        m_imageSettingsRect.x  += add;
        //m_projectWindowRect.x += add;

        Texture2D collTiles = AssetDatabase.LoadAssetAtPath("assets/collisiontiles.psd", typeof(Texture2D)) as Texture2D;

        Debug.Log("colltiles = " + collTiles);


        // Find each layer
        List <object> layersJson = (List <object>)json["layers"];
        Dictionary <string, object> layerJson = (Dictionary <string, object>)layersJson[0];
        List <object> layerData = (List <object>)layerJson["data"];
        int           tile_x, tile_y;

        for (tile_y = 0; tile_y < map_tiles_h; tile_y++)
        {
            for (tile_x = 0; tile_x < map_tiles_w; tile_x++)
            {
                int i       = tile_y * map_tiles_w + tile_x;
                int tile_id = (int)(long)layerData[i];
                tile_id--;
                TileInstance tileInstance = m_tileBank.m_allTileInstances[tile_id];

                int tile_pixel_x;
                int tile_pixel_y;

                int collTileId  = CollisionMap.GetCollisionTileIndexFromVisualIndex(tile_id);
                int coll_tile_y = (collTileId >> 3) & 0x07;
                int coll_tile_x = (collTileId) & 0x07;
                coll_tile_x <<= 3;
                coll_tile_y <<= 3;

                for (tile_pixel_y = 0; tile_pixel_y < 8; tile_pixel_y++)
                {
                    for (tile_pixel_x = 0; tile_pixel_x < 8; tile_pixel_x++)
                    {
                        int   tile_pixel_i = (tile_pixel_y * 8) + tile_pixel_x;
                        byte  palindex     = tileInstance.m_tile.m_pixels[tile_pixel_i];
                        Color col          = m_currentImageData.m_palette[palindex];

                        int dst_x = (tile_x * 8) + tile_pixel_x;
                        int dst_y = (tile_y * 8) + tile_pixel_y;
                        m_mapTexture.SetPixel(dst_x, map_pixels_h - 1 - dst_y, col);

                        int   src_coll_x = coll_tile_x + tile_pixel_x;
                        int   src_coll_y = coll_tile_y + tile_pixel_y;
                        Color collCol    = collTiles.GetPixel(src_coll_x, collTiles.height - 1 - src_coll_y);
                        m_collisionTexture.SetPixel(dst_x, map_pixels_h - 1 - dst_y, collCol);
                    }
                }
            }
        }

        m_mapTexture.Apply();
        m_collisionTexture.Apply();



        /*
         * m_openImageName = "<Untitled>";
         *
         * // Load corresponding config first as it have information on how the image should be loaded
         * m_currentImageConfig = new PalettizedImageConfig( _path + ".config" );
         *
         * m_currentImageData = PalettizedImage.LoadBMP( _path, m_currentImageConfig );
         * if( m_currentImageData != null )
         * {
         *      //
         *      m_currentImageConfig.SetImage( m_currentImageData );
         *
         *      //
         *      m_currentFramesString = m_currentImageConfig.GetNumFrames().ToString();
         *
         *      //
         *      m_openImageName = System.IO.Path.GetFileNameWithoutExtension( _path );
         *      m_tileBankWindowRect = new Rect( m_projectWindowWidth + (m_windowPadding*2.0f), m_windowTop, m_currentImageData.m_width*2.0f+10.0f, m_currentImageData.m_height*2.0f+10.0f+15.0f );
         *      m_imageSettingsRect = new Rect( m_tileBankWindowRect.x + m_tileBankWindowRect.width + m_windowPadding, m_tileBankWindowRect.y, 200.0f, 100.0f );
         *      m_paletteRemapRect = new Rect( m_imageSettingsRect.x + m_imageSettingsRect.width + m_windowPadding, m_imageSettingsRect.y, 100.0f, 15.0f + (16.0f * 30.0f) );
         *
         *      //
         *      m_planarImage = new PlanarImage( m_currentImageData);
         *      bool OptimizedTilebank = (m_currentImageConfig.m_importAsSprite == false); // If we import the image as a sprite we should not optimize the tile bank
         *      m_tileBank = new TileBank( m_currentImageData, OptimizedTilebank );
         *      m_tileMap = new TileMap( m_tileBank, m_currentImageData );
         *      m_tilePalette = new TilePalette( m_currentImageData );
         *
         *      //
         *      int w, h;
         *      w = m_currentImageData.m_width;
         *      h = m_currentImageData.m_height;
         *
         *      //
         *      m_imageTexture = new Texture2D( w, h, TextureFormat.ARGB32, false );
         *      m_imageTexture.filterMode = FilterMode.Point;
         *
         *      //
         *      int x, y;
         *      for( y=0; y<h; y++ )
         *      {
         *              for( x=0; x<w; x++ )
         *              {
         *                      int ii = ((h-1-y)*w)+x;
         *                      int ic = m_currentImageData.m_image[ ii ];
         *                      Color col = m_currentImageData.m_palette[ ic ];
         *                      m_imageTexture.SetPixel( x, y, col );
         *              }
         *      }
         *
         *      //
         *      m_imageTexture.Apply();
         * }
         */
    }