所以我正在制作一个基于平铺的游戏,我想在平铺上添加一些假阴影.这有点难以解释所以我会用图片来做:
让我们说这是我的瓷砖世界:

而且我希望它有这样的阴影:

因为世界是基于图块的,所以我可以将所有阴影部分分割成单独的图像:

但是现在我不知道如何将它带到代码中.嗯,实际上我确实有想法,但它们令人难以置信的乏味,并且它们无法以最佳方式工作.
我尝试了一个巨大的if语句......
bool ul = adjacentBlocks[0, 0] == Block.Type.Rock; //Upper Left
bool um = adjacentBlocks[1, 0] == Block.Type.Rock; //Upper Middle
bool ur = adjacentBlocks[2, 0] == Block.Type.Rock; //Upper Right
bool ml = adjacentBlocks[0, 1] == Block.Type.Rock; //Center Left
//bool cm = adjacentBlocks[1, 1] == Block.Type.Rock; //CURRENT BLOCK - NOT NEEDED
bool mr = adjacentBlocks[2, 1] == Block.Type.Rock; //Center Right
bool ll = adjacentBlocks[0, 2] == Block.Type.Rock; //Lower Left
bool lm = adjacentBlocks[1, 2] == Block.Type.Rock; //Lower Middle
bool lr = adjacentBlocks[2, 2] == Block.Type.Rock; //Lower Right
if (ml) { texture = "Horizontal"; flipX = false; flipY = false; }
if (mr) { texture = "Horizontal"; flipX = true; flipY = false; }
if (um) { texture = "Vertical"; flipX = false; flipY = false; }
if (lm) { texture = "Vertical"; flipX = false; flipY = true; }
if (ml && ul && um) texture = "HorizontalVertical";
//More if statements I can't be bothered to write
if (ul && um && ur && ml && mr && ll && lm & lr) texture = "Full";
Run Code Online (Sandbox Code Playgroud)
还有一张庞大的查询表......
var table = new List<TextureBlockLayout>
{
new TextureBlockLayout("Horizontal", false, false, new[,]
{
{ true, true, false },
{ true, true, false },
{ true, true, false }
}),
new TextureBlockLayout("Horizontal", true, false, new[,]
{
{ false, true, true },
{ false, true, true },
{ false, true, true }
}),
new TextureBlockLayout("Full", false, false, new[,]
{
{ true, true, true },
{ true, true, true },
{ true, true, true }
})
};
Run Code Online (Sandbox Code Playgroud)
但要么我做错了,要么就是拒绝工作.有任何想法吗?
每个图块有八个邻居。每个邻居都有两种可能的状态。将邻居的状态映射为字节中的位,并使用该字节作为 256 元素查找表的索引。
是的,这是“蛮力”解决方案,您可能可以使用一些更聪明的方法来使用更小的表。但 256 个元素并不算多(您可以轻松地从数据文件加载它),而且这种方法的优点在于它完全通用 — 如果您愿意,您可以让所有 256 个图块看起来略有不同。
好的,仔细看看示例图块的着色方式,看起来您实际上只需要 4 位(因此需要 16 个元素的表):
即使这 16 个图块中的一些仍然是彼此的旋转/镜像版本,但将方向与图块索引一起存储在表中可能是最简单的,而不是尝试在代码中计算它。
| 归档时间: |
|
| 查看次数: |
201 次 |
| 最近记录: |