/* * PopupSprite.cs * Authors: * Copyright (c) 2007-2008 Cornell University This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ using System; using System.Collections.Generic; using System.Text; using tAC_Engine.Graphics.Entities; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework; namespace tAC_Engine.Graphics.Entities { /// /// Defines a pop up sprite which can shrink or grow upon creation or death /// public class PopupSprite : EntitySprite { #region Fields // The current time private float mTime; // The time at which the pop up sprite started private float mStartTime; // The size the pop up sprite will end at private Point mFinalSize = Point.Zero; // The maximum size the sprite will grow to if a range isn't specified private Point mDefaultSize = Point.Zero; // The size the pop up sprite will start at private Point mStartSize = Point.Zero; // Whether or not the pop up sprite is dying private bool die = false; // Whether or not the pop up sprite is growing private bool mGetLarger = false; // Whether or not the pop up sprite is shrinking private bool mGetSmaller = false; // The center position of the pop up sprite private Vector2 mCenter = Vector2.Zero; #endregion #region Properties /// /// Gets/Sets the position of the center of the pop up sprite /// public virtual Vector2 Center { get { return mCenter; } set { mCenter = value; Recenter(mCenter); } } /// /// Gets whether or not the pop up sprite is getting larger /// public bool GettingLarger { get { return mGetLarger; } } /// /// Gets whether or not the pop up sprite is getting smaller /// public bool GettingSmaller { get { return mGetSmaller; } } #endregion #region Creation /// /// Constructor for pop up sprite /// /// Reference to the current content manager /// Path name to the texture to be used /// Position of the center of the pop up sprite /// Length of time the pop up sprite will last for public PopupSprite(ContentManager pContent, string pTextureName, Vector2 pCenter, float pTime) :base(pContent, pTextureName, Vector2.Zero) { this.Visible = false; mTime = pTime; this.Center = pCenter; this.Height = 0; this.Width = 0; } /// /// Constructor for pop up sprite with a specified default size /// /// Reference to the current content manager /// Path name to the texture to be used /// Position of the center of the pop up sprite /// Length of time the pop up sprite will last for /// Default size of the pop up sprite public PopupSprite(ContentManager pContent, string pTextureName, Vector2 pCenter, float pTime, Point pDefaultSize) : this(pContent, pTextureName, pCenter, pTime) { mDefaultSize = pDefaultSize; } #endregion #region Initialization /// /// Initializes all need values upon creation /// public override void Initialize() { base.Initialize(); Height = 0; Width = 0; } #endregion #region Update /// /// Updates the pop up sprite /// /// Time that has passed in game public override void Update(GameTime gameTime) { if ((Height < mFinalSize.Y && Width < mFinalSize.X && mGetLarger) || (Height > mFinalSize.Y && Width > mFinalSize.X && mGetSmaller)) { float timeRatio = (float)(gameTime.TotalGameTime.TotalMilliseconds - mStartTime) / (float)mTime; Width = (int)MathHelper.Lerp(mStartSize.X, mFinalSize.X, timeRatio); Height = (int)MathHelper.Lerp(mStartSize.Y, mFinalSize.Y, timeRatio); Recenter(Center); } if (Height >= mFinalSize.Y && Width >= mFinalSize.X && mGetLarger) { mGetLarger = false; } else if (Height <= mFinalSize.Y && Width <= mFinalSize.X && mGetSmaller) { mGetSmaller = false; } if (Width <= 0 || Height <= 0) { this.Visible = false; } base.Update(gameTime); if (!Visible && die) this.Dispose(); } #endregion #region State Modifiers /// /// Sprite begins to pop up to the default size /// /// Current time in game public void PopUp(GameTime pCurrentTime) { PopUp(pCurrentTime, mDefaultSize); } /// /// Sprite begins to pop up to the given size /// /// Current time in game /// Size to grow to public void PopUp(GameTime pCurrentTime, Point pSize) { this.Visible = true; mStartTime = (float)pCurrentTime.TotalGameTime.TotalMilliseconds; mFinalSize = pSize; mStartSize = Point.Zero; mGetLarger = true; } /// /// Starts to shrink and until the pop up sprite is gone /// /// Current time in game public void GoAway(GameTime pCurrentTime) { mStartTime = (float)pCurrentTime.TotalGameTime.TotalMilliseconds; mStartSize = mFinalSize; mFinalSize = Point.Zero; mGetSmaller = true; } /// /// Starts to shrink and until the pop up sprite is gone then gets disposed of /// /// Current time in game public void GoAwayAndDie(GameTime pCurrentTime) { GoAway(pCurrentTime); die = true; } #endregion #region Parameter Modifiers /// /// Recenters the pop up sprite to the given position /// /// Postion to move the center of the pop up sprite to protected void Recenter(Vector2 pCenter) { this.Position = new Vector2(pCenter.X - this.Width / 2, pCenter.Y - this.Height / 2); } #endregion } }