ispy.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // SuperTux - Ispy
  2. // Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.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/ispy.hpp"
  17. #include "editor/editor.hpp"
  18. #include "sprite/sprite.hpp"
  19. #include "supertux/sector.hpp"
  20. #include "util/log.hpp"
  21. #include "util/reader_mapping.hpp"
  22. #include "util/writer.hpp"
  23. Ispy::Ispy(const ReaderMapping& reader) :
  24. MovingSprite(reader, "images/objects/ispy/ispy.sprite", LAYER_TILES+5, COLGROUP_DISABLED),
  25. state(ISPYSTATE_IDLE),
  26. script(),
  27. dir(Direction::AUTO),
  28. m_facing_down(false)
  29. {
  30. // read script to execute
  31. reader.get("script", script);
  32. // read direction to face in
  33. std::string dir_str;
  34. if (reader.get("direction", dir_str)) {
  35. dir = string_to_dir(dir_str);
  36. } else {
  37. if (!Editor::is_active()) {
  38. dir = Direction::LEFT;
  39. }
  40. }
  41. reader.get("facing-down", m_facing_down, false);
  42. if (!Editor::is_active()) {
  43. if (m_facing_down) {
  44. dir = Direction::DOWN;
  45. }
  46. }
  47. if (dir == Direction::AUTO) {
  48. log_warning << "Setting an Ispy's direction to AUTO is no good idea" << std::endl;
  49. }
  50. // set initial sprite action
  51. m_sprite->set_action((dir == Direction::DOWN) ? "idle-down" : ((dir == Direction::LEFT) ? "idle-left" : "idle-right"));
  52. }
  53. ObjectSettings
  54. Ispy::get_settings()
  55. {
  56. ObjectSettings result = MovingSprite::get_settings();
  57. result.add_bool(_("Facing Down"), &m_facing_down, "facing-down", false);
  58. result.add_script(_("Script"), &script, "script");
  59. result.add_direction(_("Direction"), &dir, Direction::AUTO, "direction");
  60. result.reorder({"script", "facing-down", "direction", "x", "y"});
  61. return result;
  62. }
  63. void
  64. Ispy::after_editor_set()
  65. {
  66. MovingSprite::after_editor_set();
  67. m_sprite->set_action((dir == Direction::DOWN) ? "idle-down" : ((dir == Direction::LEFT) ? "idle-left" : "idle-right"));
  68. }
  69. HitResponse
  70. Ispy::collision(GameObject& , const CollisionHit& )
  71. {
  72. return ABORT_MOVE;
  73. }
  74. void
  75. Ispy::update(float )
  76. {
  77. if (state == ISPYSTATE_IDLE) {
  78. // check if a player has been spotted
  79. Vector eye = m_col.m_bbox.get_middle();
  80. if (dir == Direction::LEFT) eye = Vector(m_col.m_bbox.get_left(), m_col.m_bbox.get_middle().y);
  81. if (dir == Direction::RIGHT) eye = Vector(m_col.m_bbox.get_right(), m_col.m_bbox.get_middle().y);
  82. if (dir == Direction::UP) eye = Vector(m_col.m_bbox.get_middle().x, m_col.m_bbox.get_top());
  83. if (dir == Direction::DOWN) eye = Vector(m_col.m_bbox.get_middle().x, m_col.m_bbox.get_bottom());
  84. if (Sector::get().can_see_player(eye)) {
  85. m_sprite->set_action((dir == Direction::DOWN) ? "alert-down" : ((dir == Direction::LEFT) ? "alert-left" : "alert-right"), 1);
  86. state = ISPYSTATE_ALERT;
  87. }
  88. }
  89. if (state == ISPYSTATE_ALERT) {
  90. if (m_sprite->animation_done()) {
  91. m_sprite->set_action((dir == Direction::DOWN) ? "hiding-down" : ((dir == Direction::LEFT) ? "hiding-left" : "hiding-right"), 1);
  92. state = ISPYSTATE_HIDING;
  93. Sector::get().run_script(script, "Ispy");
  94. }
  95. }
  96. if (state == ISPYSTATE_HIDING) {
  97. if (m_sprite->animation_done()) {
  98. m_sprite->set_action((dir == Direction::DOWN) ? "showing-down" : ((dir == Direction::LEFT) ? "showing-left" : "showing-right"), 1);
  99. state = ISPYSTATE_SHOWING;
  100. }
  101. }
  102. if (state == ISPYSTATE_SHOWING) {
  103. if (m_sprite->animation_done()) {
  104. m_sprite->set_action((dir == Direction::DOWN) ? "idle-down" : ((dir == Direction::LEFT) ? "idle-left" : "idle-right"));
  105. state = ISPYSTATE_IDLE;
  106. }
  107. }
  108. }
  109. /* EOF */