Camera.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. namespace Tile_Engine
  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, worldRectangle.Width -
  24. ViewPortWidth),
  25. MathHelper.Clamp(value.Y,
  26. worldRectangle.Y, worldRectangle.Height -
  27. 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 WorldToScreen(Vector2 worldLocation)
  65. {
  66. return worldLocation - position;
  67. }
  68. public static Rectangle WorldToScreen(Rectangle worldRectangle)
  69. {
  70. return new Rectangle(
  71. worldRectangle.Left - (int)position.X,
  72. worldRectangle.Top - (int)position.Y,
  73. worldRectangle.Width,
  74. worldRectangle.Height);
  75. }
  76. public static Vector2 ScreenToWorld(Vector2 screenLocation)
  77. {
  78. return screenLocation + position;
  79. }
  80. public static Rectangle ScreenToWorld(Rectangle screenRectangle)
  81. {
  82. return new Rectangle(
  83. screenRectangle.Left + (int)position.X,
  84. screenRectangle.Top + (int)position.Y,
  85. screenRectangle.Width,
  86. screenRectangle.Height);
  87. }
  88. #endregion
  89. }
  90. }