Extensions.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. //---------------------------------------------------------------------------------
  2. // Ported to the Atomic Game Engine
  3. // Originally written for XNA by Michael Hoffman
  4. // Find the full tutorial at: http://gamedev.tutsplus.com/series/vector-shooter-xna/
  5. //----------------------------------------------------------------------------------
  6. using System;
  7. using AtomicEngine;
  8. namespace AtomicBlaster
  9. {
  10. static class Extensions
  11. {
  12. public static float ToAngle(this Vector2 vector)
  13. {
  14. return (float)Math.Atan2(vector.Y, vector.X);
  15. }
  16. public static Vector2 ScaleTo(this Vector2 vector, float length)
  17. {
  18. return vector * (length / vector.Length);
  19. }
  20. /*
  21. public static Point ToPoint(this Vector2 vector)
  22. {
  23. return new Point((int)vector.X, (int)vector.Y);
  24. }
  25. */
  26. public static float NextFloat(this Random rand, float minValue, float maxValue)
  27. {
  28. return (float)rand.NextDouble() * (maxValue - minValue) + minValue;
  29. }
  30. public static Vector2 NextVector2(this Random rand, float minLength, float maxLength)
  31. {
  32. double theta = rand.NextDouble() * 2 * Math.PI;
  33. float length = rand.NextFloat(minLength, maxLength);
  34. return new Vector2(length * (float)Math.Cos(theta), length * (float)Math.Sin(theta));
  35. }
  36. }
  37. }