简体   繁体   English

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

[英]2D Tile-Based Game Engine - Layer Management in XNA with C#

I'm going to build an advanced 2D Up-Down RPG. 我打算建立一个先进的2D Up-Down RPG。

It'll be a C# + XNA version of my existing 2D Flash RPG Engine ( Adobe Air ). 它将是我现有2D Flash RPG引擎(Adobe Air)的C#+ XNA版本。

Well, in Flash Pro i've simply used different MovieClips for the different Layers but how could i realize that in C# using XNA ? 好吧,在Flash Pro中,我只是为不同的图层使用不同的MovieClip,但我怎么能在使用XNA的C#中实现呢?

I want to be able to use the output ( map-data-file ) from the Flash Map Editor I've made. 我希望能够使用我制作的Flash Map Editor中的输出(map-data-file)。

The Output file looks like this: 输出文件如下所示:

<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>
.
.
.

an so on... 等......

where: 哪里:

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

so i want to simply loop through these lines, save in arrays and add the coresponding sprites to the specific layer, maybe like this: 所以我想简单地遍历这些行,保存在数组中并将相应的精灵添加到特定图层,可能是这样的:

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

how can i realize that ? 我怎么能意识到这一点? and also, how can I manage the z-sorting, cuz further i want to be able to walk under bridges, or drive through tunnels. 而且,我怎样才能管理z排序,因此我希望能够在桥下行走,或者通过隧道行驶。

( like in this image, there could be a ship driving unter the bridge ) (就像在这张图片中,可能有一艘船在桥上行驶)

在此输入图像描述

or even some stairs to get to the next stage ( switch layer ) 甚至一些楼梯进入下一阶段(开关层)

do you guys have any idea or even a refference for me ? 你们对我有什么想法甚至是反思吗?

thx for all your answers ! thx你所有的答案!

EDIT: 编辑:

the layer hirachy shoult look like this : 层hirachy shoult看起来像这样:

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

The SpriteBatch.Draw method has overloads that let you specify Layer SpriteBatch.Draw方法具有允许您指定Layer的重载

Specifically, the bottom two entries on the table here 具体来说, 这里的表格底部有两个条目

The layer is a float between 0 and 1. You'll also need to specify a SpriteSortMode in your SpriteBatch.Begin() . 该图层是一个介于0和1之间的浮点数。您还需要在SpriteBatch.Begin()指定SpriteSortMode Below is an example from a board game I wrote that tints the player tokens and moves the active player to the very front of the stack for when multiple players occupy the same square: 下面是我写的一个棋盘游戏的示例,该棋盘游戏为玩家代币着色并将活动玩家移动到堆叠的最前面,以便当多个玩家占据同一个方块时:

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();

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM