wind.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // SuperTux - Wind
  2. // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.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 "object/wind.hpp"
  17. #include "badguy/badguy.hpp"
  18. #include "editor/editor.hpp"
  19. #include "math/random.hpp"
  20. #include "object/particles.hpp"
  21. #include "object/player.hpp"
  22. #include "object/rock.hpp"
  23. #include "object/sprite_particle.hpp"
  24. #include "sprite/sprite.hpp"
  25. #include "sprite/sprite_manager.hpp"
  26. #include "supertux/sector.hpp"
  27. #include "util/reader_mapping.hpp"
  28. #include "video/drawing_context.hpp"
  29. Wind::Wind(const ReaderMapping& reader) :
  30. MovingObject(reader),
  31. ExposedObject<Wind, scripting::Wind>(this),
  32. blowing(),
  33. speed(0.0f, 0.0f),
  34. acceleration(),
  35. new_size(0.0f, 0.0f),
  36. dt_sec(0),
  37. affects_badguys(),
  38. affects_objects(),
  39. affects_player(),
  40. fancy_wind()
  41. {
  42. float w,h;
  43. reader.get("x", m_col.m_bbox.get_left(), 0.0f);
  44. reader.get("y", m_col.m_bbox.get_top(), 0.0f);
  45. reader.get("width", w, 32.0f);
  46. reader.get("height", h, 32.0f);
  47. m_col.m_bbox.set_size(w, h);
  48. reader.get("blowing", blowing, true);
  49. reader.get("speed-x", speed.x, 0.0f);
  50. reader.get("speed-y", speed.y, 0.0f);
  51. reader.get("acceleration", acceleration, 100.0f);
  52. reader.get("affects-badguys", affects_badguys, false);
  53. reader.get("affects-objects", affects_objects, false);
  54. reader.get("affects-player", affects_player, true);
  55. reader.get("fancy-wind", fancy_wind, false);
  56. set_group(COLGROUP_TOUCHABLE);
  57. }
  58. ObjectSettings
  59. Wind::get_settings()
  60. {
  61. new_size.x = m_col.m_bbox.get_width();
  62. new_size.y = m_col.m_bbox.get_height();
  63. ObjectSettings result = MovingObject::get_settings();
  64. //result.add_float("width", &new_size.x, "width", OPTION_HIDDEN);
  65. //result.add_float("height", &new_size.y, "height", OPTION_HIDDEN);
  66. result.add_float(_("Speed X"), &speed.x, "speed-x");
  67. result.add_float(_("Speed Y"), &speed.y, "speed-y");
  68. result.add_float(_("Acceleration"), &acceleration, "acceleration");
  69. result.add_bool(_("Blowing"), &blowing, "blowing", true);
  70. result.add_bool(_("Affects Badguys"), &affects_badguys, "affects-badguys", false);
  71. result.add_bool(_("Affects Objects"), &affects_objects, "affects-objects", false);
  72. result.add_bool(_("Affects Player"), &affects_player, "affects-player");
  73. result.add_bool(_("Fancy Particles"), &fancy_wind, "fancy-wind", false);
  74. result.reorder({"blowing", "speed-x", "speed-y", "acceleration", "affects-badguys", "affects-objects", "affects-player", "fancy-wind", "region", "name", "x", "y"});
  75. return result;
  76. }
  77. void
  78. Wind::update(float dt_sec_)
  79. {
  80. dt_sec = dt_sec_;
  81. if (!blowing) return;
  82. if (m_col.m_bbox.get_width() <= 16 || m_col.m_bbox.get_height() <= 16) return;
  83. Vector ppos = Vector(graphicsRandom.randf(m_col.m_bbox.get_left()+8, m_col.m_bbox.get_right()-8), graphicsRandom.randf(m_col.m_bbox.get_top()+8, m_col.m_bbox.get_bottom()-8));
  84. Vector pspeed = Vector(graphicsRandom.randf(speed.x-20, speed.x+20), graphicsRandom.randf(speed.y-20, speed.y+20));
  85. // TODO: Rotate sprite rather than just use 2 different actions
  86. // Approx. 1 particle per tile
  87. if (graphicsRandom.randf(0.f, 100.f) < (m_col.m_bbox.get_width() / 32.f) * (m_col.m_bbox.get_height() / 32.f))
  88. {
  89. // emit a particle
  90. if (fancy_wind)
  91. {
  92. Sector::get().add<SpriteParticle>("images/particles/wind.sprite", (std::abs(speed.x) > std::abs(speed.y)) ? "default" : "flip", ppos, ANCHOR_MIDDLE, pspeed, Vector(0, 0), LAYER_BACKGROUNDTILES + 1);
  93. }
  94. else
  95. {
  96. Sector::get().add<Particles>(ppos, 44, 46, pspeed, Vector(0, 0), 1, Color(.4f, .4f, .4f), 3, .1f, LAYER_BACKGROUNDTILES + 1);
  97. }
  98. }
  99. }
  100. void
  101. Wind::draw(DrawingContext& context)
  102. {
  103. if (Editor::is_active()) {
  104. context.color().draw_filled_rect(m_col.m_bbox, Color(0.0f, 1.0f, 1.0f, 0.6f),
  105. 0.0f, LAYER_OBJECTS);
  106. }
  107. }
  108. HitResponse
  109. Wind::collision(GameObject& other, const CollisionHit& )
  110. {
  111. if (!blowing) return ABORT_MOVE;
  112. auto player = dynamic_cast<Player*> (&other);
  113. if (player && affects_player)
  114. {
  115. player->override_velocity();
  116. if (!player->on_ground())
  117. {
  118. player->add_velocity(speed * acceleration * dt_sec, speed);
  119. }
  120. else
  121. {
  122. if (player->get_controller().hold(Control::RIGHT) || player->get_controller().hold(Control::LEFT))
  123. {
  124. player->add_velocity(Vector(speed.x, 0) * acceleration * dt_sec, speed);
  125. }
  126. else
  127. {
  128. //When on ground, get blown slightly differently, but the max speed is less than it would be otherwise seen as we take "friction" into account
  129. player->add_velocity((Vector(speed.x, 0) * 0.1f) * (acceleration+1), (Vector(speed.x, speed.y) * 0.5f));
  130. }
  131. }
  132. }
  133. auto badguy = dynamic_cast<BadGuy*>(&other);
  134. if (badguy && affects_badguys && badguy->can_be_affected_by_wind())
  135. {
  136. badguy->add_wind_velocity(speed * acceleration * dt_sec, speed);
  137. }
  138. auto rock = dynamic_cast<Rock*>(&other);
  139. if (rock && affects_objects)
  140. {
  141. rock->add_wind_velocity(speed * acceleration * dt_sec, speed);
  142. }
  143. return ABORT_MOVE;
  144. }
  145. void
  146. Wind::start()
  147. {
  148. blowing = true;
  149. }
  150. void
  151. Wind::stop()
  152. {
  153. blowing = false;
  154. }
  155. void
  156. Wind::on_flip(float height)
  157. {
  158. MovingObject::on_flip(height);
  159. speed.y = -speed.y;
  160. }
  161. /* EOF */