object_factory.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // SuperTux
  2. // Copyright (C) 2004 Ricardo Cruz <rick2@aeiou.pt>
  3. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  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 "supertux/object_factory.hpp"
  18. #include <sstream>
  19. #include "supertux/game_object.hpp"
  20. ObjectFactory::ObjectFactory() :
  21. factories(),
  22. m_badguys_names(),
  23. m_badguys_params(),
  24. m_objects_names(),
  25. m_objects_params(),
  26. m_adding_badguys(false)
  27. {
  28. }
  29. std::unique_ptr<GameObject>
  30. ObjectFactory::create(const std::string& name, const ReaderMapping& reader) const
  31. {
  32. auto it = factories.find(name);
  33. if (it == factories.end())
  34. {
  35. std::stringstream msg;
  36. msg << "No factory for object '" << name << "' found.";
  37. throw std::runtime_error(msg.str());
  38. }
  39. else
  40. {
  41. return it->second.create(reader);
  42. }
  43. }
  44. std::string
  45. ObjectFactory::get_display_name(const std::string& name) const
  46. {
  47. auto it = factories.find(name);
  48. if (it == factories.end())
  49. {
  50. std::stringstream msg;
  51. msg << "No factory for object '" << name << "' found. Unable to get display name.";
  52. throw std::runtime_error(msg.str());
  53. }
  54. else
  55. {
  56. return it->second.get_display_name();
  57. }
  58. }
  59. bool
  60. ObjectFactory::has_params(const std::string& name, uint8_t params)
  61. {
  62. for (unsigned int i = 0; i < m_objects_names.size(); i++)
  63. {
  64. if (m_objects_names[i] == name)
  65. return m_objects_params[i] & params;
  66. }
  67. return false;
  68. }
  69. std::vector<std::string>
  70. ObjectFactory::get_registered_badguys(uint8_t params)
  71. {
  72. std::vector<std::string> out;
  73. for (unsigned int i = 0; i < m_badguys_names.size(); i++)
  74. {
  75. if (m_badguys_params[i] & params)
  76. out.push_back(m_badguys_names[i]);
  77. }
  78. return out;
  79. }
  80. std::vector<std::string>
  81. ObjectFactory::get_registered_objects(uint8_t params)
  82. {
  83. std::vector<std::string> out;
  84. for (unsigned int i = 0; i < m_objects_names.size(); i++)
  85. {
  86. if (m_objects_params[i] & params)
  87. out.push_back(m_objects_names[i]);
  88. }
  89. return out;
  90. }
  91. /* EOF */