Get tile texture from map

From TRCCompSci - AQA Computer Science
Revision as of 20:01, 27 January 2018 by Admin (talk | contribs) (Created page with "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 check some form of check bounds....")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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 check 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 your need to add the following (assuming you have a texture declared for hero, my tileset is called dungeon):

            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:

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)
                            //you could then do mod division etc to get the 0 & 3 below
                            tilehit = new Rectangle(0 * 16, 3 * 16, 16, 16);
                            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 teture 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;
                        }

                    }