Difference between revisions of "Get tile texture from map"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
Line 36: Line 36:
 
                 //you could % this by the number of rows in your tilesheet
 
                 //you could % this by the number of rows in your tilesheet
 
                 tilehit = new Rectangle(0 * 16, 3 * 16, 16, 16);
 
                 tilehit = new Rectangle(0 * 16, 3 * 16, 16, 16);
 +
               
 +
                //set texture to an empty texture of the right size for new tile
 
                 tiletext = testtext;
 
                 tiletext = testtext;
 
                 //this gets all the data and adds it to TileTextureData
 
                 //this gets all the data and adds it to TileTextureData

Revision as of 20:17, 27 January 2018

Getting a texture for a tile is possible, its only taken about 6 hours to figure out the best method. This is assuming you have a game with some form of check bounds.

Declare the following at the top of Game1.cs:

Texture2D tilesheet, tiletexture;
Rectangle tilehit;
Color[] TileTextureData, PlayerTextureData;

In load content you need to add the following (assuming you have a texture declared for hero, my tileset is called dungeon in my map):

            PlayerTextureData = new Color[hero.Width * hero.Height];
            hero.GetData(pData);
            tilesheet = map.Tilesets["dungeon"].Texture;

Inside your check bounds method you should have the first line below, add the rest to check the bounds and per pixel collision:

if (collision.GetTile(x, y) != 0)
        {
            //Rectangle for position of the tile on the screen (my tiles are 16x16)
            Rectangle tilerec = new Rectangle(x*16,y*16,16,16);

            //Check if it intersects first
            if (tilerec.Intersects(playrec))
            {
                //initialize the the texture data for the tilesheet
                TileTextureData = new Color[tilesheet.Width * tilesheet.Height];
                //position of tile to test on your tileseet
                //you could get this from collision.GetTile(x,y) -1
                //you could then do mod division etc to get the 0 & 3 below
                //you could \ this by number of tiles in a row on tile sheet
                //you could % this by the number of rows in your tilesheet
                tilehit = new Rectangle(0 * 16, 3 * 16, 16, 16);
                
                //set texture to an empty texture of the right size for new tile
                tiletext = testtext;
                //this gets all the data and adds it to TileTextureData
                tilesheet.GetData(TileTextureData);

                //create a new color array to match size of tile (1D array of each pixel)
                Color[] test = new Color[16 * 16];
                //set counter for location in new Color array
                int count = 0;

                //for loops to cycle through every pixel in the tile
                for (int c = tilehit.Top; c < tilehit.Top+16; c++)
                {
                    for (int r = tilehit.Left; r < tilehit.Left+16; r++)
                    {
                        //get current pixel from texture data
                        Color colorA = TileTextureData[r + (c  * tilesheet.Width)];
                        //add to our new array
                        test[count] = colorA;
                        count++;
                     }
                  }

                  //add colour data to make texture for the tile
                  tiletext.SetData(test);

                  //use intersectpixels to test new texture
                  if(IntersectPixels(playrec,PlayerTextureData,tilerec, test))
                        check = true;
            }

        }

you can get the IntersectPixels methods from Basic Per Pixel Collision.