Game Development Community

Tile alpha

by baylor wetzel · in Torque Game Builder · 07/22/2008 (9:58 pm) · 2 replies

Is there a way to set the alpha of a specific tile? i'm doing a fog of war with a tile map of black squares and the plan was to adjust the alpha so setting the alpha for the entire tile layer isn't quite right. But maybe i need to create transparent and partially transparent tiles and swap those out?

#1
07/23/2008 (9:51 am)
You can't adjust the alpha of a single tile but as you said, you can change the image.
#2
07/23/2008 (10:02 am)
Bummer. Oh well, i've got my workaround in place. Because i'm just using it for a fog of war, i store the alpha in the custom data (there's no other data i need to store there). i thought i could save resources by using clearTile rather than setting the image to a transparent bitmap when a tile was revealed but that generates a lot of warnings and makes my log ugly so i just made a tiny transparent image.

In case anyone else is interested, here's the code i'm using:

function move(%direction)
{
	fogOfWarTileLayer.resetVisibility();
...stuff...
	updateFogOfWar(%x, %y);
}

/*** Before each move, reset the fog - black for unknown, gray for places seen
The actual fully revealed tiles will be handled by a later function */
function fogOfWarTileLayer::resetVisibility(%this)
{
	for (%x=0; %x<%this.getTileCountX(); %x++)
		for (%y=0; %y<%this.getTileCountY(); %y++)
		{
			%alpha = %this.getTileCustomData(%x, %y);

			//--- If it's been seen before, set alpha to 0.5
			%alpha = t2DGetMin(%alpha, 1);
			//--- If it was visible, set it back to 0.5
			%alpha = t2DGetMax(%alpha, 0.5);
			
			%this.setTileAlpha(%x, %y, %alpha);
		}
}

/*** Alpha is actually set by swapping bitmaps */
function fogOfWarTileLayer::setTileAlpha(%this, %x, %y, %alpha)
{
	%unknownImage = "fogOfWarUnknownImageMap";
	%knownImage   = "fogOfWarKnownImageMap";
	%visible      = "transparentImageMap";

	if (1==%alpha)
	{
		%this.setStaticTile(%x, %y, %unknownImage);
		%this.setTileCustomData(%x, %y, 1);
	}
	if (0.5==%alpha)
	{
		%this.setStaticTile(%x, %y, %knownImage);
		%this.setTileCustomData(%x, %y, 0.5);
	}
	if (0==%alpha)
	{
		%this.setStaticTile(%x, %y, %visible);
		%this.setTileCustomData(%x, %y, 0);
	}
}

/*** For each tile that can be seen from this tile, make
the tile fully visible (alpha=0). This implementation assumes 
you can only see the tile border around you, like in the old
Ultima games. It's totally non-portable but this was just
a proof of concept so i can live with it */
function updateFogOfWar(%x, %y)
{
	for (%xOffset=-1; %xOffset<=1; %xOffset++)
		for (%yOffset=-1; %yOffset<=1; %yOffset++)
		{
			fogOfWarTileLayer.setTileAlpha(%x+%xOffset, %y+%yOffset, 0);
		}
}