From 6bb8e07fd8aa7df137d4afdee85d1eec0e415f7a Mon Sep 17 00:00:00 2001 From: Ivan Hernanez Date: Thu, 31 May 2012 22:10:20 -0500 Subject: [PATCH] Added HUD --- CMakeLists.txt | 2 +- HUD.cpp | 1 + HUD.h | 603 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 605 insertions(+), 1 deletion(-) create mode 100644 HUD.cpp create mode 100644 HUD.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 5acc516..9b03e2f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,6 @@ find_package(SDL_ttf REQUIRED) find_library(GL GL) -add_executable(game Animation.cpp fns.cpp Game.cpp Level.cpp main.cpp WorldObject.cpp Sprite.cpp Text.cpp Actor.cpp Background.cpp Collision.cpp LoadResources.cpp) +add_executable(game Animation.cpp fns.cpp Game.cpp Level.cpp main.cpp WorldObject.cpp Sprite.cpp Text.cpp Actor.cpp Background.cpp Collision.cpp LoadResources.cpp HUD.cpp) target_link_libraries(game yaml-cpp ${SDL_LIBRARY} ${SDLIMAGE_LIBRARY} ${SDLTTF_LIBRARY} ${GL}) diff --git a/HUD.cpp b/HUD.cpp new file mode 100644 index 0000000..88d43c1 --- /dev/null +++ b/HUD.cpp @@ -0,0 +1 @@ +#include "HUD.h" diff --git a/HUD.h b/HUD.h new file mode 100644 index 0000000..8f92339 --- /dev/null +++ b/HUD.h @@ -0,0 +1,603 @@ +#ifndef __HUD_H__ +#define __HUD_H__ +class HUD +{ +public: + HUD(){} +// +// TextScroller scroller; +// TextFader fader; +// vector bossHealthBars = new vector(); +// vector playerHUDS = new vector(); +// static Vector2 barSize = new Vector2(100, 20); +// static Vector2 barSpacing = new Vector2(10, 2); +// ItemArea items; +// +// +// +// public void LoadCommon(IHUDTheme Theme = null) +// { +// if (Theme != null) +// { +// theme = Theme; +// } +// +// Viewport v = This.Game.GraphicsDevice.Viewport; +// scroller = new TextScroller("scroller", theme); +// scroller.Pos = new Vector2(FrostbyteLevel.BORDER_WIDTH / 2, +// v.Height - scroller.GetAnimation().Height); +// scroller.Static = true; +// +// fader = new TextFader("fader", theme); +// fader.Pos = new Vector2(v.Width - 10, v.Height - 10 - 30); +// fader.Anchor = Orientations.Up_Right; +// fader.Static = true; +// +// +// items = new ItemArea("Items", theme); +// items.Pos = new Vector2(890, 10); +// items.Static = true; +// +// } +// +// public void AddPlayer(Player p) +// { +// int xoffset = 80 + playerHUDS.Count * (int)(barSize.X + barSpacing.X); +// playerHUDS.Add(new PlayerHUD(theme, p, xoffset, 10)); +// } +// +// public void AddBossHealthBar(Boss b) +// { +// ProgressBar lastPlayerHealth = playerHUDS.Last().healthBar; +// Vector2 size = new Vector2(This.Game.GraphicsDevice.Viewport.Width * 0.8f, barSize.Y * 1.5f); +// ProgressBar healthBar = new ProgressBar(b.Name + "_health", b.MaxHealth, +// Color.DarkRed, Color.Firebrick, Color.Black, size); +// healthBar.Pos = new Vector2( +// (This.Game.GraphicsDevice.Viewport.Width - size.X) / 2, +// This.Game.GraphicsDevice.Viewport.Height - size.Y * 2); +// healthBar.Static = true; +// healthBar.Value = b.MaxHealth; +// +// b.HealthChanged += delegate(object obj, int value) +// { +// healthBar.Value = value; +// }; +// bossHealthBars.Add(healthBar); +// } +// +// public void RemoveBossHealthBar(Boss b) +// { +// foreach (ProgressBar p in bossHealthBars.FindAll(p => p.Name == b.Name + "_health")) +// { +// This.Game.CurrentLevel.RemoveSprite(p); +// bossHealthBars.Remove(p); +// } +// } +// +// public void ScrollText(string s) +// { +// if (scroller != null && s != null) +// { +// scroller.ScrollText(s); +// } +// } +// +// public void FadeText(string s) +// { +// if (fader != null && s != null) +// { +// fader.FadeText(s); +// } +// } +// +// +// class PlayerHUD +// { +// public PlayerHUD(IHUDTheme theme, Player p, int xOffset, int yOffset) +// { +// +// Text name = new Text("player_name_" + p.Name, "Text", p.Name); +// name.DisplayColor = theme.TextColor; +// name.Pos = new Vector2(xOffset, yOffset); +// name.Static = true; +// +// +// +// healthBar = new ProgressBar("Health_" + p.Name, p.MaxHealth, +// Color.DarkRed, Color.Firebrick, Color.Black, barSize); +// healthBar.Pos = new Vector2(xOffset, name.Pos.Y + name.GetAnimation().Height); +// healthBar.Static = true; +// healthBar.Value = p.MaxHealth; +// +// p.HealthChanged += delegate(object obj, int value) +// { +// healthBar.Value = value; +// if (value == 0) +// { +// name.DisplayColor = Color.Tomato; +// } +// else +// { +// name.DisplayColor = theme.TextColor; +// } +// }; +// +// +// +// manaBar = new ProgressBar("Mana_" + p.Name, p.MaxMana, +// Color.MidnightBlue, Color.Blue, Color.Black, barSize); +// manaBar.Pos = new Vector2(xOffset, +// healthBar.Pos.Y + barSize.Y + barSpacing.Y); +// manaBar.Static = true; +// manaBar.Value = p.MaxMana; +// +// p.ManaChanged += delegate(object obj, int value) +// { +// manaBar.Value = value; +// }; +// +// +// +// spellQueue = new SpellQueue("Queue", theme, p); +// spellQueue.Pos = new Vector2(xOffset, +// healthBar.Pos.Y + barSize.Y + 2 * barSpacing.Y + barSize.Y); +// spellQueue.Static = true; +// +// +// +// +// } +// +// ~PlayerHUD() +// { +// This.Game.CurrentLevel.RemoveSprite(healthBar); +// This.Game.CurrentLevel.RemoveSprite(manaBar); +// } +// +// +// public ProgressBar healthBar; +// public ProgressBar manaBar; +// public SpellQueue spellQueue; +// +// +// public int Health +// { +// get +// { +// return healthBar.Value; +// } +// set +// { +// healthBar.Value = value; +// } +// } +// public int Mana +// { +// get +// { +// return manaBar.Value; +// } +// set +// { +// manaBar.Value = value; +// } +// } +// +// } +// +// class ItemArea : Sprite +// { +// public ItemArea(string name, IHUDTheme theme) +// : base(name, new Actor(new DummyAnimation(name, +// (int)HUD.barSize.X, (int)HUD.barSize.Y))) +// { +// ZOrder = 100; +// this.theme = theme; +// background = new Texture2D(This.Game.GraphicsDevice, 1, 1); +// background.SetData(new Color[] { theme.TransparentBackgroundColor }); +// } +// +// IHUDTheme theme; +// Texture2D background; +// +// static Vector2 itemSpacing = new Vector2(3, 2); +// static int itemsPerRow = 5; +// +// public override void Draw(GameTime gameTime) +// { +// base.Draw(gameTime); +// +// This.Game.spriteBatch.Draw(background, new Rectangle( +// (int)Pos.X, +// (int)Pos.Y, +// (int)GetAnimation().Width, +// (int)GetAnimation().Height), Color.White); +// +// if (Player.ItemBag.Count > 0) +// { +// for (int x = 0; x < Player.ItemBag.Count; x++) +// { +// Sprite icon = Player.ItemBag[x].Icon; +// icon.Pos.X = Pos.X + itemSpacing.X + 1 + // Initial alignment of 1px +// (x % itemsPerRow) * (icon.GetAnimation().Width + itemSpacing.X); +// icon.Pos.Y = Pos.Y + itemSpacing.Y + +// (x / itemsPerRow) * (icon.GetAnimation().Height + itemSpacing.Y); +// icon.Visible = true; +// icon.Draw(gameTime); +// } +// } +// } +// } +// +// class SpellQueue : Sprite +// { +// public SpellQueue(string name, IHUDTheme theme, Player player) +// : base(name, new Actor(new DummyAnimation(name, +// (int)HUD.barSize.X, (int)HUD.barSize.Y))) +// { +// ZOrder = 100; +// this.theme = theme; +// background = new Texture2D(This.Game.GraphicsDevice, 1, 1); +// background.SetData(new Color[] { theme.TransparentBackgroundColor }); +// this.player = player; +// } +// +// IHUDTheme theme; +// Texture2D background; +// Player player; +// +// static Vector2 itemSpacing = new Vector2(12, 2); +// static int itemsPerRow = 3; +// +// public override void Draw(GameTime gameTime) +// { +// base.Draw(gameTime); +// +// This.Game.spriteBatch.Draw(background, new Rectangle( +// (int)Pos.X, +// (int)Pos.Y, +// (int)GetAnimation().Width, +// (int)GetAnimation().Height), Color.White); +// +// int limit = 0; +// if (Characters.Mage.UnlockedSpells.HasFlag(Spells.EarthThree)) +// limit = 3; +// else if (Characters.Mage.UnlockedSpells.HasFlag(Spells.EarthTwo)) +// limit = 2; +// else +// limit = 1; +// +// for (int x = 0; x < (player as Frostbyte.Characters.Mage).attackCounter.Count && x < limit; x++) +// { +// +// if ((player as Frostbyte.Characters.Mage).attackCounter[x] == Element.Earth && Characters.Mage.UnlockedSpells.HasFlag(Spells.EarthOne)) +// { +// Sprite icon = new ItemIcon("earth", new Actor(This.Game.CurrentLevel.GetAnimation("earthIcon.anim"))); +// icon.Pos.X = Pos.X + itemSpacing.X + 1 + // Initial alignment of 1px +// (x % itemsPerRow) * (icon.GetAnimation().Width + itemSpacing.X); +// icon.Pos.Y = Pos.Y + itemSpacing.Y + +// (x / itemsPerRow) * (icon.GetAnimation().Height + itemSpacing.Y); +// icon.Visible = true; +// icon.Draw(gameTime); +// This.Game.CurrentLevel.RemoveSprite(icon); +// } +// +// else if ((player as Frostbyte.Characters.Mage).attackCounter[x] == Element.Lightning && Characters.Mage.UnlockedSpells.HasFlag(Spells.LightningOne)) +// { +// Sprite icon = new ItemIcon("lightning", new Actor(This.Game.CurrentLevel.GetAnimation("lightningIcon.anim"))); +// icon.Pos.X = Pos.X + itemSpacing.X + 1 + // Initial alignment of 1px +// (x % itemsPerRow) * (icon.GetAnimation().Width + itemSpacing.X); +// icon.Pos.Y = Pos.Y + itemSpacing.Y + +// (x / itemsPerRow) * (icon.GetAnimation().Height + itemSpacing.Y); +// icon.Visible = true; +// icon.Draw(gameTime); +// This.Game.CurrentLevel.RemoveSprite(icon); +// } +// +// else if ((player as Frostbyte.Characters.Mage).attackCounter[x] == Element.Water && Characters.Mage.UnlockedSpells.HasFlag(Spells.WaterOne)) +// { +// Sprite icon = new ItemIcon("water", new Actor(This.Game.CurrentLevel.GetAnimation("waterIcon.anim"))); +// icon.Pos.X = Pos.X + itemSpacing.X + 1 + // Initial alignment of 1px +// (x % itemsPerRow) * (icon.GetAnimation().Width + itemSpacing.X); +// icon.Pos.Y = Pos.Y + itemSpacing.Y + +// (x / itemsPerRow) * (icon.GetAnimation().Height + itemSpacing.Y); +// icon.Visible = true; +// icon.Draw(gameTime); +// This.Game.CurrentLevel.RemoveSprite(icon); +// } +// +// else if ((player as Frostbyte.Characters.Mage).attackCounter[x] == Element.Fire && Characters.Mage.UnlockedSpells.HasFlag(Spells.FireOne)) +// { +// Sprite icon = new ItemIcon("fire", new Actor(This.Game.CurrentLevel.GetAnimation("fireIcon.anim"))); +// icon.Pos.X = Pos.X + itemSpacing.X + 1 + // Initial alignment of 1px +// (x % itemsPerRow) * (icon.GetAnimation().Width + itemSpacing.X); +// icon.Pos.Y = Pos.Y + itemSpacing.Y + +// (x / itemsPerRow) * (icon.GetAnimation().Height + itemSpacing.Y); +// icon.Visible = true; +// icon.Draw(gameTime); +// This.Game.CurrentLevel.RemoveSprite(icon); +// } +// +// //Sprite icon = Player.ItemBag[x].Icon; +// //icon.Pos.X = Pos.X + itemSpacing.X + 1 + // Initial alignment of 1px +// // (x % itemsPerRow) * (icon.GetAnimation().Width + itemSpacing.X); +// //icon.Pos.Y = Pos.Y + itemSpacing.Y + +// // (x / itemsPerRow) * (icon.GetAnimation().Height + itemSpacing.Y); +// //icon.Visible = true; +// //icon.Draw(gameTime); +// +// } +// } +// } +//} +// +//public class TextFader : Sprite +//{ +// public TextFader(string name, IHUDTheme theme) +// : this(name, theme, 0, 0) +// { +// } +// +// public TextFader(string name, int width, int height) +// : this(name, new GenericTheme(), width, height) +// { +// } +// +// public TextFader(string name, IHUDTheme theme, int width, int height) +// : base(name, new Actor(new DummyAnimation(name, width, height))) +// { +// ZOrder = 100; +// UpdateBehavior = update; +// this.theme = theme; +// Center = new Vector2(0, 0); +// Anchor = Orientations.Left; +// +// pendingText = new Queue(); +// mStates = States().GetEnumerator(); +// +// toDisplay = new Text("fader_text", theme.TextFont, ""); +// toDisplay.Visible = false; +// toDisplay.DisplayColor = theme.TextColor; +// toDisplay.Static = true; +// toDisplay.ZOrder = 101; +// } +// +// int MaxCharacters = 100; +// IHUDTheme theme; +// IEnumerator mStates; +// Queue pendingText; +// Text toDisplay; +// +// float mAlpha; +// float mFadeIncrement = 0.01f; +// TimeSpan mFadeDelay = new TimeSpan(0, 0, 0, 1); +// +// public Orientations Anchor { get; set; } +// +// public void FadeText(string text) +// { +// text = String.Join("", text.Take(MaxCharacters)).Trim(); +// if (!String.IsNullOrEmpty(text)) +// { +// pendingText.Enqueue(text); +// } +// } +// +// public void update() +// { +// mStates.MoveNext(); +// } +// +// IEnumerable States() +// { +// while (true) +// { +// if (pendingText.Count > 0) +// { +// toDisplay.Content = pendingText.Dequeue(); +// Vector2 displayPos = new Vector2(); +// +// +// switch (Anchor) +// { +// case Orientations.Right: +// displayPos.X = Pos.X - toDisplay.GetAnimation().Width; +// break; +// case Orientations.Up_Right: +// goto case Orientations.Right; +// case Orientations.Down_Right: +// goto case Orientations.Right; +// case Orientations.Left: +// goto default; +// case Orientations.Up_Left: +// goto case Orientations.Left; +// case Orientations.Down_Left: +// goto case Orientations.Left; +// default: +// displayPos.X = Pos.X; +// break; +// } +// +// +// +// switch (Anchor) +// { +// case Orientations.Down: +// displayPos.Y = Pos.Y - toDisplay.GetAnimation().Height; +// break; +// case Orientations.Down_Left: +// goto case Orientations.Down; +// case Orientations.Down_Right: +// goto case Orientations.Down; +// case Orientations.Up: +// goto default; +// case Orientations.Up_Left: +// goto case Orientations.Up; +// case Orientations.Up_Right: +// goto case Orientations.Up; +// default: +// displayPos.Y = Pos.Y; +// break; +// } +// +// toDisplay.Pos = displayPos; +// toDisplay.Visible = true; +// +// mAlpha = 0; +// toDisplay.DisplayColor = (theme.TextColor * mAlpha); +// +// foreach (string type in new string[] { "in", "out" }) +// { +// // Leave faded in (or out) for mFadeDelay time +// TimeSpan endTime = This.gameTime.TotalGameTime + mFadeDelay; +// while (This.gameTime.TotalGameTime < endTime) +// { +// yield return null; +// } +// +// while (mAlpha >= 0 && mAlpha <= 1) +// { +// toDisplay.DisplayColor = (theme.TextColor * mAlpha); +// mAlpha += mFadeIncrement; +// yield return null; +// } +// +// mAlpha -= mFadeIncrement; // Put it back within alpha range +// mFadeIncrement *= -1; // Reverse fade direction +// } +// toDisplay.Visible = false; +// } +// +// yield return null; +// } +// } +//} +// +//public class TextScroller : Sprite +//{ +// public TextScroller(string name, IHUDTheme theme) +// : this(name, theme, This.Game.GraphicsDevice.Viewport.Width - FrostbyteLevel.BORDER_WIDTH, +// This.Game.GraphicsDevice.Viewport.Height - (int)(2.5f * FrostbyteLevel.BORDER_HEIGHT)) +// { +// } +// +// public TextScroller(string name, int width, int height) +// : this(name, new GenericTheme(), width, height) +// { +// } +// +// public TextScroller(string name, IHUDTheme theme, int width, int height) +// : base(name, new Actor(new DummyAnimation(name, width, height))) +// { +// ZOrder = 100; +// UpdateBehavior = update; +// this.theme = theme; +// background = new Texture2D(This.Game.GraphicsDevice, 1, 1); +// background.SetData(new Color[] { theme.TransparentBackgroundColor }); +// Center = new Vector2(0, 0); +// } +// +// public int MaxCharactersPerLine = 62; +// public int TextSpacing = 2; +// public bool SplitOnWhitespace = true; +// IHUDTheme theme; +// vector buffer = new vector(); +// vector onScreen = new vector(); +// Texture2D background; +// +// int tickCount = 0; +// public int TicksPerScroll = 3; +// +// +// public void ScrollText(string s) +// { +// buffer.AddRange(s.Replace("\r\n", "\n")); +// buffer.AddRange("\n\n"); +// } +// +// +// +// public bool Scrolling { get { return buffer.Count > 0 || onScreen.Count > 0; } } +// +// +// +// public void update() +// { +// tickCount = (tickCount + 1) % TicksPerScroll; +// if (onScreen.Count > 0 && tickCount == 0) +// { +// foreach (Text t in onScreen) +// { +// t.Pos.Y -= 1; +// } +// +// Sprite fst = onScreen.First(); +// if (fst.Pos.Y < Pos.Y) +// { +// This.Game.CurrentLevel.RemoveSprite(fst); +// onScreen.RemoveAt(0); +// } +// } +// if (buffer.Count != 0) +// { +// // We have room to scroll another line of text +// if (onScreen.Count == 0 || +// onScreen.Last().Pos.Y + onScreen.Last().GetAnimation().Height + TextSpacing < +// Pos.Y + GetAnimation().Height - onScreen.Last().GetAnimation().Height) +// { +// int width = GetAnimation().Width; +// string toDisplay; +// +// if (String.Join("", buffer.Take(2)) == "\n\n") +// { +// toDisplay = " "; +// buffer.RemoveRange(0, 2); +// } +// else +// { +// buffer = buffer.SkipWhile(x => char.IsWhiteSpace(x)).ToList(); +// IEnumerable pendingDisplay = buffer.TakeWhile((ch, ix) => +// theme.TextFont.MeasureString( +// String.Join("", buffer.Take(ix + 1)).Trim()).X < width && +// ch != '\n'); +// +// if (SplitOnWhitespace && +// buffer.Count > pendingDisplay.Count() && +// buffer[pendingDisplay.Count()] != '\n') +// { +// // Find first instance of whitespace at end +// pendingDisplay = pendingDisplay.Reverse().SkipWhile(x => !char.IsWhiteSpace(x)).Reverse(); +// } +// +// toDisplay = String.Join("", pendingDisplay).Trim(); +// buffer.RemoveRange(0, pendingDisplay.Count()); +// } +// +// Text line = new Text("text", theme.TextFont, toDisplay.ToString()); +// line.DisplayColor = theme.TextColor; +// line.Pos = new Vector2(Pos.X, Pos.Y + GetAnimation().Height - line.GetAnimation().Height); +// line.Static = true; +// line.ZOrder = 101; +// onScreen.Add(line); +// } +// } +// } +// +// +// +// public override void Draw(GameTime gameTime) +// { +// base.Draw(gameTime); +// if (onScreen.Count > 0) +// { +// This.Game.spriteBatch.Draw(background, new Rectangle( +// (int)Pos.X, +// (int)Pos.Y, +// (int)GetAnimation().Width, +// (int)GetAnimation().Height), Color.White); +// } +// } +// +//} +}; +#endif \ No newline at end of file -- 2.11.0