All pastes #1521844 Raw Edit

Vic

public text v1 · immutable
#1521844 ·published 2009-08-08 13:19 UTC
rendered paste body
using FlatRedBall;
using FlatRedBall.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using System;

namespace FlatRedBallHelper
{
    /// <summary>
    /// This class creates a backward moving stopwatch in the form of a decreasing circle
    /// </summary>
    class Pie : PositionedObject, IDrawableBatch
    {
        #region Variables
        private BasicEffect _effect;
        private VertexDeclaration _vertexDeclaration;
        private short[] _indices;

        private int _steps; // number of triangles that form the circle
        private float _coolDownTime; // the time the cooldown will last in seconds
        private float _timeElapsed; // the time the cooldown already runs
        private float _radius; // the radius of the shape
        private Color _colorInnerPoint; // the color of the middlepoint of the shape
        private Color _colorOuterPoint; // the color of the outline of the shape

        /// <summary>
        /// defines the granularity of the pie. The bigger the number, the finer the granularity. Min 4, Max 360
        /// </summary>
        public int steps
        {
            get { return _steps; }
            set
            {
                _steps = value;

                if (_steps < 4 > 360)
                    _steps = 360;

                CreateVertices();
            }
        }

        /// <summary>
        /// the time the pie should count in seconds
        /// </summary>
        public float coolDownTime
        {
            get { return _coolDownTime; }
            set { _coolDownTime = value; }
        }

        /// <summary>
        /// the elapsed time of the current cooldown timer
        /// </summary>
        public float timeElapsed
        {
            get { return _timeElapsed; }
        }

        /// <summary>
        /// the size of the shape
        /// </summary>
        public float radius
        {
            get { return _radius; }
            set { _radius = value; CreateVertices(); }
        }

        /// <summary>
        /// the color of the middlepoint of the shape
        /// </summary>
        public Color colorInnerPoint
        {
            get { return _colorInnerPoint; }
            set { _colorInnerPoint = value; CreateVertices(); }
        }

        /// <summary>
        /// the color of the outer points of the shape
        /// </summary>
        public Color colorOuterPoint
        {
            get { return _colorOuterPoint; }
            set { _colorOuterPoint = value; CreateVertices(); }
        }

        private Dictionary<int> _verticesList;
        #endregion

        #region Con-/Destructures
        /// <summary>
        /// standard constructor
        /// </summary>
        public Pie()
            : base()
        {
            SetShapeAppearance(48, 1f, 1f, 0f, 0f, Color.White, Color.White);

            InitializePie();
        }

        /// <summary>
        /// constructor with custom initialization values
        /// </summary>
        /// <param>defines the granularity of the pie. The bigger the number, the finer the granularity. Min 4, Max 360</param>
        /// <param>the time the pie should count in seconds</param>
        /// <param>the size of the shape</param>
        /// <param>the x-position of the shape</param>
        /// <param>the y-position of the shape</param>
        /// <param>the color of the middlepoint of the shape</param>
        /// <param>the color of the outer points of the shape</param>
        public Pie(int steps, float coolDownTime, float radius, float x, float y, Color colorInnerPoint, Color colorOuterPoint)
            : base()
        {
            SetShapeAppearance(steps, coolDownTime, radius, x, y, colorInnerPoint, colorOuterPoint);

            InitializePie();
        }

        private void InitializePie()
        {
            // Create the effect
            _effect = new BasicEffect(FlatRedBallServices.GraphicsDevice, null);
            _effect.VertexColorEnabled = true;

            // Create the vertex declaration
            _vertexDeclaration = new VertexDeclaration(FlatRedBallServices.GraphicsDevice, VertexPositionColor.VertexElements);

            // Create the vertices
            CreateVertices();

            // Create the indices
            _indices = new short[] { 0, 1, 2 };
        }

        #endregion

        #region public methods
        /// <summary>
        /// starts the cooldown-timer
        /// </summary>
        public void StartCoolDown()
        {
            _timeElapsed = 0f;
        }

        /// <summary>
        /// set the position of the pie
        /// </summary>
        /// <param>the x value of the position</param>
        /// <param>the y value of the position</param>
        public void SetShapePosition(float x, float y)
        {
            this.X = x;
            this.Y = y;
        }

        /// <summary>
        /// move the pie by the given vector
        /// </summary>
        /// <param>x-value of the move vector</param>
        /// <param>y-value of the move vector</param>
        public void MoveShapePosition(float deltaX, float deltaY)
        {
            this.X += deltaX;
            this.Y += deltaY;
        }

        /// <summary>
        /// sets the variables to determine the appearance of the pie
        /// </summary>
        /// <param>defines the granularity of the pie. The bigger the number, the finer the granularity. Min 4, Max 360</param>
        /// <param>the time the pie should count in seconds</param>
        /// <param>the size of the shape</param>
        /// <param>the x-position of the shape</param>
        /// <param>the y-position of the shape</param>
        /// <param>the color of the middlepoint of the shape</param>
        /// <param>the color of the outer points of the shape</param>
        public void SetShapeAppearance(int steps, float coolDownTime, float radius, float x, float y, Color colorInnerPoint, Color colorOuterPoint)
        {
            this.steps = steps;
            _coolDownTime = coolDownTime;
            _radius = radius;
            this.X = x;
            this.Y = y;
            _colorInnerPoint = colorInnerPoint;
            _colorOuterPoint = colorOuterPoint;

            CreateVertices();
        }
        #endregion

        #region private methods

        /// <summary>
        /// creating the vertices of the triangles that will be drawn
        /// </summary>
        private void CreateVertices()
      {
         _verticesList = new Dictionary<int>();
         double oneStep = 2 * MathHelper.Pi / _steps;
         Vector3 basePoint = new Vector3(0f, 0f, 0f);
         for (int i = 0; i <_steps> _coolDownTime)
            return;

         // from which pie the circle have to be drawn
         int firstStep = Convert.ToInt32(_steps * _timeElapsed / _coolDownTime);

         // Set graphics states
         FlatRedBallServices.GraphicsDevice.RenderState.FillMode = FillMode.Solid;
         FlatRedBallServices.GraphicsDevice.RenderState.CullMode = CullMode.None;

         // Set the vertex declaration
         FlatRedBallServices.GraphicsDevice.VertexDeclaration = _vertexDeclaration;

         // Have the current camera set our current view/projection variables
         camera.SetDeviceViewAndProjection(_effect, false);

         // Here we get the positioned object's transformation (position / rotation)
         _effect.World = base.TransformationMatrix;

         // Start the effect
         _effect.Begin();
         foreach (EffectPass pass in _effect.CurrentTechnique.Passes)
         {
            // Start each pass
            pass.Begin();

            // Draw the shapes
            for (int i = firstStep; i < _steps; i++)
               FlatRedBallServices.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(
                     PrimitiveType.TriangleList,
                     _verticesList[i], 0, _verticesList[i].Length,
                     _indices, 0, _indices.Length / 3);

            // End each pass
            pass.End();
         }
         // End the effect
         _effect.End();
      }

        public void Update()
        {
        }

        public bool UpdateEveryFrame
        {
            get { return false; }
        }

        #endregion
    }
}