Camera.cs 2.7 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. private static Vector2 position = Vector2.Zero;
  11. private static Vector2 viewPortSize = Vector2.Zero;
  12. private static Rectangle worldRectangle = new Rectangle(0, 0, 0, 0);
  13. public static Vector2 Position
  14. {
  15. get { return position; }
  16. set
  17. {
  18. position = new Vector2(
  19. MathHelper.Clamp(value.X,
  20. worldRectangle.X, worldRectangle.Width -
  21. ViewPortWidth),
  22. MathHelper.Clamp(value.Y,
  23. worldRectangle.Y, worldRectangle.Height -
  24. ViewPortHeight));
  25. }
  26. }
  27. public static Rectangle WorldRectangle
  28. {
  29. get { return worldRectangle; }
  30. set { worldRectangle = value; }
  31. }
  32. public static int ViewPortWidth
  33. {
  34. get { return (int)viewPortSize.X; }
  35. set { viewPortSize.X = value; }
  36. }
  37. public static int ViewPortHeight
  38. {
  39. get { return (int)viewPortSize.Y; }
  40. set { viewPortSize.Y = value; }
  41. }
  42. public static Rectangle ViewPort
  43. {
  44. get
  45. {
  46. return new Rectangle(
  47. (int)Position.X, (int)Position.Y,
  48. ViewPortWidth, ViewPortHeight);
  49. }
  50. }
  51. public static void Move(Vector2 offset)
  52. {
  53. Position += offset;
  54. }
  55. public static bool ObjectIsVisible(Rectangle bounds)
  56. {
  57. return (ViewPort.Intersects(bounds));
  58. }
  59. public static Vector2 WorldToScreen(Vector2 worldLocation)
  60. {
  61. return worldLocation - position;
  62. }
  63. public static Rectangle WorldToScreen(Rectangle worldRectangle)
  64. {
  65. return new Rectangle(
  66. worldRectangle.Left - (int)position.X,
  67. worldRectangle.Top - (int)position.Y,
  68. worldRectangle.Width,
  69. worldRectangle.Height);
  70. }
  71. public static Vector2 ScreenToWorld(Vector2 screenLocation)
  72. {
  73. return screenLocation + position;
  74. }
  75. public static Rectangle ScreenToWorld(Rectangle screenRectangle)
  76. {
  77. return new Rectangle(
  78. screenRectangle.Left + (int)position.X,
  79. screenRectangle.Top + (int)position.Y,
  80. screenRectangle.Width,
  81. screenRectangle.Height);
  82. }
  83. }
  84. }