path_object.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // SuperTux
  2. // Copyright (C) 2018 Ingo Ruhnke <grumbel@gmail.com>
  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/path_object.hpp"
  17. #include <optional>
  18. #include "editor/editor.hpp"
  19. #include "object/path_gameobject.hpp"
  20. #include "supertux/d_scope.hpp"
  21. #include "supertux/game_object_factory.hpp"
  22. #include "supertux/sector.hpp"
  23. #include "util/log.hpp"
  24. #include "util/reader_mapping.hpp"
  25. #include "util/writer.hpp"
  26. PathObject::PathObject() :
  27. m_path_handle(),
  28. m_path_uid(),
  29. m_walker()
  30. {
  31. }
  32. PathObject::~PathObject()
  33. {
  34. }
  35. void
  36. PathObject::init_path(const ReaderMapping& mapping, bool running_default)
  37. {
  38. bool running = running_default;
  39. mapping.get("running", running);
  40. std::optional<ReaderMapping> handle_map;
  41. if (mapping.get("handle", handle_map))
  42. {
  43. handle_map->get("scale_x", m_path_handle.m_scalar_pos.x);
  44. handle_map->get("scale_y", m_path_handle.m_scalar_pos.y);
  45. handle_map->get("offset_x", m_path_handle.m_pixel_offset.x);
  46. handle_map->get("offset_y", m_path_handle.m_pixel_offset.y);
  47. }
  48. std::string path_ref;
  49. std::optional<ReaderMapping> path_mapping;
  50. if (mapping.get("path", path_mapping))
  51. {
  52. auto& path_gameobject = d_gameobject_manager->add<PathGameObject>(*path_mapping, true);
  53. m_path_uid = path_gameobject.get_uid();
  54. m_walker.reset(new PathWalker(m_path_uid, running));
  55. }
  56. else if (mapping.get("path-ref", path_ref))
  57. {
  58. d_gameobject_manager->request_name_resolve(path_ref, [this, running](UID uid){
  59. if (!m_path_uid) m_path_uid = uid;
  60. m_walker.reset(new PathWalker(m_path_uid, running));
  61. });
  62. }
  63. }
  64. void
  65. PathObject::init_path_pos(const Vector& pos, bool running)
  66. {
  67. auto& path_gameobject = d_gameobject_manager->add<PathGameObject>(pos);
  68. m_path_uid = path_gameobject.get_uid();
  69. m_walker.reset(new PathWalker(path_gameobject.get_uid(), running));
  70. }
  71. void
  72. PathObject::save_state() const
  73. {
  74. PathGameObject* path_object = get_path_gameobject();
  75. if (path_object)
  76. path_object->save_state();
  77. }
  78. void
  79. PathObject::check_state() const
  80. {
  81. PathGameObject* path_object = get_path_gameobject();
  82. if (path_object)
  83. path_object->check_state();
  84. }
  85. PathGameObject*
  86. PathObject::get_path_gameobject() const
  87. {
  88. if(!d_gameobject_manager)
  89. return nullptr;
  90. return d_gameobject_manager->get_object_by_uid<PathGameObject>(m_path_uid);
  91. }
  92. Path*
  93. PathObject::get_path() const
  94. {
  95. auto path_gameobject = get_path_gameobject();
  96. if(!path_gameobject)
  97. {
  98. return nullptr;
  99. }
  100. return &path_gameobject->get_path();
  101. }
  102. std::string
  103. PathObject::get_path_ref() const
  104. {
  105. auto path_gameobject = get_path_gameobject();
  106. if(!path_gameobject)
  107. {
  108. return {};
  109. }
  110. return path_gameobject->get_name();
  111. }
  112. void
  113. PathObject::editor_clone_path(PathGameObject* path_object)
  114. {
  115. if (!path_object)
  116. return;
  117. auto new_path_obj = GameObjectFactory::instance().create(path_object->get_class_name(), path_object->save());
  118. auto& new_path = static_cast<PathGameObject&>(Editor::current()->get_sector()->add_object(std::move(new_path_obj)));
  119. new_path.regenerate_name();
  120. m_path_uid = new_path.get_uid();
  121. }
  122. void
  123. PathObject::editor_set_path_by_ref(const std::string& new_ref)
  124. {
  125. auto* path_obj = Editor::current()->get_sector()->get_object_by_name<PathGameObject>(new_ref);
  126. m_path_uid = path_obj->get_uid();
  127. }
  128. void
  129. PathObject::on_flip()
  130. {
  131. m_path_handle.m_scalar_pos.y = 1 - m_path_handle.m_scalar_pos.y;
  132. m_path_handle.m_pixel_offset.y = -m_path_handle.m_pixel_offset.y;
  133. }
  134. /* EOF */