CObjectConstructor.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "StdInc.h"
  2. #include "CObjectConstructor.h"
  3. /*
  4. * CObjectConstructor.cpp, part of VCMI engine
  5. *
  6. * Authors: listed in file AUTHORS in main folder
  7. *
  8. * License: GNU General Public License v2.0 or later
  9. * Full text of license available in license.txt file, in main folder
  10. *
  11. */
  12. CObjectWithRewardConstructor::CObjectWithRewardConstructor()
  13. {
  14. }
  15. void CObjectWithRewardConstructor::init(const JsonNode & config)
  16. {
  17. int id = config["id"].Float();
  18. std::string name = config["name"].String();
  19. for (auto & entry : config["types"].Struct()) // for each object type
  20. {
  21. JsonNode typeConf = entry.second;
  22. int subID = typeConf["id"].Float();
  23. objectInfos[id][subID].info.init(typeConf["properties"]);
  24. for (auto entry : typeConf["templates"].Struct())
  25. {
  26. ObjectTemplate tmpl;
  27. tmpl.id = Obj(id);
  28. tmpl.subid = subID;
  29. tmpl.readJson(entry.second);
  30. objectInfos[id][subID].templates.push_back(tmpl);
  31. }
  32. }
  33. }
  34. std::vector<ObjectTemplate> CObjectWithRewardConstructor::getTemplates(si32 type, si32 subType) const
  35. {
  36. assert(handlesID(type, subtype));
  37. return objectInfos.at(type).at(subType).templates;
  38. }
  39. CGObjectInstance * CObjectWithRewardConstructor::create(ObjectTemplate tmpl) const
  40. {
  41. assert(handlesID(tmpl));
  42. auto ret = new CObjectWithReward();
  43. ret->appearance = tmpl;
  44. return ret;
  45. }
  46. bool CObjectWithRewardConstructor::handlesID(si32 id, si32 subID) const
  47. {
  48. return objectInfos.count(id) && objectInfos.at(id).count(subID);
  49. }
  50. bool CObjectWithRewardConstructor::handlesID(ObjectTemplate tmpl) const
  51. {
  52. return handlesID(tmpl.id, tmpl.subid);
  53. }
  54. void CObjectWithRewardConstructor::configureObject(CGObjectInstance * object) const
  55. {
  56. assert(handlesID(object->appearance));
  57. objectInfos.at(object->ID).at(object->subID).info.configureObject(dynamic_cast<CObjectWithReward*>(object));
  58. }
  59. const IObjectInfo * CObjectWithRewardConstructor::getObjectInfo(ObjectTemplate tmpl) const
  60. {
  61. assert(handlesID(tmpl));
  62. return &objectInfos.at(tmpl.id).at(tmpl.subid).info;
  63. }