/* * EntitySprite.cs * Authors: Mike Dapiran * 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 Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Util; namespace tAC_Engine.Graphics.Entities { /// /// This is the class which will be used to draw sprites to the screen. /// public class EntitySprite : DrawableGameScreenComponent { #region Fields /// /// Reference to the current content manager /// protected ContentManager Content; /// /// The destination rectangle of the sprite on the screen. /// Defaults to Rectangle.Empty and is set to width and height /// of the Texture upon load. /// protected Rectangle m_Destination = Rectangle.Empty; /// /// The center point of the EntitySprite's Texture. /// protected Vector2 m_TextureCenter = Vector2.Zero; /// /// The bounding sphere radius of the EntitySprite's Texture /// protected float m_Radius; /// /// The SpriteBatch object used to draw the sprite. Defaults to null. Will be ignored if the SpriteBatch is passed as a draw /// parameter. /// protected SpriteBatch m_SpriteBatch = null; /// /// The Texture that the sprite will use. /// protected Texture2D m_Texture; /// /// The name used to load the Texture, relative to the root directory. /// protected String m_TextureName; /// /// The Color to be applied to the sprite. Defaults to Color.White. /// protected Color m_Color = Color.White; /// /// The origin of rotation of the sprite. Default is Upper left (Vector.Zero). /// protected Vector2 m_RotationOrigin = Vector2.Zero; /// /// The amount of rotation to apply to the sprite. Defaults to 0 /// protected float m_Rotation = 0; /// /// The SpriteEffect for the EntitySprite. /// protected SpriteEffects m_SpriteEffect = SpriteEffects.None; /// /// The Layer Depth of the sprite. This affects what is drawn in front of it. 0 = front, 1 = back. /// Defaults to 0 /// protected float m_LayerDepth = 0; /// /// The blend mode to be used on the sprite. Can be either None, Additive, or AlphaBlend. /// Defaults to SpriteBlendMode.None /// protected SpriteBlendMode m_BlendMode = SpriteBlendMode.None; #endregion #region Properties /// /// Gets/Sets the position of the sprite on the screen. Defaults to Vecter2.Zero /// public Vector2 Position { get { return new Vector2(m_Destination.X, m_Destination.Y); } set { m_Destination.X = (int)value.X; m_Destination.Y = (int)value.Y; } } /// /// Gets/Sets the X position of the Entity. /// public int X { get { return m_Destination.X; } set { m_Destination.X = value; } } /// /// Gets/Sets the Y position of the Entity. /// public int Y { get { return m_Destination.Y; } set { m_Destination.Y = value; } } /// /// Gets/Sets the width of the Entity. /// public int Width { get { return m_Destination.Width; } set { m_Destination.Width = value; } } /// /// Gets/Sets the height of the Entity. /// public int Height { get { return m_Destination.Height; } set { m_Destination.Height = value; } } /// /// Gets the destination rectangle of the sprite on the screen. /// Defaults to Rectangle.Empty and is set to width and height /// of the Texture upon load. /// public Rectangle Destination { get { return m_Destination; } set { m_Destination = value; } } /// /// Gets/Sets the center point of the EntitySprite's Texture. /// public Vector2 TextureCenter { get { return m_TextureCenter; } set { m_TextureCenter = value; } } /// /// Gets/Sets the bounding sphere radius of the EntitySprite's Texture /// public float Radius { get { return m_Radius; } set { m_Radius = value; } } /// /// Gets/Sets the SpriteBatch object used to draw the sprite. Defaults to null. Will be ignored /// if the SpriteBatch is passed as a draw parameter /// public virtual SpriteBatch SpriteBatch { get { return m_SpriteBatch; } set { m_SpriteBatch = value; } } /// /// Gets/Sets the Texture that the sprite will use /// public Texture2D Texture { get { return m_Texture; } set { m_Texture = value; } } /// /// Gets/Sets the name used to load the Texture, relative to the root directory /// public String TextureName { get { return m_TextureName; } set { m_TextureName = value; } } /// /// Gets/Sets the Color to be applied to the sprite. Defaults to Color.White /// public Color Color { get { return m_Color; } set { m_Color = value; } } /// /// Gets/Sets the origin of rotation of the sprite. Default is Upper left (Vector.Zero) /// public Vector2 RotationOrigin { get { return m_RotationOrigin; } set { m_RotationOrigin = value; } } /// /// Gets/Sets the amount of rotation to apply to the sprite. Defaults to 0 /// public float Rotation { get { return m_Rotation; } set { m_Rotation = value; } } /// /// Gets/Sets the SpriteEffect for the EntitySprite /// public SpriteEffects SpriteEffect { get { return m_SpriteEffect; } set { m_SpriteEffect = value; } } /// /// Gets/Sets the Layer Depth of the sprite. This affects what is drawn in front of it. 0 = front, 1 = back. /// Defaults to 0 /// public float LayerDepth { get { return m_LayerDepth; } set { m_LayerDepth = value; } } /// /// Gets/Sets the blend mode to be used on the sprite. Can be either None, Additive, or AlphaBlend. /// Defaults to SpriteBlendMode.None /// public SpriteBlendMode BlendMode { get { return m_BlendMode; } set { m_BlendMode = value; } } #endregion #region Creation /// /// Constructor /// /// Reference to the current content manager. /// The texture that the sprite will use. public EntitySprite(ContentManager p_Content, String p_TextureName) { Content = p_Content; m_TextureName = p_TextureName; } /// /// Constructor /// /// Reference to the current content manager. /// The Texture2D that the sprite will use. /// The Position of the sprite on the screen. public EntitySprite(ContentManager p_Content, String p_TextureName, Vector2 p_Position) { Content = p_Content; m_TextureName = p_TextureName; Position = p_Position; } #endregion #region Management /// /// Loads necessary info for content dependent objects /// protected override void LoadContent() { m_Texture = Content.Load(m_TextureName); m_TextureCenter = new Vector2(m_Texture.Width >> 1, m_Texture.Height >> 1); m_Radius = Vector2.Distance(Vector2.Zero, m_TextureCenter); m_Destination.Width = m_Texture.Width; m_Destination.Height = m_Texture.Height; base.LoadContent(); } #endregion #region Render /// /// The draw method that will be called by XNA during the Draw cycle. /// /// The current game time. Passed in by XNA. public override void Draw(GameTime gameTime) { GraphicsDevice.RenderState.DepthBufferEnable = true; GraphicsDevice.RenderState.DepthBufferFunction = CompareFunction.Always; GraphicsDevice.RenderState.FillMode = FillMode.Solid; m_SpriteBatch.Begin(m_BlendMode); m_SpriteBatch.Draw(m_Texture, m_Destination, null, m_Color, m_Rotation, m_RotationOrigin, m_SpriteEffect, m_LayerDepth); m_SpriteBatch.End(); } #endregion } }