开发者

XNA C# Creating a Heads Up Display

开发者 https://www.devze.com 2023-04-05 10:26 出处:网络
I was wondering how to approach creating a HUD. I currently have health, mana, and experience bar drawn to the screen at set coordinates. Downside of this is that when the camera pans the bars stay at

I was wondering how to approach creating a HUD. I currently have health, mana, and experience bar drawn to the screen at set coordinates. Downside of this is that when the camera pans the bars stay at their set coordinates, I want them to adjust to the viewport or not be influenced by positions but just simply drawn to the screen.

  • Edit I managed to get the HUD to adjust using the camera's x and y coordinates. I've created a separate class for drawing the HUD, but now they don't adjust.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using GameOne.Components;
    
    
    
    
    namespace GameOne.GameScreens
    {
    
    public class HUD : BaseGameState
    {
    
    Player player;
    
    Texture2D开发者_JS百科 HealthBar;
    Texture2D HealthBarPositive;
    Texture2D HealthBarNegative;
    Texture2D ManaBar;
    Texture2D ManaBarPositive;
    Texture2D ManaBarNegative;
    Texture2D ExpBar;
    Texture2D ExpBarPositive;
    
    int CurrentHealth = 100;
    int CurrentMana = 45;
    int CurrentExp = 0;
    
    public HUD(Game game, GameStateManager manager)
        : base(game, manager)
    {
        player = new Player(game);
    }
    
    public void LoadContent()
    {
        base.LoadContent();
    
        HealthBar = Game.Content.Load<Texture2D>(@"GUI\healthBar");
        HealthBarPositive = Game.Content.Load<Texture2D>(@"GUI\healthBarPositive");
        HealthBarNegative = Game.Content.Load<Texture2D>(@"GUI\healthBarNegative");
        ManaBar = Game.Content.Load<Texture2D>(@"GUI\manaBar");
        ManaBarPositive = Game.Content.Load<Texture2D>(@"GUI\manaBarPositive");
        ManaBarNegative = Game.Content.Load<Texture2D>(@"GUI\manaBarNegative");
        ExpBar = Game.Content.Load<Texture2D>(@"GUI\expBar");
        ExpBarPositive = Game.Content.Load<Texture2D>(@"GUI\expBarPositive");
    }
    
    public void Update(GameTime gameTime)
    {
        if (InputHandler.KeyDown(Keys.F1))
        {
            CurrentHealth += 1;
        }
    
        if (InputHandler.KeyDown(Keys.F2))
        {
            CurrentHealth -= 1;
        }
    
        if (InputHandler.KeyDown(Keys.F3))
        {
            CurrentMana += 1;
        }
    
        if (InputHandler.KeyDown(Keys.F4))
        {
            CurrentMana -= 1;
        }
    
        if (InputHandler.KeyDown(Keys.F5))
        {
            CurrentExp += 1;
        }
    
        if (InputHandler.KeyDown(Keys.F6))
        {
            CurrentExp -= 1;
        }
    
        CurrentHealth = (int)MathHelper.Clamp(CurrentHealth, 0, 100);
        CurrentMana = (int)MathHelper.Clamp(CurrentMana, 0, 45);
        CurrentExp = (int)MathHelper.Clamp(CurrentExp, 0, 500);
    }
    
    public void Draw(GameTime gameTime)
    {
    
        GameRef.SpriteBatch.Draw(
            HealthBarNegative,
            new Rectangle((int)player.Camera.Position.X + 150, (int)player.Camera.Position.Y + 630, 150, 15),
            Color.White);
    
        GameRef.SpriteBatch.Draw(
            HealthBarPositive,
            new Rectangle((int)player.Camera.Position.X + 150, (int)player.Camera.Position.Y + 630, 150 * (int)CurrentHealth / 100, 15),
            Color.White);
    
        GameRef.SpriteBatch.Draw(
            HealthBar,
            new Rectangle((int)player.Camera.Position.X + 150, (int)player.Camera.Position.Y + 630, 150, 15),
            Color.White);
    
        GameRef.SpriteBatch.Draw(
            ManaBarNegative,
            new Rectangle((int)player.Camera.Position.X + 150, (int)player.Camera.Position.Y + 650, 150, 15),
            Color.White);
    
        GameRef.SpriteBatch.Draw(
            ManaBarPositive,
            new Rectangle((int)player.Camera.Position.X + 150, (int)player.Camera.Position.Y + 650, 150 * (int)CurrentMana / 45, 15),
            Color.White);
    
        GameRef.SpriteBatch.Draw(
            ManaBar,
            new Rectangle((int)player.Camera.Position.X + 150, (int)player.Camera.Position.Y + 650, 150, 15),
            Color.White);
    
        GameRef.SpriteBatch.Draw(
            ExpBarPositive,
            new Rectangle((int)player.Camera.Position.X + 10, (int)player.Camera.Position.Y + 680, 1260 * (int)CurrentExp / 500, 15),
            Color.White);
    
        GameRef.SpriteBatch.Draw(
            ExpBar,
            new Rectangle((int)player.Camera.Position.X + 10, (int)player.Camera.Position.Y + 680, 1260, 15),
            Color.White);
    }
    }
    }
    


When you are drawing your Health bars, is it inside a Spritebatch.Begin( ... ) where you specify your camera matrix?

If you draw it in its own Spritebatch.Begin, without the camera, the position of the health bars will stay relative to the screen.


When you extend you class with DrawableGameComponent, you can use the property DrawOrder to set it topmost. The class will be a bit different, you have to overload Update, draw and LoadContent, but it will be all the same.

This is only usefull if you use a HUD with SpriteBatch.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号