我正在研究基于磁贴的挖掘机游戏,我希望在玩家周围使用逐渐照明,想想Terraria。我目前只是将图像与矩形重叠,其中该矩形上的alpha是我希望拥有的亮度(0.01 =黑色,1 =明亮),这可以正常工作。这不是我如何设置我需要帮助的照明,它是如何编写一个紧凑而有效的算法,在块越靠近播放器时让我获得更大的数字,并且更小的数字街区远离玩家。数字应在(1 - 0.01)范围内。每次玩家移动时我都会调用以下方法:
public void calculateBlockBrightness(int playerInRow, int playerInCol) {
int affectedBlocks = 5;
float brightness;
// Loops through all blocks within (affectedBlocks) blocks of the player
for (int row = playerInRow - affectedBlocks; row <= playerInRow + affectedBlocks; row++) {
for (int col = playerInCol - affectedBlocks; col <= playerInCol + affectedBlocks; col++) {
// Not all blocks should be affected by this method
if (blockNotRelevantForBrightness(row, col))
continue;
// If the block is within (affectedBlocks - 1) blocks of the player
if (row >= playerInRow - (affectedBlocks - 1) && row <= playerInRow + (affectedBlocks - 1)
&& col >= playerInCol - (affectedBlocks - 1) && col <= playerInCol + (affectedBlocks - 1)) {
/*
* Algorithm
*/
blocks[row][col].setBrightnessPercentage(brightness);
// Reset the "outer layer" blocks to default brightness.
} else {
blocks[row][col].setBrightnessPercentage(blocks[row][col].getBrightnessDefault());
}
}
}
}
答案 0 :(得分:0)
在这种情况下,通过两个步骤计算所需的值通常是有益的:
此处的插值是&#34;相对&#34; 距离,用于在最小和最大亮度之间进行插值。
两个图块之间的距离可以计算为
sqrt(rowDifference² + columnDifference²)
(顺便说一句,这可以方便地使用Math#hypot
计算。)
受影响的磁贴在水平和垂直方向上对播放器磁贴的最大距离为(affectedBlocks - 1)
。对于一对图块,即图块(row, col)
和(playerInRow, playerInCol)
,现在可以计算这些图块之间的距离,并将其除以最大距离,以获得相对距离作为[0]中的值,1]。该值可用于计算适当的亮度。
private float computeBrightness(
int row, int col, int playerRow, int playerCol, int affectedBlocks)
{
// Compute the distance of the tile (row, col) to the player tile
double distance = Math.hypot(row - playerRow, col - playerCol);
// Compute the maximum possible distance of a tile
double maxDistance = Math.hypot(affectedBlocks - 1, affectedBlocks -1);
// Compute the relative distance, as a value in [0,1]
double relative = distance / maxDistance;
// Compute the brightness for the relative distance
double minBrightness = 0.01;
double maxBrightness = 1.0;
double brightness =
maxBrightness - relative * (maxBrightness - minBrightness);
System.out.println(
"For " + row + " " + col + " brightness is " + brightness);
return (float)brightness;
}
附注:假设affectedBlocks
是常数,最大距离也是常数,并且可以存储为常数值字段,例如:
private static final int AFFECTED_BLOCKS = 5;
private static final double MAX_DISTANCE =
Math.hypot(AFFECTED_BLOCKS - 1, AFFECTED_BLOCKS - 1);