ispy.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // SuperTux - Ispy
  2. // Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.de>
  3. // 2022 Jiri Palecek <narre@protonmail.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/ispy.hpp"
  18. #include "editor/editor.hpp"
  19. #include "sprite/sprite.hpp"
  20. #include "supertux/flip_level_transformer.hpp"
  21. #include "supertux/sector.hpp"
  22. #include "util/log.hpp"
  23. #include "util/reader_mapping.hpp"
  24. #include "util/writer.hpp"
  25. Ispy::Ispy(const ReaderMapping& reader) :
  26. MovingSprite(reader, "images/objects/ispy/ispy.sprite", LAYER_TILES + 5, COLGROUP_DISABLED),
  27. m_state(ISPYSTATE_IDLE),
  28. m_script(),
  29. m_dir(Direction::LEFT)
  30. {
  31. reader.get("script", m_script);
  32. std::string dir_str;
  33. if (reader.get("direction", dir_str))
  34. m_dir = string_to_dir(dir_str);
  35. else if (!Editor::is_active())
  36. m_dir = Direction::LEFT;
  37. if (m_dir == Direction::AUTO)
  38. log_warning << "Setting an Ispy's direction to AUTO is no good idea." << std::endl;
  39. set_action("idle", m_dir);
  40. }
  41. ObjectSettings
  42. Ispy::get_settings()
  43. {
  44. ObjectSettings result = MovingSprite::get_settings();
  45. result.add_script(_("Script"), &m_script, "script");
  46. result.add_direction(_("Direction"), &m_dir,
  47. { Direction::LEFT, Direction::RIGHT, Direction::UP, Direction::DOWN }, "direction");
  48. result.reorder({"script", "facing-down", "direction", "x", "y"});
  49. return result;
  50. }
  51. void
  52. Ispy::after_editor_set()
  53. {
  54. MovingSprite::after_editor_set();
  55. set_action("idle", m_dir);
  56. }
  57. HitResponse
  58. Ispy::collision(GameObject& , const CollisionHit& )
  59. {
  60. return ABORT_MOVE;
  61. }
  62. void
  63. Ispy::update(float dt_sec)
  64. {
  65. if (m_state == ISPYSTATE_IDLE)
  66. {
  67. //Check if a player has been spotted
  68. Vector eye = m_col.m_bbox.get_middle();
  69. switch (m_dir)
  70. {
  71. case Direction::DOWN: eye = Vector(m_col.m_bbox.get_middle().x, m_col.m_bbox.get_bottom()); break;
  72. case Direction::UP: eye = Vector(m_col.m_bbox.get_middle().x, m_col.m_bbox.get_top()); break;
  73. case Direction::LEFT: eye = Vector(m_col.m_bbox.get_left(), m_col.m_bbox.get_middle().y); break;
  74. case Direction::RIGHT: eye = Vector(m_col.m_bbox.get_right(), m_col.m_bbox.get_middle().y); break;
  75. default: break;
  76. }
  77. if (Sector::get().can_see_player(eye))
  78. {
  79. set_action("alert", m_dir, 1);
  80. m_state = ISPYSTATE_ALERT;
  81. }
  82. }
  83. if (m_state == ISPYSTATE_ALERT)
  84. {
  85. if (m_sprite->animation_done())
  86. {
  87. set_action("hiding", m_dir, 1);
  88. m_state = ISPYSTATE_HIDING;
  89. Sector::get().run_script(m_script, "Ispy");
  90. }
  91. }
  92. if (m_state == ISPYSTATE_HIDING)
  93. {
  94. if (m_sprite->animation_done())
  95. {
  96. set_action("showing", m_dir, 1);
  97. m_state = ISPYSTATE_SHOWING;
  98. }
  99. }
  100. if (m_state == ISPYSTATE_SHOWING)
  101. {
  102. if (m_sprite->animation_done())
  103. {
  104. set_action("idle", m_dir);
  105. m_state = ISPYSTATE_IDLE;
  106. }
  107. }
  108. }
  109. void
  110. Ispy::on_flip(float height)
  111. {
  112. MovingSprite::on_flip(height);
  113. if (m_dir == Direction::UP)
  114. {
  115. m_dir = Direction::DOWN;
  116. set_action("idle-down");
  117. }
  118. else if (m_dir == Direction::DOWN)
  119. {
  120. m_dir = Direction::UP;
  121. set_action("idle-up");
  122. }
  123. }
  124. /* EOF */