I am currently trying to create a HUD. I ended up going with 2 classes to create the HUD. It will be easier to show you than explain:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CGPLibrary.Sprites;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
namespace CGPLibrary
{
public class HUDElement
{
protected Sprite element;
protected Texture2D texture;
protected int width, height;
protected Vector2 m_position;
protected int offsetX, offsetY;
protected GameWindow Window;
protected Color color;
public enum Position
{
TOP_LEFT, TOP_RIGHT, TOP_MIDDLE, MIDDLE_LEFT, MIDDLE_RIGHT, MIDDLE, BOTTOM_LEFT, BOTTOM_RIGHT, BOTTOM_MIDDLE
}
public enum ELEM_TYPE { Health, Armour, H_ABackground, Life }ELEM_TYPE elementType;
public HUDElement(Sprite sprite, GameWindow window)
{
color = sprite.sprColor;
Window = window;
offsetX = 0;
offsetY = 0;
element = sprite;
texture = sprite.spTexture;
width = sprite.Width;
height = sprite.Height;
}
public Sprite spr { get { return element; } }
public int Width { get { return width; } }
public int Height { get { return height; } }
public Color elemColor { get { return color; } set { color = value; } }
public Texture2D elemText { get { return texture; } }
public ELEM_TYPE type { get { return elementType; } set { elementType = value; } }
public Vector2 position { get { return m_position; } }
public int xOffset { get { return offsetX; } set { offsetX = value; } }
public int yOffset { get { return offsetY; } set { offsetY = value; } }
public void setPosition(Position pos)
{
switch (pos)
{
case Position.TOP_LEFT:
m_position = new Vector2(offsetX, offsetY);
break;
case Position.TOP_MIDDLE:
m_position = new Vector2((Window.ClientBounds.Width / 2 - texture.Width / 2) + offsetX, offsetY);
break;
case Position.TOP_RIGHT:
m_position = new Vector2((Window.ClientBounds.Width - texture.Width) + offsetX, offsetY);
break;
case Position.MIDDLE_LEFT:
m_position = new Vector2(offsetX, (Window.ClientBounds.Height / 2 - texture.Height / 2) + offsetY);
break;
case Position.MIDDLE:
m_position = new Vector2((Window.ClientBounds.Width / 2 - texture.Width / 2) + offsetX, (Window.ClientBounds.Height / 2 - texture.Height / 2) + offsetY);
break;
case Position.MIDDLE_RIGHT:
m_position = new Vector2((Window.ClientBounds.Width - texture.Width) + offsetX, (Window.ClientBounds.Height / 2 - texture.Height / 2) + offsetY);
break;
case Position.BOTTOM_LEFT:
m_position = new Vector2(offsetX, (Window.ClientBounds.Height - texture.Height) + offsetY);
break;
case Position.BOTTOM_MIDDLE:
m_position = new Vector2((Window.ClientBounds.Width / 2 - texture.Width / 2) + offsetX, (Window.ClientBounds.Height - texture.Height) + offsetY);
break;
case Position.BOTTOM_RIGHT:
m_position = new Vector2((Window.ClientBounds.Width - texture.Width) + offsetX, (Window.ClientBounds.Height - texture.Height) + offsetY);
break;
}
}
}
}
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using HeadStacker;
using CGPLibrary.Sprites;
using Microsoft.Xna.Framework.Graphics;
using CGPLibrary.Entities;
namespace CGPLibrary
{
/// <summary>
/// This is a game component that implements IUpdateable.
/// </summary>
public class HUD : DrawableGameComponent
{
protected Dictionary<HUDElement.ELEM_TYPE, HUDElement> hudElements;
HumanPlayer player;
protected SpriteBatch batch;
public Main game;
public HUD(Main game)
: base(game)
{
hudElements = new Dictionary<HUDElement.ELEM_TYPE, HUDElement>();
this.game = game;
player =game.human;
this.batch = game.spriteBatch;
}
public void addHudElement(HUDElement elem)
{
hudElements.Add(elem.type, elem);
}
public override void Draw(GameTime gameTime)
{
batch.Begin();
batch.Draw(hudElements[HUDElement.ELEM_TYPE.Life].elemText, hudElements[HUDElement.ELEM_TYPE.Life].position, hudElements[HUDElement.ELEM_TYPE.Life].spr.sourceRect,
hudElements[HUDElement.ELEM_TYPE.Life].elemColor, 0f, Vector2.Zero, hudElements[HUDElement.ELEM_TYPE.Life].spr.Scale,SpriteEffects.None,0f);
batch.Draw(hudElements[HUDElement.ELEM_TYPE.H_ABackground].elemText, hudElements[HUDElement.ELEM_TYPE.H_ABackground].position,null,
hudElements[HUDElement.ELEM_TYPE.H_ABackground].elemColor,0f,Vector2.Zero,hudElements[HUDElement.ELEM_TYPE.H_ABackground].spr.Scale,SpriteEffects.None,0f);
batch.Draw(hudElements[HUDElement.ELEM_TYPE.Health].elemText, hudElements[HUDElement.ELEM_TYPE.Health].position,
new Rectangle(0,0,(int)(hudElements[HUDElement.ELEM_TYPE.Health].Width * ((double)player.health/100)),hudElements[HUDElement.ELEM_TYPE.Health].Height)
,hudElements[HUDElement.ELEM_TYPE.Health].elemColor,0f,Vector2.Zero,hudElements[HUDElement.ELEM_TYPE.Health].spr.Scale,SpriteEffects.None,0f);
batch.Draw(hudElements[HUDElement.ELEM_TYPE.Armour].elemText, hudElements[HUDElement.ELEM_TYPE.Armour].position,
new Rectangle(0, 0, (int)(hudElements[HUDElement.ELEM_TYPE.Armour].Width * ((double)player.armour / 100)), hudElements[HUDElement.ELEM_TYPE.Armour].Height)
, hudElements[HUDElement.ELEM_TYPE.Armour].elemColor,0f, Vector2.Zero, hudElements[HUDElement.ELEM_TYPE.Armour].spr.Scale, SpriteEffects.None, 0f);
batch.End();
base.Draw(gameTime);
}
public override void Update(GameTime gameTime)
{
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
hudElements[HUDElement.ELEM_TYPE.Life].spr.animate(elapsed);
// hudElements[HUDElement.ELEM_TYPE.Health].elemText = player.currentWeapon.texture;
base.Update(gameTime);
}
}
}
You can see in the draw method, I find each element inside the Dictionary and draw it. My question is, will this cause any kind of overhead? I think I read that the Dictionary is normally O( c )but that was using the .containsKey() method. Will using the [] notation cause any slowdowns(or increased performance)?