Difference between revisions of "Get tile texture from map"
(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....") |
|||
Line 1: | Line 1: | ||
− | 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 | + | 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: | Declare the following at the top of Game1.cs: | ||
Line 9: | Line 9: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | 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): | |
<syntaxhighlight lang=c#> | <syntaxhighlight lang=c#> | ||
Line 17: | Line 17: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | Inside your check bounds method you should have the first line below: | + | Inside your check bounds method you should have the first line below, add the rest to check the bounds and per pixel collision: |
<syntaxhighlight lang=c#> | <syntaxhighlight lang=c#> | ||
Line 66: | Line 66: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | you can get the IntersectPixels methods from [[Basic Per Pixel Collision]]. |
Revision as of 20:03, 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)
//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;
}
}
you can get the IntersectPixels methods from Basic Per Pixel Collision.