using FlatRedBall; using FlatRedBall.Graphics; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System.Collections.Generic; using System; namespace FlatRedBallHelper { /// /// This class creates a backward moving stopwatch in the form of a decreasing circle /// 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 /// /// defines the granularity of the pie. The bigger the number, the finer the granularity. Min 4, Max 360 /// public int steps { get { return _steps; } set { _steps = value; if (_steps < 4 > 360) _steps = 360; CreateVertices(); } } /// /// the time the pie should count in seconds /// public float coolDownTime { get { return _coolDownTime; } set { _coolDownTime = value; } } /// /// the elapsed time of the current cooldown timer /// public float timeElapsed { get { return _timeElapsed; } } /// /// the size of the shape /// public float radius { get { return _radius; } set { _radius = value; CreateVertices(); } } /// /// the color of the middlepoint of the shape /// public Color colorInnerPoint { get { return _colorInnerPoint; } set { _colorInnerPoint = value; CreateVertices(); } } /// /// the color of the outer points of the shape /// public Color colorOuterPoint { get { return _colorOuterPoint; } set { _colorOuterPoint = value; CreateVertices(); } } private Dictionary _verticesList; #endregion #region Con-/Destructures /// /// standard constructor /// public Pie() : base() { SetShapeAppearance(48, 1f, 1f, 0f, 0f, Color.White, Color.White); InitializePie(); } /// /// constructor with custom initialization values /// /// defines the granularity of the pie. The bigger the number, the finer the granularity. Min 4, Max 360 /// the time the pie should count in seconds /// the size of the shape /// the x-position of the shape /// the y-position of the shape /// the color of the middlepoint of the shape /// the color of the outer points of the shape 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 /// /// starts the cooldown-timer /// public void StartCoolDown() { _timeElapsed = 0f; } /// /// set the position of the pie /// /// the x value of the position /// the y value of the position public void SetShapePosition(float x, float y) { this.X = x; this.Y = y; } /// /// move the pie by the given vector /// /// x-value of the move vector /// y-value of the move vector public void MoveShapePosition(float deltaX, float deltaY) { this.X += deltaX; this.Y += deltaY; } /// /// sets the variables to determine the appearance of the pie /// /// defines the granularity of the pie. The bigger the number, the finer the granularity. Min 4, Max 360 /// the time the pie should count in seconds /// the size of the shape /// the x-position of the shape /// the y-position of the shape /// the color of the middlepoint of the shape /// the color of the outer points of the shape 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 /// /// creating the vertices of the triangles that will be drawn /// private void CreateVertices() { _verticesList = new Dictionary(); 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( 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 } }