Ecrofirt

Ring

Apr 5th, 2011
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6.  
  7. namespace Rings
  8. {
  9.     class Ring
  10.     {
  11.  
  12.         public bool Visible { get; set; }                       //a boolean property that dictates whether or not the ring is visible
  13.         public int WedgeSpan { get; set; }                      //An integer property that dictates the arclength of each wedge
  14.        
  15.         private int rotation = 0;                               //The current degree of rotation for the ring
  16.         private float radius = 0f;                              //The radius of the ring
  17.         private static float degree = (float)(Math.PI/180);     //This float represents 1 degree, in radians
  18.         private Position[] positions;                           //An array of position data for all 360 degrees, based upon the radius of the ring
  19.  
  20.         private Factory myFactory;                              //A factory which will be responsible for making wedges
  21.  
  22.  
  23.         List<Wedge> wedges;                                     //A list of wedges
  24.  
  25.         /// <summary>
  26.         /// This is a list of all the wedges in the ring
  27.         /// </summary>
  28.         public List<Wedge> Wedges
  29.         {
  30.             get
  31.             {
  32.                 return this.wedges;
  33.             }
  34.         }
  35.  
  36.  
  37.         /// <summary>
  38.         /// This gets or sets the current rotation for the ring.
  39.         /// If the rotation is set, all wedges will have their rotation updated
  40.         /// </summary>
  41.         public int Rotation
  42.         {
  43.             get
  44.             {
  45.                 return this.rotation;
  46.             }
  47.             set
  48.             {
  49.                 //Bound the rotation between 0 and 360 degrees
  50.                 //value = value < 0 ? 360 + value : value;
  51.                 if (value < 0) { value = 360 + value; }
  52.                 value=value%360;
  53.  
  54.                 this.rotation = value;
  55.  
  56.                 updateWedgePositions();
  57.             }
  58.         }
  59.  
  60.  
  61.         /// <summary>
  62.         /// Initializes a ring with a specified radius, degree of rotation, and arclength of each wedge
  63.         /// </summary>
  64.         /// <param name="radius">The radius of the ring, with the size of its wedges factored in</param>
  65.         /// <param name="rotation">A positive integer between 0 and 359 that represents the degrees of rotation for this ring</param>
  66.         /// <param name="wedgeSpan">The arclength of each wedge</param>
  67.         public Ring(float radius, int rotation, int wedgeSpan,bool visible, Factory myFactory)
  68.         {
  69.             this.radius = radius;           //Sets the ring's radius
  70.             this.rotation = rotation;       //Sets the ring's rotation
  71.             this.WedgeSpan = wedgeSpan;     //Sets the ring's wedgespan
  72.             this.myFactory = myFactory;     //Sets the ring's factory
  73.             this.Visible = visible;         //Sets whether or not the ring is visible
  74.  
  75.             initializeWedges(visible);      //Initializes all of the wedges in the ring
  76.             setPositions();                 //Sets up the position array
  77.            
  78.         }
  79.  
  80.  
  81.         /// <summary>
  82.         /// This function sets up the position array for the ring
  83.         /// </summary>
  84.         private void setPositions()
  85.         {
  86.             positions = new Position[360];
  87.  
  88.             //Loops through each position, and sets its corresponding values.
  89.             //Note: Each index is also the the degree for the position
  90.             for (int i = 0; i < positions.Length; i++)
  91.             {
  92.                 positions[i].Degrees = Game1.basePositions[i].Degrees;
  93.                 positions[i].Radians = Game1.basePositions[i].Radians;
  94.                 positions[i].X = Game1.basePositions[i].X * this.radius;
  95.                 positions[i].Y = Game1.basePositions[i].Y * this.radius;
  96.                 positions[i].LocationVector=new Vector2(positions[i].X,positions[i].Y);
  97.             }
  98.         }
  99.  
  100.         /// <summary>
  101.         /// Gets the Position information at a specific index
  102.         /// </summary>
  103.         /// <param name="index">The index to return</param>
  104.         /// <returns></returns>
  105.         public Position getWedgePosition(int index)
  106.         {
  107.             return positions[wedges[index].Rotation];
  108.         }
  109.  
  110.  
  111.         /// <summary>
  112.         /// Creates and initializes the wedges contained within the ring
  113.         /// </summary>
  114.         /// <param name="visible">Whether or not the wedges are visible</param>
  115.         private void initializeWedges(bool visible)
  116.         {
  117.             int numWedges;
  118.  
  119.  
  120.             numWedges = 360 / wedgeSpan;
  121.             wedges = new List<Wedge>(numWedges);
  122.  
  123.             //Fills the ring with random wedges
  124.             for (int i = 0; i < numWedges; i++)
  125.             {
  126.                 //Wedge newWedge = GameScreen.wedgeFactory.CreateWedge();
  127.                 Wedge newWedge = myFactory.CreateWedge(visible);
  128.                 newWedge.Rotation = i * rotation;
  129.                 wedges.Add(newWedge);
  130.                 //wedges.Add( new Wedge(i*rotation) );
  131.             }
  132.  
  133.         }
  134.  
  135.  
  136.         /// <summary>
  137.         /// Updates the position of each wedge
  138.         /// </summary>
  139.         private void updateWedgePositions()
  140.         {
  141.             //Iterates through each wedge and updates its rotation
  142.             for (int i = 0; i < wedges.Count; i++)
  143.             {
  144.  
  145.                 wedges[i].Rotation = ((i * WedgeSpan) + rotation) % 360;
  146.  
  147.             }
  148.  
  149.         }
  150.  
  151.         /// <summary>
  152.         /// Makes a currently-hidden wedge visible and changes its type to the proper WedgeType
  153.         /// </summary>
  154.         /// <param name="index">The index of the wedge that will become visible</param>
  155.         /// <param name="type">The WedgeType to set the wedge to when it is visible</param>
  156.         public void showWedge(int index, WedgeType type)
  157.         {
  158.            
  159.             wedges[index].Type = type;      //Sets the new type for the wedge
  160.             wedges[index].Visible = true;   //Makes the wedge visible
  161.             this.Visible = true;            //Ensures that the ring is visible
  162.  
  163.         }
  164.  
  165.         /// <summary>
  166.         /// Hides a currently-visible wedge.
  167.         /// After the wedge is hidden, it may hide the entire ring.
  168.         /// </summary>
  169.         /// <param name="index">The index of the wedge to be hidden</param>
  170.         public void hideWedge(int index)
  171.         {
  172.  
  173.             bool showRing = false;          //Set up a bool that will potentially hide the ring
  174.  
  175.             wedges[index].Visible = false;  //hide the wedge
  176.  
  177.             //checks if any of the wedges in the ring are visible. If they are, then the ring should be visible
  178.             for (int i = 0; i < wedges.Count; i++)
  179.             {
  180.                 if (wedges[i].Visible == true)
  181.                 {
  182.                     showRing = true;
  183.                     break;
  184.                 }
  185.             }
  186.             this.Visible = showRing;
  187.  
  188.         }
  189.  
  190.     }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment