RPG游戏中对话框的简单实现原理
额,大地无敌考虑到了命运艺术中由于没有人配音所以战斗紧张来不及看对话的情况,加入了类似RPG游戏的对话框系统~~~
就是说~~出现信息以后,按回车键出现下一条信息-_-
简陋的实现图
原理是这样的~~~
使用一个List装string或自己定义的更高级的文字类来储存文字
发生对话时使用List.Add把一条条的对话内容加入!
绘出时绘出第[0]项的文字!
按下回车后使用List的RemoveAt(0)就可以丢掉第一条信息然后把第二条换上来了
于是对话框类就出现了!这是命运艺术的,供参考原理~~
public class GameMessageBox:UI
{
Texture2D blank;
public GameWorld gameWorld;
SpriteBatch spriteBatch;
public Vector2 Size;
public Color Color2 = Color.White;
public Color Color = new Color(Color.Blue, 210);
public bool Visible = false;
public List Text = new List(50);
bool prot = false; //额,这是为了防止命运艺术中的一些BUG的出现,不用理会
public GameMessageBox(GameWorld gameWorld)
{
this.gameWorld = gameWorld;
blank = gameWorld.game.Content.Load(@"blank"); //有时一个1像素白点图片很有用..
position = new Vector2(50, gameWorld.game.GraphicsDevice.Viewport.Height * 0.67f);
Size = new Vector2(gameWorld.game.GraphicsDevice.Viewport.Width - 100, gameWorld.game.GraphicsDevice.Viewport.Height * 0.25f);
spriteBatch = gameWorld.spriteBatch;
}
public void AddText(AODText t)
{
Text.Add(t);
}
public override void Update(GameTime gameTime)
{
if (Visible)
{
if (Text.Count == 0)
{
Visible = false;
prot = false;
}
else if ((PlayerControl.IsKeyPressed(Keys.Enter) || PlayerControl.IsKeyPressed(Keys.Space)) && prot) //判断是否按下回车或空格
{
Text.RemoveAt(0); //移除第[0]项!
prot = false;
}
else
{
prot = true;
}
}
}
public override void Draw(GameTime gameTime)
{
if (Visible)
{
Rectangle t = GetRectangle();
spriteBatch.Begin();
spriteBatch.Draw(blank,t, Color); //画背景
spriteBatch.Draw(blank, new Rectangle(t.X, t.Y, t.Width, 2), Color2); //画边框
spriteBatch.Draw(blank, new Rectangle(t.X, t.Y, 2, t.Height), Color2); //画边框
spriteBatch.Draw(blank, new Rectangle(t.X, t.Y + t.Height - 2, t.Width, 2), Color2); //画边框
spriteBatch.Draw(blank, new Rectangle(t.X + t.Width - 2, t.Y, 2, t.Height), Color2); //画边框
spriteBatch.End();
if (Text.Count >= 1)
{
Text[0].position = position + new Vector2(20, 20);
Text[0].Draw(gameTime);//画出第[0]项文字
}
}
}
Rectangle GetRectangle()
{
return new Rectangle((int)position.X, (int)position.Y, (int)Size.X, (int)Size.Y);
}
}