object.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include <irrlicht/irrlicht.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <nlohmann/json.hpp>
  5. #include "object.h"
  6. #include "core.h"
  7. #include "object3d.h"
  8. #include "image.h"
  9. namespace wp
  10. {
  11. using json = nlohmann::json;
  12. object::object (json json_data)
  13. {
  14. json::const_iterator size = json_data.find ("size");
  15. json::const_iterator scale = json_data.find ("scale");
  16. json::const_iterator origin = json_data.find ("origin");
  17. if (size != json_data.end () && size.value ().is_string () == true)
  18. {
  19. std::string size_s = *size;
  20. this->m_size = core::ato2vf (size_s.c_str ());
  21. }
  22. if (scale != json_data.end () && scale.value ().is_string () == true)
  23. {
  24. std::string scale_s = *scale;
  25. this->m_scale = core::ato3vf (scale_s.c_str ());
  26. }
  27. if (origin != json_data.end () && origin.value ().is_string () == true)
  28. {
  29. std::string origin_s = *origin;
  30. this->m_origin = core::ato3vf (origin_s.c_str ());
  31. }
  32. json::const_iterator angles = json_data.find ("angles");
  33. json::const_iterator id = json_data.find ("id");
  34. json::const_iterator name = json_data.find ("name");
  35. if (angles != json_data.end () && (*angles).is_string () == true)
  36. {
  37. std::string angles_s = *angles;
  38. this->m_angles = core::ato3vf (angles_s.c_str ());
  39. }
  40. if (id != json_data.end () && (*id).is_null () == false)
  41. {
  42. this->m_id = *id;
  43. }
  44. if (name != json_data.end () && (*name).is_string () == true)
  45. {
  46. this->m_name = *name;
  47. }
  48. json::const_iterator image = json_data.find ("image");
  49. json::const_iterator model = json_data.find ("model");
  50. json::const_iterator particle = json_data.find ("particle");
  51. object3d::Type _type = object3d::Type::Type_None;
  52. if (image != json_data.end () && (*image).is_null () == false)
  53. {
  54. _type = object3d::Type::Type_Image;
  55. }
  56. if (model != json_data.end () && (*model).is_null () == false)
  57. {
  58. _type = object3d::Type::Type_Model;
  59. }
  60. if (particle != json_data.end () && (*particle).is_null () == false)
  61. {
  62. _type = object3d::Type::Type_Particle;
  63. }
  64. switch (_type)
  65. {
  66. case object3d::Type::Type_Image:
  67. this->m_object3d = new wp::image (json_data);
  68. break;
  69. case object3d::Type::Type_Model:
  70. break;
  71. case object3d::Type::Type_Particle:
  72. break;
  73. }
  74. json::const_iterator effects = json_data.find ("effects");
  75. if (effects != json_data.end () && (*effects).is_array () == true)
  76. {
  77. json::const_iterator cur = (*effects).begin ();
  78. json::const_iterator end = (*effects).end ();
  79. for (; cur != end; cur ++)
  80. {
  81. this->m_effects.push_back (new effect (*cur));
  82. }
  83. }
  84. }
  85. object::~object ()
  86. {
  87. if (this->m_object3d != nullptr)
  88. {
  89. delete this->m_object3d;
  90. }
  91. }
  92. }