toad.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Toad - A jumping toad
  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 "badguy/toad.hpp"
  17. #include "audio/sound_manager.hpp"
  18. #include "object/player.hpp"
  19. #include "sprite/sprite.hpp"
  20. namespace {
  21. const float VERTICAL_SPEED = -450; /**< y-speed when jumping */
  22. const float HORIZONTAL_SPEED = 320; /**< x-speed when jumping */
  23. const float TOAD_RECOVER_TIME = 0.5; /**< time to stand still before starting a (new) jump */
  24. static const std::string HOP_SOUND = "sounds/hop.ogg";
  25. }
  26. Toad::Toad(const ReaderMapping& reader) :
  27. BadGuy(reader, "images/creatures/toad/toad.sprite"),
  28. recover_timer(),
  29. state()
  30. {
  31. SoundManager::current()->preload(HOP_SOUND);
  32. }
  33. void
  34. Toad::initialize()
  35. {
  36. // initial state is JUMPING, because we might start airborne
  37. state = JUMPING;
  38. m_sprite->set_action(m_dir == Direction::LEFT ? "jumping-left" : "jumping-right");
  39. }
  40. void
  41. Toad::set_state(ToadState newState)
  42. {
  43. if (newState == IDLE) {
  44. m_physic.set_velocity_x(0);
  45. m_physic.set_velocity_y(0);
  46. if (!m_frozen)
  47. m_sprite->set_action(m_dir == Direction::LEFT ? "idle-left" : "idle-right");
  48. recover_timer.start(TOAD_RECOVER_TIME);
  49. } else
  50. if (newState == JUMPING) {
  51. m_sprite->set_action(m_dir == Direction::LEFT ? "jumping-left" : "jumping-right");
  52. m_physic.set_velocity_x(m_dir == Direction::LEFT ? -HORIZONTAL_SPEED : HORIZONTAL_SPEED);
  53. m_physic.set_velocity_y(VERTICAL_SPEED);
  54. SoundManager::current()->play( HOP_SOUND, get_pos());
  55. } else
  56. if (newState == FALLING) {
  57. Player* player = get_nearest_player();
  58. // face player
  59. if (player && (player->get_bbox().get_right() < m_col.m_bbox.get_left()) && (m_dir == Direction::RIGHT)) m_dir = Direction::LEFT;
  60. if (player && (player->get_bbox().get_left() > m_col.m_bbox.get_right()) && (m_dir == Direction::LEFT)) m_dir = Direction::RIGHT;
  61. m_sprite->set_action(m_dir == Direction::LEFT ? "idle-left" : "idle-right");
  62. }
  63. state = newState;
  64. }
  65. bool
  66. Toad::collision_squished(GameObject& object)
  67. {
  68. m_sprite->set_action(m_dir == Direction::LEFT ? "squished-left" : "squished-right");
  69. kill_squished(object);
  70. return true;
  71. }
  72. void
  73. Toad::collision_solid(const CollisionHit& hit)
  74. {
  75. // default behavior when frozen
  76. if (m_frozen || BadGuy::get_state() == STATE_BURNING)
  77. {
  78. BadGuy::collision_solid(hit);
  79. return;
  80. }
  81. // just default behaviour (i.e. stop at floor/walls) when squished
  82. if (BadGuy::get_state() == STATE_SQUISHED) {
  83. BadGuy::collision_solid(hit);
  84. return;
  85. }
  86. // ignore collisions while standing still
  87. if (state == IDLE) {
  88. return;
  89. }
  90. // check if we hit left or right while moving in either direction
  91. if (((m_physic.get_velocity_x() < 0) && hit.left) || ((m_physic.get_velocity_x() > 0) && hit.right)) {
  92. /*
  93. dir = dir == LEFT ? RIGHT : LEFT;
  94. if (state == JUMPING) {
  95. sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
  96. } else {
  97. sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
  98. }
  99. */
  100. m_physic.set_velocity_x(-0.25f*m_physic.get_velocity_x());
  101. }
  102. // check if we hit the floor while falling
  103. if ((state == FALLING) && hit.bottom) {
  104. set_state(IDLE);
  105. return;
  106. }
  107. // check if we hit the roof while climbing
  108. if ((state == JUMPING) && hit.top) {
  109. m_physic.set_velocity_y(0);
  110. }
  111. }
  112. HitResponse
  113. Toad::collision_badguy(BadGuy& , const CollisionHit& hit)
  114. {
  115. // behaviour for badguy collisions is the same as for collisions with solids
  116. collision_solid(hit);
  117. return CONTINUE;
  118. }
  119. void
  120. Toad::active_update(float dt_sec)
  121. {
  122. BadGuy::active_update(dt_sec);
  123. // change sprite when we are falling and not frozen
  124. if ((state == JUMPING) && (m_physic.get_velocity_y() > 0) && !m_frozen) {
  125. set_state(FALLING);
  126. return;
  127. }
  128. // jump when fully recovered and if not frozen
  129. if ((state == IDLE) && (recover_timer.check() && !m_frozen)) {
  130. set_state(JUMPING);
  131. return;
  132. }
  133. }
  134. void
  135. Toad::unfreeze()
  136. {
  137. BadGuy::unfreeze();
  138. initialize();
  139. }
  140. bool
  141. Toad::is_freezable() const
  142. {
  143. return true;
  144. }
  145. /* EOF */