Camera.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. namespace Robot_Rampage
  7. {
  8. public static class Camera
  9. {
  10. #region Declarations
  11. private static Vector2 position = Vector2.Zero;
  12. private static Vector2 viewPortSize = Vector2.Zero;
  13. private static Rectangle worldRectangle = new Rectangle(0, 0, 0, 0);
  14. #endregion
  15. #region Properties
  16. public static Vector2 Position
  17. {
  18. get { return position; }
  19. set
  20. {
  21. position = new Vector2(
  22. MathHelper.Clamp(value.X,
  23. worldRectangle.X,
  24. worldRectangle.Width - ViewPortWidth),
  25. MathHelper.Clamp(value.Y,
  26. worldRectangle.Y,
  27. worldRectangle.Height - ViewPortHeight));
  28. }
  29. }
  30. public static Rectangle WorldRectangle
  31. {
  32. get { return worldRectangle; }
  33. set { worldRectangle = value; }
  34. }
  35. public static int ViewPortWidth
  36. {
  37. get { return (int)viewPortSize.X; }
  38. set { viewPortSize.X = value; }
  39. }
  40. public static int ViewPortHeight
  41. {
  42. get { return (int)viewPortSize.Y; }
  43. set { viewPortSize.Y = value; }
  44. }
  45. public static Rectangle ViewPort
  46. {
  47. get
  48. {
  49. return new Rectangle(
  50. (int)Position.X, (int)Position.Y,
  51. ViewPortWidth, ViewPortHeight);
  52. }
  53. }
  54. #endregion
  55. #region Public Methods
  56. public static void Move(Vector2 offset)
  57. {
  58. Position += offset;
  59. }
  60. public static bool ObjectIsVisible(Rectangle bounds)
  61. {
  62. return (ViewPort.Intersects(bounds));
  63. }
  64. public static Vector2 Transform(Vector2 point)
  65. {
  66. return point - position;
  67. }
  68. public static Rectangle Transform(Rectangle rectangle)
  69. {
  70. return new Rectangle(
  71. rectangle.Left - (int)position.X,
  72. rectangle.Top - (int)position.Y,
  73. rectangle.Width,
  74. rectangle.Height);
  75. }
  76. #endregion
  77. }
  78. }