sspiky.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/sspiky.hpp"
  17. #include "object/player.hpp"
  18. #include "sprite/sprite.hpp"
  19. #include "supertux/object_factory.hpp"
  20. SSpiky::SSpiky(const ReaderMapping& reader)
  21. : WalkingBadguy(reader, "images/creatures/spiky/sleepingspiky.sprite", "left", "right"), state(SSPIKY_SLEEPING)
  22. {
  23. walk_speed = 80;
  24. max_drop_height = 600;
  25. }
  26. void
  27. SSpiky::initialize()
  28. {
  29. state = SSPIKY_SLEEPING;
  30. physic.set_velocity_x(0);
  31. sprite->set_action(dir == LEFT ? "sleeping-left" : "sleeping-right");
  32. }
  33. void
  34. SSpiky::collision_solid(const CollisionHit& hit)
  35. {
  36. if(state != SSPIKY_WALKING) {
  37. BadGuy::collision_solid(hit);
  38. return;
  39. }
  40. WalkingBadguy::collision_solid(hit);
  41. }
  42. HitResponse
  43. SSpiky::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
  44. {
  45. if(state != SSPIKY_WALKING) {
  46. return BadGuy::collision_badguy(badguy, hit);
  47. }
  48. return WalkingBadguy::collision_badguy(badguy, hit);
  49. }
  50. void
  51. SSpiky::active_update(float elapsed_time) {
  52. if(state == SSPIKY_WALKING) {
  53. WalkingBadguy::active_update(elapsed_time);
  54. return;
  55. }
  56. if(state == SSPIKY_SLEEPING) {
  57. Player* player = get_nearest_player();
  58. if (player) {
  59. Rectf pb = player->get_bbox();
  60. bool inReach_left = (pb.p2.x >= bbox.p2.x-((dir == LEFT) ? 256 : 0));
  61. bool inReach_right = (pb.p1.x <= bbox.p1.x+((dir == RIGHT) ? 256 : 0));
  62. bool inReach_top = (pb.p2.y >= bbox.p1.y);
  63. bool inReach_bottom = (pb.p1.y <= bbox.p2.y);
  64. if (inReach_left && inReach_right && inReach_top && inReach_bottom) {
  65. // wake up
  66. sprite->set_action(dir == LEFT ? "waking-left" : "waking-right", 1);
  67. state = SSPIKY_WAKING;
  68. }
  69. }
  70. BadGuy::active_update(elapsed_time);
  71. }
  72. if(state == SSPIKY_WAKING) {
  73. if(sprite->animation_done()) {
  74. // start walking
  75. state = SSPIKY_WALKING;
  76. WalkingBadguy::initialize();
  77. }
  78. BadGuy::active_update(elapsed_time);
  79. }
  80. }
  81. void
  82. SSpiky::freeze()
  83. {
  84. WalkingBadguy::freeze();
  85. state = SSPIKY_WALKING; // if we get hit while sleeping, wake up :)
  86. }
  87. bool
  88. SSpiky::is_freezable() const
  89. {
  90. return true;
  91. }
  92. bool
  93. SSpiky::is_flammable() const
  94. {
  95. return state != SSPIKY_SLEEPING;
  96. }
  97. /* EOF */