moving_object.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // SuperTux
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include "supertux/moving_object.hpp"
  17. #include <simplesquirrel/class.hpp>
  18. #include <simplesquirrel/vm.hpp>
  19. #include "editor/resize_marker.hpp"
  20. #include "supertux/sector.hpp"
  21. #include "util/reader_mapping.hpp"
  22. #include "util/writer.hpp"
  23. MovingObject::MovingObject() :
  24. m_col(COLGROUP_MOVING, *this),
  25. m_parent_dispenser()
  26. {
  27. }
  28. MovingObject::MovingObject(const ReaderMapping& reader) :
  29. GameObject(reader),
  30. m_col(COLGROUP_MOVING, *this),
  31. m_parent_dispenser()
  32. {
  33. float height, width;
  34. if (reader.get("width", width))
  35. m_col.m_bbox.set_width(width);
  36. if (reader.get("height", height))
  37. m_col.m_bbox.set_height(height);
  38. reader.get("x", m_col.m_bbox.get_left());
  39. reader.get("y", m_col.m_bbox.get_top());
  40. }
  41. MovingObject::~MovingObject()
  42. {
  43. }
  44. ObjectSettings
  45. MovingObject::get_settings()
  46. {
  47. ObjectSettings result = GameObject::get_settings();
  48. if (m_parent_dispenser)
  49. {
  50. result.remove("name");
  51. return result;
  52. }
  53. if (has_variable_size())
  54. {
  55. result.add_float(_("Width"), &m_col.m_bbox.get_width(), "width", {}, OPTION_HIDDEN);
  56. result.add_float(_("Height"), &m_col.m_bbox.get_height(), "height", {}, OPTION_HIDDEN);
  57. }
  58. result.add_float(_("X"), &m_col.m_bbox.get_left(), "x", {}, OPTION_HIDDEN);
  59. result.add_float(_("Y"), &m_col.m_bbox.get_top(), "y", {}, OPTION_HIDDEN);
  60. return result;
  61. }
  62. void
  63. MovingObject::set_parent_dispenser(Dispenser* dispenser)
  64. {
  65. m_parent_dispenser = dispenser;
  66. if (dispenser)
  67. {
  68. m_name.clear();
  69. }
  70. }
  71. void
  72. MovingObject::editor_select()
  73. {
  74. Sector::get().add<ResizeMarker>(this, ResizeMarker::Side::LEFT_UP, ResizeMarker::Side::LEFT_UP);
  75. Sector::get().add<ResizeMarker>(this, ResizeMarker::Side::LEFT_UP, ResizeMarker::Side::NONE);
  76. Sector::get().add<ResizeMarker>(this, ResizeMarker::Side::LEFT_UP, ResizeMarker::Side::RIGHT_DOWN);
  77. Sector::get().add<ResizeMarker>(this, ResizeMarker::Side::NONE, ResizeMarker::Side::LEFT_UP);
  78. Sector::get().add<ResizeMarker>(this, ResizeMarker::Side::NONE, ResizeMarker::Side::RIGHT_DOWN);
  79. Sector::get().add<ResizeMarker>(this, ResizeMarker::Side::RIGHT_DOWN, ResizeMarker::Side::LEFT_UP);
  80. Sector::get().add<ResizeMarker>(this, ResizeMarker::Side::RIGHT_DOWN, ResizeMarker::Side::NONE);
  81. Sector::get().add<ResizeMarker>(this, ResizeMarker::Side::RIGHT_DOWN, ResizeMarker::Side::RIGHT_DOWN);
  82. }
  83. void
  84. MovingObject::on_flip(float height)
  85. {
  86. Vector pos = get_pos();
  87. pos.y = height - pos.y - get_bbox().get_height();
  88. set_pos(pos);
  89. }
  90. void
  91. MovingObject::register_class(ssq::VM& vm)
  92. {
  93. ssq::Class cls = vm.addAbstractClass<MovingObject>("MovingObject", vm.findClass("GameObject"));
  94. cls.addFunc("get_x", &MovingObject::get_x);
  95. cls.addFunc("get_y", &MovingObject::get_y);
  96. cls.addFunc<void, MovingObject, float, float>("set_pos", &MovingObject::set_pos);
  97. cls.addFunc<void, MovingObject, float, float>("move", &MovingObject::move);
  98. cls.addFunc("get_width", &MovingObject::get_width);
  99. cls.addFunc("get_height", &MovingObject::get_height);
  100. }
  101. /* EOF */