DarkTornado

PropertyUpdater Class Example

Jun 4th, 2011
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.95 KB | None | 0 0
  1. class PropertyUpdater
  2.         {
  3.             static Random rand = new Random(); //Random to set random initial value.
  4.  
  5.             public Property property; //The property
  6.             float wiggleAmount; //The amount of wiggle.
  7.             float value; //The value of the property.
  8.             bool growing; //Checking if the particle is growing (by wiggling).
  9.             public PropertyUpdater(Property property, bool randomWiggle)
  10.             {
  11.                 this.property = property;
  12.                 value = (float)rand.NextDouble() * (property.max - property.min) + property.min;
  13.                 if (randomWiggle)
  14.                 {
  15.                     growing = rand.Next(2) == 0;
  16.                     wiggleAmount = (float)rand.NextDouble() * (growing ? 1 : -1);
  17.                 }
  18.                 else
  19.                 {
  20.                     growing = false;
  21.                     wiggleAmount = 0;
  22.                 }
  23.             }
  24.  
  25.             //Update property
  26.             public void Update(GameTime gameTime)
  27.             {
  28.                 value += property.increase * (float)gameTime.ElapsedGameTime.TotalSeconds;
  29.                 if (growing)
  30.                 {
  31.                     wiggleAmount += (float)gameTime.ElapsedGameTime.TotalSeconds;
  32.                     if (wiggleAmount >= 1)
  33.                     {
  34.                         wiggleAmount = 1;
  35.                         growing = false;
  36.                     }
  37.                 }
  38.                 else
  39.                 {
  40.                     wiggleAmount -= (float)gameTime.ElapsedGameTime.TotalSeconds;
  41.                     if (wiggleAmount <= -1)
  42.                     {
  43.                         wiggleAmount = -1;
  44.                         growing = true;
  45.                     }
  46.                 }
  47.             }
  48.  
  49.             //Return the value of the property.
  50.             public float GetValue()
  51.             {
  52.                 return value + wiggleAmount * property.wiggle;
  53.             }
  54.         }
Advertisement
Add Comment
Please, Sign In to add comment