gpt4 book ai didi

c# - 2D 基于图 block 的游戏引擎 - 使用 C# 在 XNA 中进行层管理

转载 作者:行者123 更新时间:2023-11-30 12:49:16 26 4
gpt4 key购买 nike

我要构建一个高级的 2D Up-Down RPG。

它将是我现有的 2D Flash RPG 引擎 (Adobe Air) 的 C# + XNA 版本。

好吧,在 Flash Pro 中,我只是为不同的层使用了不同的 MovieClip,但我怎么能在 C# 中使用 XNA 实现这一点呢?

我希望能够使用我制作的 Flash map 编辑器的输出 ( map-data-file )。

输出文件如下所示:

<layer1>
0.0.0.A.A.A.A.B.B.A.A.0.0.0
0.0.0.A.A.A.A.B.B.B.A.A.0.0
0.0.A.A.A.B.B.B.B.B.B.A.0.0
0.A.A.A.A.B.B.B.B.B.B.A.A.0
0.A.A.A.A.B.B.B.B.B.B.A.A.0
0.A.A.A.A.A.B.B.B.B.B:B.A.0
0.0.A.A.A.A.B.B.B.B.A.A.A.0
0.0.A.A.A.A.A.B.B.A.A.A.0.0
0.0.0.A.A.A.A.B.B.A.A.0.0.0

<layer2>
. . . . .
. . .
.
.

<layer3>
.
.
.

等等...

哪里:

0 = Blank;
A = Gras;
B = Water;

所以我想简单地遍历这些行,保存在数组中并将对应的 Sprite 添加到特定层,可能像这样:

Layers[2].Add( tile, x, y );

我怎么能意识到这一点?而且,我该如何管理 z 排序,因为我还希望能够在桥下行走,或开车穿过隧道。

(就像这张图,桥下可能有一艘船在行驶)

enter image description here

甚至是一些楼梯才能到达下一阶段(切换层)

你们对我有什么想法甚至引用吗?

谢谢大家的回答!

编辑:

层 hirachy 应该是这样的:

map{
Layer[0] { // "bg"
xyArray{ .... }
}
Layer[1] { // "static" - bottom part / stairs of the bridge
xyArray{ .... }
}
Layer[2] { // "items"
xyArray{ .... }
}
Layer[3] { // "player"
xyArray{ .... }
}
Layer[4] { // "static2" - top part of bridge
xyArray{ .... }
}
}

// if the player is ON a tile that hat a Stair funktion, the "player" layer will be
// moved on top of the "static2" layer, so 'Layer[3].Z = 4;' and 'Layer[4].Z = 3;'
// like i showed in the Switch funktion up there

最佳答案

SpriteBatch.Draw 方法具有可让您指定图层的重载

具体来说,表中底部的两个条目 here

图层是一个介于 0 和 1 之间的 float 。您还需要在 SpriteBatch.Begin() 中指定一个 SpriteSortMode。下面是我编写的一个棋盘游戏的示例,当多个玩家占据同一个方 block 时,它会为玩家标记着色并将事件玩家移动到堆栈的最前面:

if (i == currentPlayer)
{
SpriteColor = Color.White;
spriteDepth = 0f; // Front
}
else
{
SpriteColor = Color.Gray;
spriteDepth = 0.1f; // Back
}
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(players[i].Sprite, SquareCentre, null, SpriteColor, 0f, SpriteCentre,0.5f, SpriteEffects.None, spriteDepth);
spriteBatch.End();

希望这对您有所帮助。

关于c# - 2D 基于图 block 的游戏引擎 - 使用 C# 在 XNA 中进行层管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11967096/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com