Camera.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "Camera.h"
  2. #include "utility/misc.h"
  3. #include "input/Input.h"
  4. using WalrusRPG::Camera;
  5. using namespace WalrusRPG;
  6. using WalrusRPG::Input::Key;
  7. Camera::Camera(signed x, signed y)
  8. : x(x), y(y), render_area_width(320), render_area_height(240)
  9. {
  10. }
  11. Camera::~Camera()
  12. {
  13. // TODO if you allocate dynamically members
  14. }
  15. void Camera::update(unsigned dt)
  16. {
  17. UNUSED(dt);
  18. // TODO update map's data according to elasped time
  19. /*
  20. // Need to think aagain on how to go to a target point and/or we need to
  21. align the corner OR the center to this point.
  22. position += velocity * dt;
  23. velocity += acceleration * dt;
  24. */
  25. if (Input::key_down(Key::K_DOWN))
  26. y++;
  27. if (Input::key_down(Key::K_UP))
  28. y--;
  29. if (Input::key_down(Key::K_RIGHT))
  30. x++;
  31. if (Input::key_down(Key::K_LEFT))
  32. x--;
  33. }
  34. void Camera::set_x(signed x)
  35. {
  36. this->x = x;
  37. }
  38. signed Camera::get_x() const
  39. {
  40. return this->x;
  41. }
  42. void Camera::set_y(signed y)
  43. {
  44. this->y = y;
  45. }
  46. signed Camera::get_y() const
  47. {
  48. return this->y;
  49. }
  50. void Camera::set_center_x(signed x)
  51. {
  52. this->x = x - render_area_width/2;
  53. }
  54. signed Camera::get_center_x() const
  55. {
  56. return this->x - render_area_height/2;
  57. }
  58. void Camera::set_center_y(signed y)
  59. {
  60. this->y = y - render_area_height/2;
  61. }
  62. signed Camera::get_center_y() const
  63. {
  64. return this->y - render_area_height/2;
  65. }
  66. bool Camera::is_visible(const WalrusRPG::Utils::Rect &object) const
  67. {
  68. if ((in_range(object.x, x, x + (signed) render_area_width) ||
  69. in_range(object.x + (signed) object.width - 1, x,
  70. x + (signed) render_area_width)) &&
  71. (in_range(object.y, y, y + (signed) render_area_height) ||
  72. in_range(object.y + (signed) object.height - 1, y,
  73. y + (signed) render_area_height)))
  74. {
  75. return true;
  76. }
  77. else
  78. {
  79. return false;
  80. }
  81. }