rusty_trampoline.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // SuperTux - Rusty Trampoline
  2. // Copyright (C) 2006 Wolfgang Becker <uafr@gmx.de>
  3. // Copyright (C) 2011 Jonas Kuemmerlin <rgcjonas@googlemail.com>
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #include "object/rusty_trampoline.hpp"
  18. #include "audio/sound_manager.hpp"
  19. #include "badguy/walking_badguy.hpp"
  20. #include "control/controller.hpp"
  21. #include "object/player.hpp"
  22. #include "sprite/sprite.hpp"
  23. #include "util/reader_mapping.hpp"
  24. /* Trampoline will accelerate Tux to to VY_BOUNCE, if
  25. * he jumps on it to VY_TRIGGER. */
  26. namespace {
  27. const std::string BOUNCE_SOUND = "sounds/trampoline.wav";
  28. const float VY_TRIGGER = -900; //negative, upwards
  29. const float VY_BOUNCE = -500;
  30. }
  31. RustyTrampoline::RustyTrampoline(const ReaderMapping& mapping) :
  32. Rock(mapping, "images/objects/rusty-trampoline/rusty-trampoline.sprite"),
  33. portable(true), counter(3)
  34. {
  35. SoundManager::current()->preload(BOUNCE_SOUND);
  36. mapping.get("counter", counter);
  37. mapping.get("portable", portable); //do we really need this?
  38. }
  39. void
  40. RustyTrampoline::update(float dt_sec)
  41. {
  42. if (m_sprite->animation_done()) {
  43. if (counter < 1) {
  44. remove_me();
  45. } else {
  46. set_action("normal");
  47. }
  48. }
  49. Rock::update(dt_sec);
  50. }
  51. ObjectSettings
  52. RustyTrampoline::get_settings()
  53. {
  54. ObjectSettings result = Rock::get_settings();
  55. result.add_int(_("Counter"), &counter, "counter", 3);
  56. result.add_bool(_("Portable"), &portable, "portable", true);
  57. result.reorder({"counter", "x", "y"});
  58. return result;
  59. }
  60. HitResponse
  61. RustyTrampoline::collision(GameObject& other, const CollisionHit& hit)
  62. {
  63. //Trampoline has to be on ground to work.
  64. if (on_ground) {
  65. auto player = dynamic_cast<Player*> (&other);
  66. //Trampoline works for player
  67. if (player) {
  68. float vy = player->get_physic().get_velocity_y();
  69. //player is falling down on trampoline
  70. if (hit.top && vy >= 0) {
  71. if (player->get_controller().hold(Control::JUMP)) {
  72. vy = VY_TRIGGER;
  73. } else {
  74. vy = VY_BOUNCE;
  75. }
  76. player->get_physic().set_velocity_y(vy);
  77. SoundManager::current()->play(BOUNCE_SOUND, get_pos());
  78. counter--;
  79. if (counter > 0) {
  80. set_action("swinging", 1);
  81. } else {
  82. set_action("breaking", 1);
  83. }
  84. return FORCE_MOVE;
  85. }
  86. }
  87. auto walking_badguy = dynamic_cast<WalkingBadguy*> (&other);
  88. //Trampoline also works for WalkingBadguy
  89. if (walking_badguy) {
  90. float vy = walking_badguy->get_velocity_y();
  91. //walking_badguy is falling down on trampoline
  92. if (hit.top && vy >= 0) {
  93. vy = VY_BOUNCE;
  94. walking_badguy->set_velocity_y(vy);
  95. SoundManager::current()->play(BOUNCE_SOUND, get_pos());
  96. counter--;
  97. if (counter > 0) {
  98. set_action("swinging", 1);
  99. } else {
  100. set_action("breaking", 1);
  101. }
  102. return FORCE_MOVE;
  103. }
  104. }
  105. }
  106. return Rock::collision(other, hit);
  107. }
  108. void
  109. RustyTrampoline::collision_solid(const CollisionHit& hit) {
  110. Rock::collision_solid(hit);
  111. }
  112. void
  113. RustyTrampoline::grab(MovingObject& object, const Vector& pos, Direction dir) {
  114. Rock::grab(object, pos, dir);
  115. }
  116. void
  117. RustyTrampoline::ungrab(MovingObject& object, Direction dir)
  118. {
  119. Rock::ungrab(object, dir);
  120. set_action("breaking", 1);
  121. counter = 0; //remove in update()
  122. }
  123. bool
  124. RustyTrampoline::is_portable() const
  125. {
  126. return Rock::is_portable() && portable;
  127. }
  128. /* EOF */