jumpy.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 "badguy/jumpy.hpp"
  17. #include <algorithm>
  18. #include "object/player.hpp"
  19. #include "sprite/sprite.hpp"
  20. static const float JUMPYSPEED=-600;
  21. static const float JUMPY_MID_TOLERANCE=4;
  22. static const float JUMPY_LOW_TOLERANCE=2;
  23. Jumpy::Jumpy(const ReaderMapping& reader) :
  24. BadGuy(reader, "images/creatures/snowjumpy/snowjumpy.sprite"),
  25. pos_groundhit(),
  26. groundhit_pos_set(false)
  27. {
  28. m_sprite->set_action("left-middle");
  29. // TODO create a nice sound for this...
  30. //SoundManager::current()->preload("sounds/skid.wav");
  31. }
  32. void
  33. Jumpy::collision_solid(const CollisionHit& chit)
  34. {
  35. hit(chit);
  36. }
  37. HitResponse
  38. Jumpy::collision_badguy(BadGuy& , const CollisionHit& chit)
  39. {
  40. return hit(chit);
  41. }
  42. HitResponse
  43. Jumpy::hit(const CollisionHit& chit)
  44. {
  45. if (chit.bottom) {
  46. if (!groundhit_pos_set)
  47. {
  48. pos_groundhit = get_pos();
  49. groundhit_pos_set = true;
  50. }
  51. m_physic.set_velocity_y((m_frozen || get_state() != STATE_ACTIVE) ? 0 : JUMPYSPEED);
  52. // TODO create a nice sound for this...
  53. //SoundManager::current()->play("sounds/skid.wav");
  54. update_on_ground_flag(chit);
  55. } else if (chit.top) {
  56. m_physic.set_velocity_y(0);
  57. }
  58. return CONTINUE;
  59. }
  60. void
  61. Jumpy::active_update(float dt_sec)
  62. {
  63. BadGuy::active_update(dt_sec);
  64. if (m_frozen)
  65. return;
  66. auto player = get_nearest_player();
  67. if (player)
  68. {
  69. m_dir = (player->get_pos().x > get_pos().x) ? Direction::RIGHT : Direction::LEFT;
  70. }
  71. if (!groundhit_pos_set)
  72. {
  73. m_sprite->set_action(m_dir == Direction::LEFT ? "left-middle" : "right-middle");
  74. return;
  75. }
  76. if ( get_pos().y < (pos_groundhit.y - JUMPY_MID_TOLERANCE ) )
  77. m_sprite->set_action(m_dir == Direction::LEFT ? "left-up" : "right-up");
  78. else if ( get_pos().y >= (pos_groundhit.y - JUMPY_MID_TOLERANCE) &&
  79. get_pos().y < (pos_groundhit.y - JUMPY_LOW_TOLERANCE) )
  80. m_sprite->set_action(m_dir == Direction::LEFT ? "left-middle" : "right-middle");
  81. else
  82. m_sprite->set_action(m_dir == Direction::LEFT ? "left-down" : "right-down");
  83. }
  84. void
  85. Jumpy::freeze()
  86. {
  87. BadGuy::freeze();
  88. m_physic.set_velocity_y(std::max(0.0f, m_physic.get_velocity_y()));
  89. }
  90. bool
  91. Jumpy::is_freezable() const
  92. {
  93. return true;
  94. }
  95. bool
  96. Jumpy::is_flammable() const
  97. {
  98. return true;
  99. }
  100. /* EOF */