circleplatform.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (C) 2020 Daniel Ward <weluvgoatz@gmail.com>
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. #include "object/circleplatform.hpp"
  16. #include "editor/editor.hpp"
  17. #include "math/util.hpp"
  18. #include "supertux/flip_level_transformer.hpp"
  19. #include "util/reader_mapping.hpp"
  20. #include "util/writer.hpp"
  21. #include "video/surface.hpp"
  22. CirclePlatform::CirclePlatform(const ReaderMapping& reader) :
  23. MovingSprite(reader, "images/objects/platforms/icebridge1.png", LAYER_OBJECTS, COLGROUP_STATIC),
  24. start_position(m_col.m_bbox.p1()),
  25. angle(0.0),
  26. radius(),
  27. speed(),
  28. timer(),
  29. time(0.0),
  30. m_radius_indicator(Surface::from_file("images/objects/platforms/circleplatform-editor.png"))
  31. {
  32. reader.get("radius", radius, 100.0f);
  33. reader.get("speed", speed, 2.0f);
  34. reader.get("time", time, 0.0f);
  35. if (!Editor::is_active())
  36. {
  37. m_col.m_bbox.set_pos(Vector(start_position.x + cosf(angle) * radius,
  38. start_position.y + sinf(angle) * radius));
  39. initialize();
  40. }
  41. }
  42. HitResponse
  43. CirclePlatform::collision(GameObject& other, const CollisionHit& )
  44. {
  45. return FORCE_MOVE;
  46. }
  47. ObjectSettings
  48. CirclePlatform::get_settings()
  49. {
  50. ObjectSettings result = MovingSprite::get_settings();
  51. result.add_float(_("Radius"), &radius, "radius", 100.0f);
  52. result.add_float(_("Speed"), &speed, "speed", 2.0f);
  53. result.add_float(_("Delay"), &time, "time", 0.0f);
  54. result.reorder({"radius", "speed", "time", "sprite", "x", "y"});
  55. return result;
  56. }
  57. void
  58. CirclePlatform::update(float dt_sec)
  59. {
  60. if (timer.get_timeleft() <= 0.00f)
  61. {
  62. time = 0;
  63. timer.stop();
  64. angle = fmodf(angle + dt_sec * speed, math::TAU);
  65. if (!Editor::is_active())
  66. {
  67. Vector newpos(start_position.x + cosf(angle) * radius,
  68. start_position.y + sinf(angle) * radius);
  69. m_col.set_movement(newpos - get_pos());
  70. m_col.propagate_movement(newpos - get_pos());
  71. }
  72. }
  73. }
  74. void
  75. CirclePlatform::on_flip(float height)
  76. {
  77. MovingObject::on_flip(height);
  78. start_position.y = height - start_position.y - get_bbox().get_height();
  79. FlipLevelTransformer::transform_flip(m_flip);
  80. }
  81. void
  82. CirclePlatform::draw(DrawingContext& context)
  83. {
  84. MovingSprite::draw(context);
  85. if (Editor::is_active())
  86. {
  87. Rectf rect(Vector(get_pos().x - radius + get_bbox().get_width() / 2,
  88. get_pos().y - radius + get_bbox().get_height() / 2),
  89. Sizef(radius * 2, radius * 2));
  90. context.color().draw_surface_scaled(m_radius_indicator, rect, m_layer);
  91. }
  92. }
  93. void
  94. CirclePlatform::initialize()
  95. {
  96. timer.start(time);
  97. }